Help with postcode validation error [RESOLVED]

Hello, Can anyone help please.

I have a booking form where customers order for local delivery. So I need to only allow bookings in my delivery area. So I want the postcode field in the address section to error if a postcode is entered that I don’t deliver to.

I want to be able to deliver to all sectors in certain postcode areas e.g. I deliver to all postcodes in the PO14 postcode area PO14 1AB, PO14 3DE etc etc

We have used the following code copied below but it just errors on all postcodes unless it matches PO14* but we were hoping the * would mean any letter or number.

I’d appreciate help from anyone who understands the code and can see the problem.

thank you.

add_filter( 'gform_field_validation_10_25', 'custom_zip_validation', 10, 4 );
function custom_zip_validation( $result, $value, $form, $field ) {
    if ( $result['is_valid'] ) {
        $acceptable_zips = array(
            'PO1*',
            'PO2*',
			'PO3*',
			'PO4*',
			'PO5*',
            'PO6*',
			'PO7*',
			'PO8*',
			'PO12*',
            'PO13*',
			'PO14*',
			'PO15*',
			'PO16*',
            'PO17*',
			'SO30*',
			'SO31*',
			'SO32*'
        );
 
        $zip_value     = rgar( $value, $field->id . '.5' );
 
        if ( ! in_array( $zip_value, $acceptable_zips ) ) {
            $result['is_valid'] = false;
            $result['message']  = 'Sorry your address is outside our service area. Please contact us to discuss.';
        }
    }
 
    return $result;
}

The asterisk won’t work to match “anything” :slight_smile:

Try this code like this:

<?php
// apply to form 10 field 25
add_filter( 'gform_field_validation_10_25', 'custom_zip_validation', 10, 4 );
function str_begins( $haystack, $needle ) {
	return 0 === substr_compare( $haystack, $needle, 0, strlen( $needle ), TRUE );
}
function custom_zip_validation( $result, $value, $form, $field ) {
	if ( $result['is_valid'] ) {
		$acceptable_zips = array(
			'PO1',
			'PO2',
			'PO3',
			'PO4',
			'PO5',
			'PO6',
			'PO7',
			'PO8',
			'PO12',
			'PO13',
			'PO14',
			'PO15',
			'PO16',
			'PO17',
			'SO30',
			'SO31',
			'SO32'
		);
		
		$zip_value = rgar( $value, $field->id . '.5' );
		foreach( $acceptable_zips as $zip ) {
			// if you find the zip, return is_valid
			if ( str_begins( $zip_value, $zip ) ) {
				return $result;
			}
			else {
				$result['is_valid'] = false;
				$result['message']  = 'Sorry your address is outside our service area. Please contact us to discuss.';
			}
		}
	}
	return $result;
}

I did a quick test of that and it seemed to work, but I did not try all possible variants of good or bad postcodes.

Hi Chris,

Thank you for this. It works perfectly, I owe you a drink.

FYI, I had asked someone on Fiverr to do this for me, I used the * to represent ‘anything’ in the instructions and it was this person who wrote the code said it couldn’t be done when it didn’t work with the * after the postcode. I guess a communication issue more than anything.

Many thanks again

James

I’m happy that worked. It worked in my tests but I was not sure it covered all cases.

Cheers!

cheers

Hi Chris,

I may have been a bit premature on saying it works ‘perfectly’ although in theory it does. Basically it works for the PO postcodes but it errors with the SO postcodes. Strange!

I’ve posted another question on this to see if it’s an obvious error we’re missing. I’ll copy the link in case you have a spare moment to take a look. I don’t really get why it errors on the SO, it’s like they’re even not in the code.

Many Thanks

James