PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/vendor/php/HTML/QuickForm/Rule/Regex.php

https://github.com/HumboldtStateUniversity/HSU-UNL-Event-Publisher
PHP | 107 lines | 39 code | 10 blank | 58 comment | 7 complexity | 39a9fec0ef23e686e65d916cfb7cd5ad MD5 | raw file
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Validates values using regular expressions
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * LICENSE: This source file is subject to version 3.01 of the PHP license
  9. * that is available through the world-wide-web at the following URI:
  10. * http://www.php.net/license/3_01.txt If you did not receive a copy of
  11. * the PHP License and are unable to obtain it through the web, please
  12. * send a note to license@php.net so we can mail you a copy immediately.
  13. *
  14. * @category HTML
  15. * @package HTML_QuickForm
  16. * @author Bertrand Mansion <bmansion@mamasam.com>
  17. * @copyright 2001-2010 The PHP Group
  18. * @license http://www.php.net/license/3_01.txt PHP License 3.01
  19. * @version CVS: $Id: Regex.php 304451 2010-10-17 07:31:18Z avb $
  20. * @link http://pear.php.net/package/HTML_QuickForm
  21. */
  22. /**
  23. * Abstract base class for QuickForm validation rules
  24. */
  25. require_once 'HTML/QuickForm/Rule.php';
  26. /**
  27. * Validates values using regular expressions
  28. *
  29. * @category HTML
  30. * @package HTML_QuickForm
  31. * @author Bertrand Mansion <bmansion@mamasam.com>
  32. * @version Release: 3.2.12
  33. * @since 3.2
  34. */
  35. class HTML_QuickForm_Rule_Regex extends HTML_QuickForm_Rule
  36. {
  37. /**
  38. * Array of regular expressions
  39. *
  40. * Array is in the format:
  41. * $_data['rulename'] = 'pattern';
  42. *
  43. * @var array
  44. * @access private
  45. */
  46. var $_data = array(
  47. 'lettersonly' => '/^[a-zA-Z]+$/',
  48. 'alphanumeric' => '/^[a-zA-Z0-9]+$/',
  49. 'numeric' => '/(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/',
  50. 'nopunctuation' => '/^[^().\/\*\^\?#!@$%+=,\"\'><~\[\]{}]+$/',
  51. 'nonzero' => '/^-?[1-9][0-9]*/'
  52. );
  53. /**
  54. * Validates a value using a regular expression
  55. *
  56. * @param string $value Value to be checked
  57. * @param string $regex Regular expression
  58. * @access public
  59. * @return boolean true if value is valid
  60. */
  61. function validate($value, $regex = null)
  62. {
  63. // Fix for bug #10799: add 'D' modifier to regex
  64. if (isset($this->_data[$this->name])) {
  65. if (!preg_match($this->_data[$this->name] . 'D', $value)) {
  66. return false;
  67. }
  68. } else {
  69. if (!preg_match($regex . 'D', $value)) {
  70. return false;
  71. }
  72. }
  73. return true;
  74. } // end func validate
  75. /**
  76. * Adds new regular expressions to the list
  77. *
  78. * @param string $name Name of rule
  79. * @param string $pattern Regular expression pattern
  80. * @access public
  81. */
  82. function addData($name, $pattern)
  83. {
  84. $this->_data[$name] = $pattern;
  85. } // end func addData
  86. function getValidationScript($options = null)
  87. {
  88. $regex = isset($this->_data[$this->name]) ? $this->_data[$this->name] : $options;
  89. // bug #12376, converting unicode escapes and stripping 'u' modifier
  90. if ($pos = strpos($regex, 'u', strrpos($regex, '/'))) {
  91. $regex = substr($regex, 0, $pos) . substr($regex, $pos + 1);
  92. $regex = preg_replace('/(?<!\\\\)(?>\\\\\\\\)*\\\\x{([a-fA-F0-9]+)}/', '\\u$1', $regex);
  93. }
  94. return array(" var regex = " . $regex . ";\n", "{jsVar} != '' && !regex.test({jsVar})");
  95. } // end func getValidationScript
  96. } // end class HTML_QuickForm_Rule_Regex
  97. ?>