Checkout Field ZIP code validation

add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');

function my_custom_checkout_field_process() {
	global $woocommerce;

	// Check if set, if its not set add an error. This one is only requite for companies
	if ( ! (preg_match('/^[0-9]{4}$/D', $_POST['billing_postcode'] ))){
		wc_add_notice( "The Postcode should contain only 4 digits"  ,'error' );
	}
	
	if( ! (preg_match("/^[a-zA-Z]+$/", $_POST['billing_first_name']))){
		wc_add_notice( "The Billing First Name should contain only alphanumeric characters [a-z] and [A-Z]"  ,'error' );
	}
	
	if( ! (preg_match("/^[a-zA-Z]+$/", $_POST['billing_last_name']))){
		wc_add_notice( "The Billing Last Name should contain only alphanumeric characters [a-z] and [A-Z]"  ,'error' );
	}
	
	if( ! (preg_match("/^[a-zA-Z]+$/", $_POST['billing_city']))){
		wc_add_notice( "The City should contain only alphanumeric characters [a-z] and [A-Z]"  ,'error' );
	}
	
}

Leave a Comment