Alnum.class.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. class Staple_Form_Validate_Alnum extends Staple_Form_Validator
  22. {
  23. const DEFAULT_ERROR = 'Data is not alphanumeric';
  24. const REGEX = '/^[A-Za-z0-9]+$/';
  25. /**
  26. *
  27. * @param mixed $data
  28. * @return bool
  29. * @see Staple_Form_Validator::check()
  30. */
  31. public function check($data)
  32. {
  33. if(ctype_alnum($data))
  34. {
  35. return true;
  36. }
  37. else
  38. {
  39. $this->addError(self::DEFAULT_ERROR);
  40. }
  41. return false;
  42. }
  43. /**
  44. * (non-PHPdoc)
  45. * @see Staple_Form_Validator::clientJQuery()
  46. */
  47. public function clientJQuery($fieldType, Staple_Form_Element $field)
  48. {
  49. switch ($fieldType)
  50. {
  51. case 'Staple_Form_SelectElement':
  52. $fieldid = "#{$field->getId()}";
  53. $valstring = "#{$field->getId()} option:selected";
  54. break;
  55. case 'Staple_Form_RadioGroup':
  56. $fieldid = "input:radio[name={$field->getName()}]";
  57. $valstring = "input:radio[name={$field->getName()}]:checked";
  58. break;
  59. case 'Staple_Form_CheckboxElement':
  60. return '';
  61. break;
  62. default:
  63. $fieldid = "#{$field->getId()}";
  64. $valstring = $fieldid;
  65. }
  66. $script = "\t//Alphanumeric Validator for ".addslashes($field->getLabel())."\n";
  67. $script .= "\tif(!(".self::REGEX.".test($('$valstring').val())))\n\t{\n";
  68. $script .= "\t\terrors.push('".addslashes($field->getLabel()).": \\n{$this->clientJSError()}\\n');\n";
  69. $script .= "\t\t$('$fieldid').addClass('form_error');\n";
  70. $script .= "\t}\n";
  71. $script .= "\telse {\n";
  72. $script .= "\t\t$('$fieldid').removeClass('form_error');\n";
  73. $script .= "\t}\n";
  74. return $script;
  75. }
  76. }
  77. ?>