2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 // +----------------------------------------------------------------------+
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 // +----------------------------------------------------------------------+
21 require_once('HTML/QuickForm/Rule.php');
24 * Validates values using regular expressions
27 class HTML_QuickForm_Rule_Regex
extends HTML_QuickForm_Rule
30 * Array of regular expressions
32 * Array is in the format:
33 * $_data['rulename'] = 'pattern';
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]*/'
47 * Validates a value using a regular expression
49 * @param string $value Value to be checked
50 * @param string $regex Regular expression
52 * @return boolean true if value is valid
54 function validate($value, $regex = null)
56 if (isset($this->_data
[$this->name
])) {
57 if (!preg_match($this->_data
[$this->name
], $value)) {
61 if (!preg_match($regex, $value)) {
66 } // end func validate
69 * Adds new regular expressions to the list
71 * @param string $name Name of rule
72 * @param string $pattern Regular expression pattern
75 function addData($name, $pattern)
77 $this->_data
[$name] = $pattern;
81 function getValidationScript($options = null)
83 $regex = isset($this->_data
[$this->name
]) ?
$this->_data
[$this->name
] : $options;
85 return array(" var regex = " . $regex . ";\n", "{jsVar} != '' && !regex.test({jsVar})");
86 } // end func getValidationScript
88 } // end class HTML_QuickForm_Rule_Regex