Zip Code Field Validation

I’m using the Address field in my forms so there is no way of setting a mask for the individual sub fields in the Address field. I need to validate the zip code field such that, upon submission, if the field contains anything other than 5 numerals, the form should show an error message just like any other required field. I’m using United States address format and no need for the zip+4 format.

I added the https://docs.gravityforms.com/gform_field_validation/#3-address-field-validation filter that mentions zip code validation to my functions.php file (see below), but this has no effect; the form will still accept and submit non-numeric values and strings longer than 5 caharcters. Any idea what I’m missing?

my functions.php (the zip code field in question is on form ID3 and is field 55.5

<?php
/*================================================
#1 Load the styles 
================================================*/
function dc_enqueue_styles() {
	wp_enqueue_style( 'divi-parent', get_template_directory_uri() . '/style.css' );
}
add_action( 'wp_enqueue_scripts', 'dc_enqueue_styles' );

add_filter( 'gform_field_validation_3_55.5', 'custom_zip_validation', 10, 4 );
function custom_zip_validation( $result, $value, $form, $field ) {
    if ( $result['is_valid'] ) {
        $acceptable_zips = array(
            '123',
            '456'
        );
 
        $zip_value = rgar( $value, $field->id . '55.5' );
 
        if ( ! in_array( $zip_value, $acceptable_zips ) ) {
            $result['is_valid'] = false;
            $result['message']  = 'Zip validation failed.';
        }
    }
 
    return $result;
}

I see you’ve used gform_field_validation_3_55.5 as the filter, have you tried gform_field_validation_3_55?

From what I see in the docs (and from what I’ve used myself in the past) it doesn’t work with the subID for the specific field inside a field-group and should be Form ID + Field ID. So the overall field ID (55 in this case).

I got it. You were right about gform_field_validation_3_55; then I referenced the subfield .5 below and it worked:

$zip_value = rgar( $value, $field->id . ‘.5’ );

Thanks!

I spoke too soon. The code now throws the error, but replacing the zip with a 5-digit string still continues to throw the same validation error.

Well, that’s one step closer to the solution either way :wink:

In the code you pasted above, the ‘demo zips’ are still mentioned (which are both 3 numbers). Is that the code you’re indeed using? Because that situation doesn’t resemble what you’re trying to achieve.

You could use ctype_digit + strlen inside the filter to check if the given value is indeed (5) digits:

if ( ! ctype_digit( $zip_value ) || 5 != strlen( $zip_value ) ) 
{
    $result['is_valid'] = false; 
    $result['message'] = 'Zip validation failed.'; 
}

So like this:

add_filter( ‘gform_field_validation_3_55’, ‘custom_zip_validation’, 10, 4 );
function custom_zip_validation( $result, $value, $form, $field ) {
if ( $result[‘is_valid’] ) {

    $zip_value = rgar( $value, $field->id . '.5' );

    if ( ! ctype_digit( $zip_value ) || 5 != strlen( $zip_value ) ) 

{
$result[‘is_valid’] = false;
$result[‘message’] = ‘Zip validation failed.’;
}
}

return $result;

}

That worked, but what if I want to use the same filter for more than one form on my site? I repeated the filter and updated the form but with both filters in my functions.php, the site was not accessible. What would the syntax look like for two forms / 2 field IDs?

Then you’d add a seperate add_filter for that form + field ID (and I’d select it at field type address inside the function instead of the field ID to keep it compact & more scalable).

So would the syntax look like this if the two forms are ID 6 and ID 7 with both having the zip field at 15.5?

add_filter( ‘gform_field_validation_6_15’, ‘gform_field_validation_7_15’, ‘custom_zip_validation’, 10, 4 );
function custom_zip_validation( $result, $value, $form, $field ) {
if ( $result[‘is_valid’] ) {

    $zip_value = rgar( $value, $field->id . '.5' );

    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;

}

I tried the above and it doesn’t work. So I’m not sure how to have the same filter run on multiple forms.

The add_filter should be used once per form. The first string is the filtername, the second string is the name of the function that should be used. In yours the first two strings are filter names and the third is the functionname. Since the second string is recognized by WordPress as the function and that function doesn’t exist: it doesn’t work.

I’ve added a gist with the two approaches I was referring to earlier: https://gist.github.com/hiranthi/b23884c144c5000db535b19a38c0a2f6

Perhaps that clarifies things a bit :slight_smile:

3 Likes

Thanks again!

Hi,

I am also looking to implement this solution. The only difference for me is I have several ‘zip’ fields on the same form.

How can I add the filter to every ‘Zip’ field on the form?

Thanks

Yes - I believe all you need to do is add an additional line like:
add_filter( ‘gform_field_validation_3_55’, ‘custom_zip_validation’, 10, 4 ); for each zip field in the form (in this example, ‘3’ is form ID and ‘55’ is the field ID of address field) replacing the " _55" with the field ID of additional address field.

If you are using a field for a zip code as a stand-alone and not part pf a address field, the function itself might be a little different.

Actually, Hiranthi’s code here accounts for multiple forms. You can add multiple form IDs on line form 4.

$forms = array( '6', '7' );

@chrishajer sorry but her solution is for multiple forms. I only have 1 form with 24 Address fields that need zip code validation.

@jcallanan Thank you. I guess I was looking for a more simpler solution rather then write ‘add_filter’ 24 times. I have 24 address fields and each of the zip codes need validation.

Will there be any issues if I write 24 ‘add_filter’ hooks?

Actually that’s quite possible, just remove the field ID (to apply it to a single form) and use the first gist where the ‘address’-field type is mentioned.

The add_filter would just be added once for the whole form and all address-fields in that form would have that same filter for the zipcode field.

Anyway, if you check the gist for zipcode-3.php (just added it) you’ll see what I mean :slight_smile:

@hiranthi You are a genius. I saw the gist but never really pay attention to the zipcode-2.php :man_facepalming:t4:

I owe you a cup of coffee for this one :coffee:

Cheers!

2 Likes

Oops sorry, my mistake.