Range.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Checks that the length of value is within range
  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-2009 The PHP Group
  18. * @license http://www.php.net/license/3_01.txt PHP License 3.01
  19. * @version CVS: $Id: Range.php,v 1.8 2009/04/04 21:34:04 avb Exp $
  20. * @link http://pear.php.net/package/HTML_QuickForm
  21. */
  22. /**
  23. * Checks that the length of value is within range
  24. *
  25. * @category HTML
  26. * @package HTML_QuickForm
  27. * @author Bertrand Mansion <bmansion@mamasam.com>
  28. * @version Release: 3.2.11
  29. * @since 3.2
  30. */
  31. class HTML_QuickForm_Rule_Range extends HTML_QuickForm_Rule
  32. {
  33. /**
  34. * Validates a value using a range comparison
  35. *
  36. * @param string $value Value to be checked
  37. * @param mixed $options Int for length, array for range
  38. * @access public
  39. * @return boolean true if value is valid
  40. */
  41. function validate($value, $options)
  42. {
  43. // Modified by Ivan Tcholakov, 16-MAR-2010.
  44. //$length = strlen($value);
  45. $length = api_strlen($value);
  46. //
  47. switch ($this->name) {
  48. case 'minlength': return ($length >= $options);
  49. case 'maxlength': return ($length <= $options);
  50. default: return ($length >= $options[0] && $length <= $options[1]);
  51. }
  52. } // end func validate
  53. function getValidationScript($options = null)
  54. {
  55. switch ($this->name) {
  56. case 'minlength':
  57. $test = '{jsVar}.length < '.$options;
  58. break;
  59. case 'maxlength':
  60. $test = '{jsVar}.length > '.$options;
  61. break;
  62. default:
  63. $test = '({jsVar}.length < '.$options[0].' || {jsVar}.length > '.$options[1].')';
  64. }
  65. return array('', "{jsVar} != '' && {$test}");
  66. } // end func getValidationScript
  67. } // end class HTML_QuickForm_Rule_Range
  68. ?>