Help with gform_field_validation - Postcode field problem

Hello!

Something is baffling us that someone maybe able to help with…

Copied below is the code we are using to validate the postcode field so it only accepts orders from customers in our delivery area. We operate in some PO postcode areas and some SO postcode areas. The problem that’s baffling us is it works perfectly for the PO postcodes but errors for the SO postcodes as if they were not in the code.

You can see from the code we use it in 2 fields in the same form. 1 for delivery address and 1 for collection address both have the same issue.

Would appreciate any help if you can spot what the problem is we’re missing.

Our URL where this form is on: www.boxmystuff.co.uk/form

CODE:

// 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 <a href="https://boxmystuff.co.uk/contact/">contact us</a> to discuss.';
		}
	}
}
return $result;

}
// apply to form 10 field 32
add_filter( ‘gform_field_validation_10_32’, ‘custom_zip_validationw’, 10, 4 );

function custom_zip_validationw( $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 <a href="https://boxmystuff.co.uk/contact/">contact us</a> to discuss.';
		}
	}
}
return $result;

}