I found the code snippet below to check the phone number from a blog on the internet and it works quite nicely.
- Thanks to the code, we limit the phone number to a maximum and minimum of 11 digits. (We cannot place an order if the number is incomplete.)
- We require the phone number to start with “05”.
- We are able to show a guiding error message in case of any problem.
While the code works great on the checkout page in every way, it doesn’t work on the my account page.
How can I get this code to work on my account page as well?
Waiting for your help, I wish you all a good day.
// Limit Woocommerce phone field to 11 digits number
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('/^05[0-9]{9}$/', $_POST['billing_phone'] ))){
wc_add_notice( "Wrong Phone Number Format" ,'error' );
}
}