The Problem: Unwanted Submissions from Outside Service Areas
The client’s WooCommerce site was getting flooded with form entries from users — wasting valuable time and resources.
They wanted a simple, automatic way to stop this and only allow form submissions from within the defined service area using zip-code or address fields.
What We Found
- Gravity Forms doesn’t support this natively. Gravity forms’ default settings do not include zip validation or address-field checks. There’s no built-in ZIP code validation.
- A custom solution was required using a predefined ZIP list, validation hook, and programming logic.
- Initially, the client wanted this for one form, but later expanded it to cover all forms on the site, ensuring proper form validation, field validation, and form submission control.
Implementation Steps
Step 1: Create a ZIP Code List
We converted the client’s list of ZIP codes into a PHP array:
php
CopyEdit
$allowed_zips = [‘20105’, ‘20109’, ‘20110’, ‘20111’, ‘20112’, …];
Step 2: Add Custom Validation Hook
We used a Gravity Forms filter to check the ZIP code field before submission:
php
CopyEdit
add_filter(‘gform_field_validation_4_9’, ‘custom_zip_validation’, 10, 4);
function custom_zip_validation($result, $value, $form, $field) {
$allowed_zips = […]; // your ZIP list
if (!in_array($value, $allowed_zips)) {
$result[‘is_valid’] = false;
$result[‘message’] = ‘Sorry, we do not service your area.’;
}
return $result;
}
(4 = Form ID, 9 = ZIP Code Field ID)
Step 3: Resolve Other Errors
While testing, we encountered unrelated JavaScript issues tied to the ZIP validation logic. Once confirmed, we pushed the code live using the developer snippet without risk. Conditional logic, email addresses, payment checks, WP_redirect, and Google Places API integration were tested to ensure full functionality per form.
Step 4: Make It Global
Later, we upgraded the logic to work across all contact forms by checking dynamically for any field labeled “ZIP Code,” so no need to edit each form individually. Gravity Forms comes with hooks and validation filters allowing per form customization, field IDs, string or digit entries, and array-based ZIP validation.
Results That Matter
- The site now automatically blocks ZIP codes outside the service area.
- Lead quality and team productivity have both improved.
- The setup is easy to update or expand as needed.
Want to Build Smarter Forms?
Need advanced validations, field controls, or multi-form logic? We’ll help you get the most out of Gravity Forms and your WordPress stack.
Leave a Reply