Just after some help with adding regex validation to fields.
I have created some code with my formula and it works perfectly, what i wanted to know is…
If i have a 2nd field on the same form, which will use the same regex validation can i add another underscore and the field ID to gform_field_validation_23_4 ?
Can someone tell me what the 10, 4 is for after validate_customer_field ?
I could see this on the gravity form info page for validations, and on one part it was 10, 5, but i couldnt see anything to tell me what it meant.
Last of all, the code below is to work on a text field, which it works, but is there anything that should change ?
Would i be right in thinking, i can just duplicate the “add_filter” line and just change the field ID for each extra field i want to use the same Regex?
The 10 is the priority the filter runs at (10 is the default), and the 4 is the number of arguments the function supports. These are WordPress conventions:
If you want to apply the same regex to another field in the same form, change the “23_4” to “23_YOUR_FIELD_ID”, like this:
// apply to field 3 in form 23
add_filter( 'gform_field_validation_23_4', 'validate_custom_field', 10, 4 );
// apply to field 17 in form 23
add_filter( 'gform_field_validation_23_17', 'validate_custom_field', 10, 4 );
function validate_custom_field( $result, $value, $form, $field ) {
$pattern = "/^(<200\/200|200\/200|<\d{1,3}(?:\.\d+)?\/\d{1,3}(?:\.\d+)?|\d{1,3}(?:\.\d+)?\/\d{1,3}(?:\.\d+)?|(HM|POL|LP))([+-])?$/";
if ( ! preg_match( $pattern, $value ) && $value != '' ) {
$result['is_valid'] = false;
$result['message'] = 'Please enter a valid value';
}
return $result;
}