Validate ZIP Code against a list of codes

I know this post is old but I was working on the same thing and figured what I used would be useful for someone else. I came up with a method to check a txt file. The zips in the text file are separated by one zip per line (no commas).

/**
 * gform zip code validator.
 */
function gform_zip_checker($result, $value, $form, $field) {
	$file = get_stylesheet_directory_uri().'/zips.txt'; // I have this in my child theme

	if ( $result['is_valid'] && (!preg_match('/^' . $value . '$/m', file_get_contents($file))) ) {
		$result['is_valid'] = false;
		$result['message'] = 'We don\'t provide service to this area. If you believe this is incorrect please call us.';
	}
	return $result;
}
add_filter('gform_field_validation_2_8', 'gform_zip_checker', 10, 4); // gform_field_validation_FORMID_FIELDID
2 Likes