Validate Postcode

Experimental Snippet 🧪

Check to confirm if the postcode entered into the Address field is a valid postcode. Currently limited to UK postcodes. If you need support for another country, let us know via support.

Code

Filename: gw-validate-postcode.php

<?php
/**
 * Gravity Wiz // Gravity Forms // Validate Postcode
 * https://gravitywiz.com/
 *
 * Experimental Snippet 🧪
 *
 * Check to confirm if the postcode entered into the Address field is a valid postcode.
 * Currently limited to UK postcodes. If you need support for another country, let us
 * know [via support](https://gravitywiz.com/support/).
 */
// Update "123" to the Form ID and "4" to the Address field ID.
add_filter( 'gform_field_validation_123_4', 'gw_validate_postcode', 10, 5 );
function gw_validate_postcode( $result, $value, $form, $field ) {
	$country_to_check = 'United Kingdom';
	$regex_pattern    = '/^([A-Z]{1,2}\d[A-Z\d]? ?\d[A-Z]{2}|GIR ?0A{2})$/';
	$country          = rgar( $value, $field->id . '.6' );
	if ( $country == $country_to_check ) {
		if ( $result['is_valid'] ) {
			$zip_value = rgar( $value, $field->id . '.5' );
			if ( ! preg_match( $regex_pattern, strtoupper( $zip_value ) ) ) {
				$result['is_valid'] = false;
				$result['message']  = 'Please enter a valid UK postcode.';
			}
		}
	}
	return $result;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.