1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- <?php
- /**
- * @author Ironpilot
- * @copyright Copywrite (c) 2011, STAPLE CODE
- *
- * This file is part of the STAPLE Framework.
- *
- * The STAPLE Framework is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published by the
- * Free Software Foundation, either version 3 of the License, or (at your option)
- * any later version.
- *
- * The STAPLE Framework is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
- * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
- * more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with the STAPLE Framework. If not, see <http://www.gnu.org/licenses/>.
- */
- class Staple_Form_Validate_Zip extends Staple_Form_Validator
- {
- const DEFAULT_ERROR = 'Zip Code is invalid.';
- const REGEX = '/^\d{5}([\-]\d{4})?$/';
- /**
- *
- * @param mixed $data
-
- * @return bool
-
- * @see Staple_Form_Validator::check()
- */
- public function check($data)
- {
- if(preg_match(self::REGEX, $data))
- {
- return true;
- }
- else
- {
- $this->addError();
- return false;
- }
- }
-
- public function clientJQuery($fieldType, Staple_Form_Element $field)
- {
- switch ($fieldType)
- {
- case 'Staple_Form_SelectElement':
- $fieldid = "#{$field->getId()}";
- $valstring = "#{$field->getId()} option:selected";
- break;
- case 'Staple_Form_RadioGroup':
- $fieldid = "input:radio[name={$field->getName()}]";
- $valstring = "input:radio[name={$field->getName()}]:checked";
- break;
- case 'Staple_Form_CheckboxElement':
- return '';
- break;
- default:
- $fieldid = "#{$field->getId()}";
- $valstring = $fieldid;
- }
-
- $script = "\t//Zip Code Validator for ".addslashes($field->getLabel())."\n";
- $script .= "\tif(!(".self::REGEX.".test($('$valstring').val())))\n\t{\n";
- $script .= "\t\terrors.push('".addslashes($field->getLabel()).": \\n{$this->clientJSError()}\\n');\n";
- $script .= "\t\t$('$fieldid').addClass('form_error');\n";
- $script .= "\t}\n";
- $script .= "\telse {\n";
- $script .= "\t\t$('$fieldid').removeClass('form_error');\n";
- $script .= "\t}\n";
-
- return $script;
- }
- }
- ?>
|