HTML_QuickForm
[ class tree: HTML_QuickForm ] [ index: HTML_QuickForm ] [ all elements ]

Source for file Regex.php

Documentation is available at Regex.php

  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: Regex.php,v 1.3 2003/11/03 16:08:24 avb Exp $
  20.  
  21. require_once('HTML/QuickForm/Rule.php');
  22.  
  23. /**
  24. * Validates values using regular expressions
  25. @version     1.0
  26. */
  27. {
  28.     /**
  29.      * Array of regular expressions
  30.      *
  31.      * Array is in the format:
  32.      * $_data['rulename'] = 'pattern';
  33.      *
  34.      * @var     array 
  35.      * @access  private
  36.      */
  37.     var $_data = array(
  38.                     'lettersonly'   => '/^[a-zA-Z]+$/',
  39.                     'alphanumeric'  => '/^[a-zA-Z0-9]+$/',
  40.                     'numeric'       => '/(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/',
  41.                     'nopunctuation' => '/^[^().\/\*\^\?#!@$%+=,\"\'><~\[\]{}]+$/',
  42.                     'nonzero'       => '/^-?[1-9][0-9]*/'
  43.                     );
  44.  
  45.     /**
  46.      * Validates a value using a regular expression
  47.      *
  48.      * @param     string    $value      Value to be checked
  49.      * @param     string    $regex      Regular expression
  50.      * @access    public
  51.      * @return    boolean   true if value is valid
  52.      */
  53.     function validate($value$regex = null)
  54.     {
  55.         if (isset($this->_data[$this->name])) {
  56.             if (!preg_match($this->_data[$this->name]$value)) {
  57.                 return false;
  58.             }
  59.         else {
  60.             if (!preg_match($regex$value)) {
  61.                 return false;
  62.             }
  63.         }
  64.         return true;
  65.     // end func validate
  66.  
  67.     /**
  68.      * Adds new regular expressions to the list
  69.      *
  70.      * @param     string    $name       Name of rule
  71.      * @param     string    $pattern    Regular expression pattern
  72.      * @access    public
  73.      */
  74.     function addData($name$pattern)
  75.     {
  76.         $this->_data[$name$pattern;
  77.     // end func addData
  78.  
  79.  
  80.     function getValidationScript($options = null)
  81.     {
  82.         $regex = isset($this->_data[$this->name]$this->_data[$this->name$options;
  83.  
  84.         return array("  var regex = " $regex ";\n""{jsVar} != '' && !regex.test({jsVar})");
  85.     // end func getValidationScript
  86.  
  87. // end class HTML_QuickForm_Rule_Regex
  88. ?>

Documentation generated on Mon, 11 Mar 2019 14:16:36 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.