Range.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. * Abstract base class for QuickForm validation rules
  24. */
  25. require_once 'HTML/QuickForm/Rule.php';
  26. /**
  27. * Checks that the length of value is within range
  28. *
  29. * @category HTML
  30. * @package HTML_QuickForm
  31. * @author Bertrand Mansion <bmansion@mamasam.com>
  32. * @version Release: 3.2.11
  33. * @since 3.2
  34. */
  35. class HTML_QuickForm_Rule_Range extends HTML_QuickForm_Rule
  36. {
  37. /**
  38. * Validates a value using a range comparison
  39. *
  40. * @param string $value Value to be checked
  41. * @param mixed $options Int for length, array for range
  42. * @access public
  43. * @return boolean true if value is valid
  44. */
  45. function validate($value, $options)
  46. {
  47. // Modified by Ivan Tcholakov, 16-MAR-2010.
  48. //$length = strlen($value);
  49. $length = api_strlen($value);
  50. //
  51. switch ($this->name) {
  52. case 'minlength': return ($length >= $options);
  53. case 'maxlength': return ($length <= $options);
  54. default: return ($length >= $options[0] && $length <= $options[1]);
  55. }
  56. } // end func validate
  57. function getValidationScript($options = null)
  58. {
  59. switch ($this->name) {
  60. case 'minlength':
  61. $test = '{jsVar}.length < '.$options;
  62. break;
  63. case 'maxlength':
  64. $test = '{jsVar}.length > '.$options;
  65. break;
  66. default:
  67. $test = '({jsVar}.length < '.$options[0].' || {jsVar}.length > '.$options[1].')';
  68. }
  69. return array('', "{jsVar} != '' && {$test}");
  70. } // end func getValidationScript
  71. } // end class HTML_QuickForm_Rule_Range
  72. ?>