Regex.class.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * @author Ironpilot
  4. * @copyright Copywrite (c) 2011, STAPLE CODE
  5. *
  6. * This file is part of the STAPLE Framework.
  7. *
  8. * The STAPLE Framework is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by the
  10. * Free Software Foundation, either version 3 of the License, or (at your option)
  11. * any later version.
  12. *
  13. * The STAPLE Framework is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  15. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  16. * more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with the STAPLE Framework. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. class Staple_Form_Validate_Regex extends Staple_Form_Validator
  23. {
  24. /**
  25. * Constant for validating usernames. Usernames must be 6-50 characters and include only letters, numbers and underscores.
  26. * @var string
  27. */
  28. const USERNAME = '/^[a-zA-Z0-9_]{6,50}$/';
  29. /**
  30. * Constant for complex passwords. Requires minimum length a 8 characters, one lowercase letter, one uppercase letter, one number, a special character and no whitespace.
  31. * @var string
  32. */
  33. const PASSWORD = '/(?=^.{8,}$)(?=.*[a-z])(?=.*[A-Z])(?=.*[\W_])(?=^.*[^\s].*$).*$/';
  34. /**
  35. * A string regular expression
  36. * @var string
  37. */
  38. protected $regex;
  39. /**
  40. * Array of Matches
  41. * @var array
  42. */
  43. protected $matches;
  44. /**
  45. * Constructor sets the regex value to validate against and supports an optional user message.
  46. * @param string $regex
  47. * @param string $usermsg
  48. */
  49. function __construct($regex,$usermsg = NULL)
  50. {
  51. $this->setRegex($regex);
  52. parent::__construct($usermsg);
  53. }
  54. /**
  55. * Set the regex to validate against.
  56. * @param string $regex
  57. */
  58. public function setRegex($regex)
  59. {
  60. $this->regex = $regex;
  61. return $this;
  62. }
  63. /**
  64. * Returns the regex value
  65. * @return the $regex
  66. */
  67. public function getRegex()
  68. {
  69. return $this->regex;
  70. }
  71. /**
  72. * @return array
  73. */
  74. public function getMatches()
  75. {
  76. return $this->matches;
  77. }
  78. /**
  79. *
  80. * @param mixed $data
  81. * @return bool
  82. * @see Staple_Form_Validator::check()
  83. */
  84. public function check($data)
  85. {
  86. if(preg_match($this->regex, $data, $this->matches) >= 1)
  87. {
  88. return true;
  89. }
  90. else
  91. {
  92. $this->addError();
  93. return false;
  94. }
  95. }
  96. /**
  97. * (non-PHPdoc)
  98. * @see Staple_Form_Validator::clientJQuery()
  99. */
  100. public function clientJQuery($fieldType, Staple_Form_Element $field)
  101. {
  102. switch ($fieldType)
  103. {
  104. case 'Staple_Form_SelectElement':
  105. $fieldid = "#{$field->getId()}";
  106. $valstring = "#{$field->getId()} option:selected";
  107. break;
  108. case 'Staple_Form_RadioGroup':
  109. $fieldid = "input:radio[name={$field->getName()}]";
  110. $valstring = "input:radio[name={$field->getName()}]:checked";
  111. break;
  112. case 'Staple_Form_CheckboxElement':
  113. return '';
  114. break;
  115. default:
  116. $fieldid = "#{$field->getId()}";
  117. $valstring = $fieldid;
  118. }
  119. $script = "\t//Regex Validator for ".addslashes($field->getLabel())."\n";
  120. $script .= "\tif(!(".$this->getRegex().".test($('$valstring').val())))\n\t{\n";
  121. $script .= "\t\terrors.push('".addslashes($field->getLabel()).": \\n{$this->clientJSError()}\\n');\n";
  122. $script .= "\t\t$('$fieldid').addClass('form_error');\n";
  123. $script .= "\t}\n";
  124. $script .= "\telse {\n";
  125. $script .= "\t\t$('$fieldid').removeClass('form_error');\n";
  126. $script .= "\t}\n";
  127. return $script;
  128. }
  129. }
  130. ?>