This article requires the GP Populate Anything perk. Buy Gravity Perks today to get this perk plus 40 other premium Gravity Forms plugins!
If you own a business that has a limited service area, it’s important to limit online orders to that service area. Otherwise, you have to check the delivery address for every order and will likely have to cancel orders that are placed outside of that service area. This leads to more work for you and customer disappointment.
Save yourself the potential headaches with a simple Gravity Forms zip code validation process — allowing you to check a customer’s zip code directly on the order form and prevent form submissions if they fall outside your service area.
In this tutorial, we’re going to show you how to use Gravity Forms to create a simple database of zip codes and validate against that database and populate data from that database into your forms.
Getting Started
In order to have a list of zip codes to validate against, we first need to store them somewhere in the WordPress database. We’ll then use Populate Anything to retrieve that list of zip codes and validate against it. While there are lots of ways to create a database in WordPress, our preferred method is to use Gravity Forms, of course! 🧙♂️
Gravity Forms Zip Code Validation Steps
- Create Input Form and Submit Entries
- Add Zip Code Fields to Customer Form
- Add Validation Messages
- Set up Conditional Logic on Submit Button
Step 1 – Create Input Form and Submit Entries
We’re going to create a form specifically to submit entries with zip codes. You might have seen a similar method before in our Using Gravity Forms as a Simple Database article.
Start by adding a Single Line Text field to your input form. Name it “Zip Code” or “Postal Code,” and start submitting entries. Enter a single zip code per entry.
Once you have entered all of the valid zip codes, you are done with the input form. If you decide in the future to adjust your service area, you can add new entries or delete old ones. Here’s an example of our service area.
Step 2 – Add Zip Code Fields to Customer Form
With the database done, it’s time to add the zip code validation fields to our customer-facing form. Start by adding two Single Line Text fields to your form. Call the first one “Zip Code” and the second “Is Zip Code Valid?”
The “Is Zip Code Valid?” field is going to use Populate Anything to check if the entered zip code is stored in the database from the input form we built in Step 1. To do this, first, check Populate value dynamically and set the population type to “Gravity Forms Entry.” Then, select your input form.
Next, we’ll need to set the Filters so that the “Zip Code” field value entered in this form matches the zip codes stored in the database.
Instead of selecting a field value in the Value Templates, we’re going to insert the {count}
merge tag. This will count the number of values that match our filtering, which we will use for Conditional Logic. To do this, first select Custom Value from the drop down, then insert the merge tag.
Lastly, set this field’s Visibility to Hidden. Your customers don’t need to see this field on the form.
Step 3 – Add Validation Messages
With the population sorted, the next step is to add validation messages. We’re going to add two for our form, one for success and one for failure. I like to end on a high note, so we’ll start with the failure message. 😁
Zip Code Validation Failure Message
Add an HTML field to your form and insert your message for when the user’s zip code falls outside your delivery area. I like to style mine a bit, so there’s some additional CSS included.
Enable Gravity Forms Conditional Logic on this field and use the following settings to only show it when the zip code doesn’t exist in our database.
The conditional logic we’re using checks to see if the user has submitted a zip code into the “Zip Code” field and then checks the “Is Zip Code Valid?” field for any value outside of 0
. Since we’re using the {count}
merge tag in the Value Template, it will return 0
if the zip code doesn’t exist in the database, and 1
of it does.
Zip Code Validation Success Message
The success message is set up similarly to the failure message, but with slightly different conditional logic. Below is my success message HTML.
And here you can see the conditional logic settings.
As you can see, the “Zip Code” field logic is identical, but this time we’re checking the “Is Zip Code Valid” field for values greater than 0
. {count}
will return 1 when the zip code exists in the database, so using greater than has the advantage of still triggering the conditional logic even if you accidentally enter the zip code more than once when building your database.
Step 3 – Set up Conditional Logic on Submit Button
The last step is to set up conditional logic on the Submit button. This will hide the Submit button when the entered zip code isn’t valid, preventing the customer from placing an order that we can’t fulfill.
The conditional logic rules match our success validation message exactly. We’ll show the button when there is an entered zip code and that zip code is found in the database.
Ready For Orders
With everything in place, our form is ready to take orders. When someone enters their zip code, the database checks to see if their zip code is within our delivery area. If it is, we show them a message and display the submit button; if not, they see a failure message and the submit button is hidden.
An Alternate Approach
The Populate Anything approach is perfect when you have many zip codes, but if your service area only includes a handful of postal codes, you may find our “is In” conditional logic operator to be an easier option. We wrote a snippet that adds the operator to conditional logic for any field that supports it.
Once installed, using the operator is easy. In the success message conditional logic, select the “is in” operator and insert your zip codes as a comma-delimited list.
For the failure message, you’ll need to add multiple conditions to show the message if the zip code doesn’t match.
You’re good to go. Have questions? Reply in the comments or shoot us a message!
Did this resource help you do something awesome with Gravity Forms? Then you'll absolutely love Gravity Perks; a suite of 39+ essential add-ons for Gravity Forms with support you can count on.
Excellent tutorial Scott! This give me a lot of idea on how to implement validation on all sorts of fields. Thanks!
Hi Veronique,
You’re welcome.
Cheers,