Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310]

   1  <?php
   2  /* vim: set expandtab tabstop=4 shiftwidth=4: */
   3  // +----------------------------------------------------------------------+
   4  // | PHP version 4.0                                                      |
   5  // +----------------------------------------------------------------------+
   6  // | Copyright (c) 1997-2003 The PHP Group                                |
   7  // +----------------------------------------------------------------------+
   8  // | This source file is subject to version 2.0 of the PHP license,       |
   9  // | that is bundled with this package in the file LICENSE, and is        |
  10  // | available at through the world-wide-web at                           |
  11  // | http://www.php.net/license/2_02.txt.                                 |
  12  // | If you did not receive a copy of the PHP license and are unable to   |
  13  // | obtain it through the world-wide-web, please send a note to          |
  14  // | license@php.net so we can mail you a copy immediately.               |
  15  // +----------------------------------------------------------------------+
  16  // | Authors: Bertrand Mansion <bmansion@mamasam.com>                     |
  17  // +----------------------------------------------------------------------+
  18  //
  19  // $Id$
  20  
  21  require_once('HTML/QuickForm/Rule.php');
  22  
  23  /**
  24  * Validates values using regular expressions
  25  * @version     1.0
  26  */
  27  class HTML_QuickForm_Rule_Regex extends HTML_QuickForm_Rule
  28  {
  29      /**
  30       * Array of regular expressions
  31       *
  32       * Array is in the format:
  33       * $_data['rulename'] = 'pattern';
  34       *
  35       * @var     array
  36       * @access  private
  37       */
  38      var $_data = array(
  39                      'lettersonly'   => '/^[a-zA-Z]+$/',
  40                      'alphanumeric'  => '/^[a-zA-Z0-9]+$/',
  41                      'numeric'       => '/(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/',
  42                      'nopunctuation' => '/^[^().\/\*\^\?#!@$%+=,\"\'><~\[\]{}]+$/',
  43                      'nonzero'       => '/^-?[1-9][0-9]*/',
  44                      'positiveint'   => '/^[1-9]\d*$/'
  45                      );
  46  
  47      /**
  48       * Validates a value using a regular expression
  49       *
  50       * @param     string    $value      Value to be checked
  51       * @param     string    $regex      Regular expression
  52       * @access    public
  53       * @return    boolean   true if value is valid
  54       */
  55      function validate($value, $regex = null)
  56      {
  57          if (isset($this->_data[$this->name])) {
  58              if (!preg_match($this->_data[$this->name], $value)) {
  59                  return false;
  60              }
  61          } else {
  62              if (!preg_match($regex, $value)) {
  63                  return false;
  64              }
  65          }
  66          return true;
  67      } // end func validate
  68  
  69      /**
  70       * Adds new regular expressions to the list
  71       *
  72       * @param     string    $name       Name of rule
  73       * @param     string    $pattern    Regular expression pattern
  74       * @access    public
  75       */
  76      function addData($name, $pattern)
  77      {
  78          $this->_data[$name] = $pattern;
  79      } // end func addData
  80  
  81  
  82      function getValidationScript($options = null)
  83      {
  84          $regex = isset($this->_data[$this->name]) ? $this->_data[$this->name] : $options;
  85  
  86          return array("  var regex = " . $regex . ";\n", "{jsVar} != '' && !regex.test({jsVar})");
  87      } // end func getValidationScript
  88  
  89  } // end class HTML_QuickForm_Rule_Regex
  90  ?>