TL;DR:
Freshy implemented a custom ZIP code validation in Gravity Forms for a WooCommerce site. The solution checks user-entered ZIP codes against an approved list and prevents form submission if the ZIP is outside the service area. This guide outlines how this was accomplished, including implementation steps and edge case handling.
How-To Guide: Adding Custom ZIP Code Validation to Gravity Forms
Issue Background
A WordPress website owner requested a feature for their Gravity Forms:
They wanted to restrict form submissions to users located within their specific service area, defined by a list of 209 ZIP codes.
The site was receiving unwanted form submissions from outside its service area, which was costing the business time and resources.
Diagnosis
The Freshy team determined that Gravity Forms does not offer native ZIP code restriction functionality. Therefore, a custom code solution was required to:
- Validate the ZIP Code field against a predefined list.
- Display an error message and prevent submission if the ZIP wasn’t on the list.
Additionally:
- The site had multiple forms, but the initial request was for a single form.
- The client later inquired about applying the ZIP validation to all forms on the site.
Resolution Steps
1. Prepare the Approved ZIP Code List
The client provided a PDF file with the approved list of 209 ZIP codes.
Freshy converted this list into a PHP array to use in the validation logic.
$allowed_zips = ['20105', '20109', '20110', '20111', '20112', ...];
2. Add Custom Validation Logic
Freshy implemented the following validation hook in the site’s theme or a custom plugin:
add_filter('gform_field_validation_4_9', 'custom_zip_validation', 10, 4);
function custom_zip_validation($result, $value, $form, $field) {
$allowed_zips = ['20105', '20109', '20110', '20111', '20112', /* etc */];
if (!in_array($value, $allowed_zips)) {
$result['is_valid'] = false;
$result['message'] = 'Sorry, we do not service your area.';
}
return $result;
}
- 4 is the Form ID.
- 9 is the Field ID for the ZIP code field.
The filter checks the ZIP value during form validation and blocks submission if it is not approved.
3. Address Submission Errors
While testing on the staging site, Freshy discovered unrelated JavaScript errors preventing form submissions.
They confirmed these were not caused by the ZIP validation code and proceeded to safely move the validation logic to the live site.
4. Optional Expansion to All Forms
The client later asked about making this ZIP restriction global across all forms.
Freshy refactored the validation hook to dynamically identify any field labeled ZIP Code (field IDs vary per form) and applied the validation site-wide.
Final Outcome
The Gravity Forms on the site now prevent users outside the service area from submitting requests, improving lead quality and operational efficiency.
Additionally:
- The validation was successfully expanded to all forms across the site.
- The implementation allows for easy updates to the approved ZIP code list in the future.
Need help customizing Gravity Forms or WordPress functionality?
Freshy’s team can build tailored solutions for your business.
Contact us today for expert help