Advertisement
chrishajer

Custom validation for a number format

Sep 15th, 2012
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.29 KB | None | 0 0
  1. <?php
  2. // http://www.gravityhelp.com/forums/topic/number-masked-field-keeps-resetting-tablet-keyboard-to-alpha
  3. // change 160 here to your form ID
  4. add_filter('gform_validation_160', 'force_input_format');
  5. function force_input_format($validation_result){
  6.         // this assumes the FHA Case # is entered in field one on your form
  7.         // change this input_ number if it's a different field
  8.         // try to match the first 10 characters after removing leading whitespace
  9.         $case_number = substr(trim(rgpost('input_1')), 0, 10);
  10.         // return a validation error if $case_number is not in the proper format, or if we needed to edit the $case_number at all before checking it.
  11.         if (!preg_match("/\d{3}-\d{6}/", $case_number) || ($case_number != rgpost('input_1'))) {
  12.         $validation_result['is_valid'] = false;
  13.         foreach($validation_result['form']['fields'] as &$field){
  14.             // field 1 is the field where we want to show the validation message
  15.             if($field['id'] == 1){
  16.                 $field['failed_validation'] = true;
  17.                                 $field['validation_message'] = 'The FHA Case Number code you entered is invalid: please check the number and try again.';
  18.                 break;
  19.             }
  20.         }
  21.     }
  22.     return $validation_result;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement