Home > Software design >  Woocommerce phone number field validation not working if the field contains only one "0" c
Woocommerce phone number field validation not working if the field contains only one "0" c

Time:03-21

I wrote a regular expression that validates the phone number in woocommerce. It works in all cases, unless the field contains a single "0" character. What could be the error?

That's the code:

$post_value = $_POST['billing_phone'];   
    if ( $post_value && ! preg_match( '/^(\ 49)[0-9]{9,}$/', $post_value ) ) {      
        //error                                         
    }   

CodePudding user response:

Your regex does not match 0 or 049. So the problem is probably that something in that if statement is different when the POST param 'billing_phone' is 0. In PHP if (int) is true for any int other than 0 and false for 0, perhaps that is the problem?

So you could solve that by something like this:

if ( ($post_value || $post_value == 0) && ! preg_match( '/^(\ 49)[0-9]{9,}$/', $post_value ) ) {

(Disclaimer: I am generally not very familiar with PHP.)

  • Related