Ecommerce Shopify WordPress Discussion

Validate phone number length from starting digits in WooCommerce checkout page

I would like to add custom validation to WooCommerce checkout page phone number: If the phone number starts with "02" the length should be 9 digits max If the phone number starts with "08" the length should be 10 digits max Here is the code what I have so far: add_action('woocommerce_checkout_process', 'njengah_custom_checkout_field_process'); function njengah_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]{10}$/D', $_POST['billing_phone'] ))){ wc_add_notice( "Incorrect Phone Number! Please enter valid 10 digits phone number" ,'error' ); } }
With the following code, an error message will be thrown avoiding checkout if the inputted phone number: starts with "02" and if the string length has not 9 digits, starts with "08" and if the string length has not 10 digits, does not start with "02" or "08", is not totally numerical. The code: add_action('woocommerce_checkout_process', 'custom_checkout_billing_phone_validation'); function custom_checkout_billing_phone_validation() { if ( isset($_POST['billing_phone']) && ! empty($_POST['billing_phone']) ) { $phone = str_replace(' ', '', $_POST['billing_phone']); $length = strlen($phone); // Number starting with "02" with more digits than 9 if ( strpos($phone,'02') === 0 && is_numeric($phone) && $length == 9 ) { wc_add_notice( __('Incorrect Phone Number! Please enter valid 9 digits phone number', 'woocommerce'), 'error' ); } // Number starting with "08" with more digits than 10 elseif ( strpos($phone,'08') === 0 && is_numeric($phone) && $length == 10 ) { wc_add_notice( __('Incorrect Phone Number! Please enter valid 10 digits phone number', 'woocommerce'), 'error' ); } // Optional: Number not starting with "02" or "08" elseif ( ! ( strpos($phone,'02') === 0 || strpos($phone,'08') === 0 ) ) { wc_add_notice( __('Incorrect Phone Number! Phone number must start with "02" or "08"', 'woocommerce'), 'error' ); } // Optional: Not numerical input elseif ( ! is_numeric($phone) ) { wc_add_notice( __('Incorrect Phone Number! Phone number only accept numbers', 'woocommerce'), 'error' ); } } } Code goes in functions.php file of your child theme (or in a plugin). Tested and works.

January 10, 2024

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.