Phone.class.php 2.3 KB

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