Zip code validation against custom post type + custom field [RESOLVED]

Hello, I am wanting to create a zip code validation within a form for a subset of zips. I see how I can do this with an array but I would like to have an office manager add and remove zips as the business needs it so I want to create a way for the user to manage zips from within the WP dashboard.

My thought is to create a custom post type and a custom field. Custom Post Type will be active zips and include a title which will be the city and a custom field, zip code.

Is there a way for me to validate that the zip code entered by a user on a form is within the current active zip codes from that custom post type?

Sure, the same way that you do the custom validation with the array using the gform_field_validation filter. But in this case you need to create the code to query your custom post first and parse the data to use it for the custom validation.

2 Likes

Any chance you can help with that :)? It’s a little outside my know how. I’ve got the following so far which I put together from a few other posts:

//Custom post type to query is delivery_area
//Custom field to query zip_code
//I've currently assigned a custom field group with zip_code to the custom post type delivery_area

//ZIP CODE Validation for vessel creation form
add_filter( 'gform_field_validation_7_11', 'zip_validation_1', 10, 4 );
function custom_zip_validation( $result, $value, $form, $field ) {
    if ( $result['is_valid'] ) {
        $acceptable_zips = array(
            

);
 
        $zip_value = rgar( $value, $field->id . '.5' );
 
        if ( ! in_array( $zip_value, $acceptable_zips ) ) {
            $result['is_valid'] = false;
            $result['message']  = 'Your location is not within our delivery area.';
        }
    }
 
    return $result;
}

@sacom Are you able to point me toward a resource on this? I have not been able to figure it out. I have been referencing this tutorial without much progress. https://www.advancedcustomfields.com/resources/query-posts-custom-fields/

Hi Brian, how about live validation that shows an error message via an HTML field and hides the submit button if the entered zip isn’t valid?

Here’s a full walkthrough that demonstrates how to accomplish this with Gravity Forms and Populate Anything:

1 Like

Nice solution!

But still probably good to remember this is client side only, users can (not very easible though, and can’t fully test it now) still change values in browser inspector tools to still be able to submit the form.

David, I think this will do the trick. I am going to implement it today and will confirm success. Very much appreciated!

@david The validation is perfect for my needs. Thank you! I had recently picked up gravity perks also so it was great to use it for this purpose.

1 Like
© 2008 - 2019. Gravity Forms is a project by Rocketgenius Inc.