My coding abilities are very limited - I apologize if this is an obvious answer.
I have three zip fields on 2 forms that need to allow only 5 numbers.
I took code from here - Zipcode validation for Gravity Forms (5 digits, 2 approaches). · GitHub and try to adapt it to single line fields, however when I add the code, it applies the filter to every single line field on my forms.
How do I get it to just validate the zip on single line fields with the IDs 17, 43, 63 ?
$forms = array( '2', '4' );
# looping through the array to add an 'add_filter' for each
foreach ( $forms as $i => $form )
add_filter( "gform_field_validation_{$form}", 'custom_zip_validation', 10, 4 );
# the function
function custom_zip_validation( $result, $value, $form, $field )
{
if ( ( 'address' == $field->type ) && $result['is_valid'] )
{
$zip_value = rgar( $value, $field->id . '17, 43, 63' );
if ( ! ctype_digit( $zip_value ) || 5 != strlen( $zip_value ) )
{
$result['is_valid'] = false;
$result['message'] = 'Please check your Zip Code. It must be 5 digits only.';
}
}
return $result;
} // end custom_zip_validation