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.
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?
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.
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:
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;
}
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;
}
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:
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.
I know this post is old but I was working on the same thing and figured what I used would be useful for someone else. I came up with a method to check a txt file. The zips in the text file are separated by one zip per line (no commas).
/**
* gform zip code validator.
*/
function gform_zip_checker($result, $value, $form, $field) {
$file = get_stylesheet_directory_uri().'/zips.txt'; // I have this in my child theme
if ( $result['is_valid'] && (!preg_match('/^' . $value . '$/m', file_get_contents($file))) ) {
$result['is_valid'] = false;
$result['message'] = 'We don\'t provide service to this area. If you believe this is incorrect please call us.';
}
return $result;
}
add_filter('gform_field_validation_2_8', 'gform_zip_checker', 10, 4); // gform_field_validation_FORMID_FIELDID