Check zip code if certain country is selected

Hi. I’m trying to add a certain check to gravity forms, it would be something like this:

If user selects “United Kingdom”, then zip code would be checked if it’s correct. I got the zip code part working, however I’m not sure how to make it so zip code check would only run if “United Kingdom” is selected. This is what I got so far:


add_filter( 'gform_field_validation_9_51', 'custom_zip_validation', 10, 4 );
function custom_zip_validation( $result, $value, $form, $field ) {
	$country = rgar( $value, $field->id . '.6' );
	if ($country == "United Kingdom") {
	if ( $result['is_valid'] ) {
        $acceptable_zips = array(
            'AL', 'B', 'BD', 'BH', 'BN', 'BR', 'CB', 'CH', 'CM', 'CO', 'CR', 'CV', 'DA', 'DE', 'DN', 'DY', 'E', 'EC', 'EN', 'GU', 'HA', 'IG', 'IP', 'L', 'LE', 'LN', 'LS', 'LU', 'M', 'ME', 'MK', 'N', 'NG', 'NN', 'NR', 'NW', 'PE', 'PO', 'RG', 'RM', 'S', 'SE', 'SG', 'SK', 'SL', 'SM', 'SO', 'SS', 'ST', 'SW', 'TN', 'TW', 'UB', 'W', 'WD', 'WF', 'WR', 'WS', 'WV'
        );
 
        $zip_value = rgar( $value, $field->id . '.5' );
		$zip_value = preg_replace('/[0-9]+/', '', $zip_value);
 
        if ( ! in_array( $zip_value, $acceptable_zips ) ) {
            $result['is_valid'] = false;
            $result['message']  = 'Sorry, we don't deliver to this region.';
        }
    }

	}
 
    return $result;
}

Perhaps someone can help me with this? Thanks.

If it’s a fatal error you’re encountering, note that on this line, you have an unescaped single quote.

1 Like

No, this isn’t it. I just changed it last minute before pasting here.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.