Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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                      );
  45  
  46      /**
  47       * Validates a value using a regular expression
  48       *
  49       * @param     string    $value      Value to be checked
  50       * @param     string    $regex      Regular expression
  51       * @access    public
  52       * @return    boolean   true if value is valid
  53       */
  54      function validate($value, $regex = null)
  55      {
  56          if (isset($this->_data[$this->name])) {
  57              if (!preg_match($this->_data[$this->name], $value)) {
  58                  return false;
  59              }
  60          } else {
  61              if (!preg_match($regex, $value)) {
  62                  return false;
  63              }
  64          }
  65          return true;
  66      } // end func validate
  67  
  68      /**
  69       * Adds new regular expressions to the list
  70       *
  71       * @param     string    $name       Name of rule
  72       * @param     string    $pattern    Regular expression pattern
  73       * @access    public
  74       */
  75      function addData($name, $pattern)
  76      {
  77          $this->_data[$name] = $pattern;
  78      } // end func addData
  79  
  80  
  81      function getValidationScript($options = null)
  82      {
  83          $regex = isset($this->_data[$this->name]) ? $this->_data[$this->name] : $options;
  84  
  85          return array("  var regex = " . $regex . ";\n", "{jsVar} != '' && !regex.test({jsVar})");
  86      } // end func getValidationScript
  87  
  88  } // end class HTML_QuickForm_Rule_Regex
  89  ?>