Validate ZIP Code against a list of codes

I have a client that offers services in certain areas, and charges certain prices. So I would like clients to:

  1. Enter a Zip Code
  2. If the Zip matches a service area Zip Code, allow them to continue to fill out the rest of the form.
  3. Have another field that dynamically displays the price for that area.

Are the ZIP codes contiguous ranges, or a simple list of 10 or 20 valid codes? What are all the valid ZIP codes? The number of codes will determine how to approach this. Thank you.

@chrishajer It will be a list, as one city that the client serves has about 45 zip codes. The other city has about 25.

I have included two images of example Zip Codes.

Screenshot%20(1) Screenshot

Based on that, I recommend using the gform_field_validation filter.

You can hard-code your ZIP codes into an array, and compare the ZIP code entered with your list, to determine if they should be able to continue the form or not. I recommend having a single field on the first page of the form, asking for the ZIP so they don’t waste time entering a bunch of data, only to have you tell them later that they are not in your service area.

It will require a small amount of PHP but can be certainly handled in Gravity Forms.

Thank you for your recommendation! I’m not too knowledgable with PHP, are you able to refer me to a code I can use as a starting point and try to figure this out?

Making some progress!!!

I was able to put the zip codes as an array and it is validating against my list. If it validates it proceeds to the rest of the form, if it doesn’t it gives the “Zip validation failed.” message.

Is there a way to redirect the user to a landing page if the validation fails? this way I can redirect them to a page where the user receives a message that we don’t service the area.

Definitely possible, but would require a bunch of custom snippets. Are the rates going to change from time to time?

So far, so good!

The gform_field_validation filter does not work like that; it returns a validation error when the form fails validation (in this case, the ZIP code is not in your service area.)

I have not tried it, but you might be able to use wp_redirect() to abort returning that validation error:

https://developer.wordpress.org/reference/functions/wp_redirect/

I don’t know what your code looks like exactly, but something like this might work:

add_filter( 'gform_field_validation_FORMID_FIELDID', 'custom_validation', 10, 4 );
function custom_validation( $result, $value, $form, $field ) {
	$url = 'https://www.example.com/landing-page/';
	// zip validation
	if ( $result['is_valid'] && ( ZIP IS NOT IN ARRAY ) ) {
		// failed validation, just redirect
		wp_redirect( $url );
		exit;
	}
	// passed validation, continue with the form validation for other fields
	return $result;
}

Update:

Super close to accomplishing, but I am facing an issue. When users enter the correct zip it works as it should, but if they enter the wrong zip code, it just gives me a never ending orange circle (I guess trying to validate it). Thing is, it works on the gravity forms “preview” sections, but not on the live site.

This is the code:

add_filter( 'gform_field_validation_2_48', 'custom_validation', 10, 4 );
function custom_validation( $result, $value, $form, $field ) {
	$url = 'https://domain.com/sorry/';
	  $acceptable_zips = array(
            '10026',
			'10027', (here I included a list of all acceptable codes, took them out to make it shorter)
        );
 
    $zip_value = rgar( $value, $field->id . '.5' );
  
  	// zip validation
	if ( ! in_array( $zip_value, $acceptable_zips ) ) {
	  
	// failed validation, just redirect
	wp_redirect( $url );
	exit;
	}
  
	// passed validation, continue with the form validation for other fields
	return $result;
}

Take a look at the suggestions for getting wp_redirect to work, here:

This post is over a year old, so I don’t know if you ever figured this out. I’ve been trying to figure out a solution for this, and I think I got something that works using wp_redirect:

add_filter( 'gform_field_validation_10_1', 'custom_zip_validation', 10, 4 );
function custom_zip_validation( $result, $value, $form, $field ) {
    if ( $result['is_valid'] ) {
        $acceptable_zips = array(
            '02138',
            '32207'
        );
 
        $zip_value = rgar( $value, $field->id . '.5' );
 
        if ( ! in_array( $zip_value, $acceptable_zips ) ) {
            wp_redirect( "http://www.example.com/", 301 );
            exit();
        }
    }
 
    return $result;
}
1 Like

Hi Chris,

Yes, this looks very similar to the solution I currently have implemented on the site.

Working well so far. What I did was direct to a “Sorry Page” if the Zip is not within the array. If it is, then I use Gravity Form’s confirmations and have it redirect to specific forms using the conditional logic.

1 Like

Hi,
I have a same query. Can you show me how you register the field?

I have similar query. But don’t know where to from. Please help me

Hi
Can you help me with this same query?

© 2008 - 2019. Gravity Forms is a project by Rocketgenius Inc.