How to Create WooCommerce Checkout Billing And Shipping Fields Validation

How-to-Create-WooCommerce-Checkout-Billing-And-Shipping-Fields-Validation
567 Views

CodexCoach’s This guide will walk you through the process of adding an email verification field to your checkout page. Visitors will be reminded to double-check their entrance. We’ll utilize a special PHP code snippet that I wrote just for this purpose.

Phone number validation Checkout Page

Sorting through fraudulent or incorrect leads may also be time-consuming. To solve this issue, you must force verify the WooCommerce checkout process in order to limit the phone number field to a ten-digit legitimate phone number.

If a consumer enters a phone number with less than ten digits, the checkout process will fail and an error message will appear at the top of the checkout page.

<?php
add_filter( 'woocommerce_checkout_fields', 'cxc_checkout_billing_shipping_phone_no_validation' );
function cxc_checkout_billing_shipping_phone_no_validation( $fields ) {

	// billing phone number
	unset( $fields[ 'billing' ][ 'billing_phone' ][ 'validate' ] );
	// shipping phone number
	unset( $fields[ 'shipping' ][ 'shipping_phone' ][ 'validate' ] );
	
	// uncomment the following lines if you would like to make a phone field optional
	//unset( $fields[ 'billing' ][ 'billing_phone' ][ 'required' ] );
	//unset( $fields[ 'shipping' ][ 'shipping_phone' ][ 'required' ] );
	
	return $fields;
}
?>

ZIP code validation Checkout Page

You can limit the digits in the zip code field to fully validate it. If your country’s zip code has at least four, five, or six digits, you may use the woocommerce checkout process function to add an action code to confirm your customers’ zip code entry. Let us have a look.

<?php
add_filter( 'woocommerce_checkout_fields', 'cxc_checkout_billing_shipping_zip_no_validation' );
function cxc_checkout_billing_shipping_zip_no_validation( $fields ) {
	// billing postcode
	unset( $fields[ 'billing' ][ 'billing_postcode' ][ 'validate' ] );
	// shipping postcode
	unset( $fields[ 'shipping' ][ 'shipping_postcode' ][ 'validate' ] );
	
	return $fields;
}
?>

Email validation Checkout Page

Email validation is a process that determines whether or not an email address is deliverable and legitimate. This technique detects typos, whether they are honest errors or deliberate misdirections. It also certifies the existence of a certain email address.

<?php
add_filter( 'woocommerce_checkout_fields', 'cxc_checkout_billing_shipping_email_no_validation' );
function cxc_checkout_billing_shipping_email_no_validation( $fields ) {

	unset( $fields[ 'billing' ][ 'billing_email' ][ 'validate' ] );
	unset( $fields[ 'shipping' ][ 'shipping_email' ][ 'validate' ] );
	
	return $fields;
}
?>

Create Custom Validation, Checkout Page

The code below is a very simple example of the Billing Last Name field; you may enhance it for any field if you wish.

<?php
add_action( 'woocommerce_after_checkout_validation', 'cxc_checkout_billing_shipping_fname_lname_validation', 10, 2 );
function cxc_checkout_billing_shipping_fname_lname_validation( $fields, $errors ){	
	
	if ( preg_match( '/\\d/', $fields[ 'billing_first_name' ] ) || preg_match( '/\\d/', $fields[ 'billing_last_name' ] ) ){
		$errors->add( 'validation', 'Your first name or last name contains a number. Really?' );
	}
}
?>

JavaScript validation Checkout Page

<?php
add_action( 'wp_footer', 'cxc_checkout_billing_validation_jquery' ); 
function cxc_checkout_billing_validation_jquery(){
	if( ! is_checkout() ) { return; } 
  ?>
	<script type="text/javascript">
		jQuery(function($){
			jQuery( 'body' ).on( 'blur change', '#billing_last_name', function(){
				const wrapper = jQuery(this).closest( '.form-row' );
				// you do not have to removeClass() because Woo do it in checkout.js
				if( /\d/.test( $(this).val() ) ) { // check if contains numbers
					wrapper.addClass( 'woocommerce-invalid' ); // error
				} else {
					wrapper.addClass( 'woocommerce-validated' ); // success		
				}
			});
		});
	</script>
	<?php
}
?>

This article explains how to add a confirm email address box to the WooCommerce checkout page. I’ve included the custom PHP code snippet you’ll need. Simply copy and paste it into the theme’s functions.php file.

Also Read:

How to add a custom field on a product and change the price using a custom field

Get product details from $product object WooCommerce

Only Allow 1 Product in the Cart WooCommerce

Was this article helpful?
YesNo

Leave a comment

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