Gravity Forms Zip Code Validation [RESOLVED]

Hello,

I’m using the gform_field_validation to verify the applicant is in the correct zip code before moving on. I enter a valid zip code but I still get the invalid message.

Form can be seen here.

This is my code

//GRAVITY FORMS ZIP CODE AUTHENTICATION

add_filter( 'gform_field_validation_15_193', 'custom_zip_validation', 10, 4 );
function custom_zip_validation( $result, $value, $form, $field ) {
    if ( $result['is_valid'] ) {
        $acceptable_zips = array(
        '91901',
		'91976',
		'92040',
		'92085',
		'92123',
		'92160',
		'91902',
		'91977',
		'83687',
		'83709',
		'83720',
		'83756',
		'83631',
		'83652',
		'83701',
		'83711',
		'83722',
		'83799',
		'83301',
		'83302',
		'83303',
		'83647'
        );
 
 
        if ( ! in_array( $zip_value, $acceptable_zips ) ) {
            $result['is_valid'] = false;
            $result['message']  = 'Zip validation failed. Unfortunately, we are not able to assist you. We currently serve the areas within an hour San Diego County, Santa Ana, California; and Boise, Idaho. Please visit the Assistance Dogs International (ADI) website for more information on service dog training. <a href="https://www.assistancedogsinternational.org/" target="_blank">https://www.assistancedogsinternational.org/</a>
';
        }
    }
 
    return $result;
}
1 Like

You’re not assigning the information submitted in field 193 (the ZIP code) to $zip_value anywhere. So you are trying to see if $zip_value is in your $acceptable_zips array, but $zip_value would be undefined at that point. With WP_DEBUG or PHP warnings enabled you would see this notice.

You need something like this around line 31 (after $acceptable_zips is defined):

$zip_value = rgar( $value, '193' );

Ah… I had removed that line from the original code because I know enough about php coding to get me into trouble. :-). I will add it back with the correct value. Thank you so much!!!

I added the line of code but it didn’t fix the issue. However, I did fix this by changing the field type from single line of text to address and showing only the zip code and then adding this line of code

	 $zip_value = rgar( $value, $field->id . '.5' );        

This solved the issue and it’s working now.

1 Like