Regex.php 3.2 KB

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