Need Help Creating a Form to Validate Zip Code

Hello Everyone!

I’m trying to optimize a form on a clients’ website. I would like the form to ask for a ZIP Code, and then validate it to see if it falls within the service area. If it does then allow customers to proceed with filling out a form to request the service.

If the ZIP Code does not fall within the service area, then display a message, and potentially collect an email.

Does anyone have an idea how to get this done?

All help is appreciated!

I think I saw some of this discussion on the Facebook group. How many ZIP codes are there, or, is there a definite range of codes? If it’s a simple range, or just a few distinct codes, it can be done with conditional logic. If there are lots of individual codes, and no real contiguous ranges, then you would be better off using the gform_field_validation filter.

It will be a specific range, but it will vary due to the counties the client provides services in.

Example:

County 1:
10552 - 10575

County 2:
10811 - 10895

I need to set up about 4 counties, a total of about 60 zip codes, so what I was thinking was having a field where users can input the zip code. Then create a list that will contain all the zip codes for every county the client serves. If the zip code matches a number on the list, then let them complete the order.

With multiple contiguous ranges, you won’t be able to use the built in conditional logic. Those rules allow Greater Than and Less Than, to create a range, but the combined rule would have to be “All” (as in it matches all those rules, which something in the range would do.) That would only work for one range though.

I would recommend using your list and the gform_field_validation filter:

In example one you would replace this line:

if ( $result['is_valid'] && intval( $value ) > 10 ) {

With something to check your array of values for the submitted value. It would be a small amount of PHP to accomplish. I wrote something like this a while back:

You can use that approach to get you started if you need a hint.