0

OK,

I have the following regulare expression for phone number validation, but it doesn't really do EXACTLY what I want...

What I want to see is a forced format 000-000-0000 where it needs to be entered with the area code-prefix-suffix including the dashes.

I store it in a varchar field in mysql.

What I realized today was that someone could enter 12 numbers and it would be accepted.

Any direction / tweaks on this code that would make it do what I want would be greatly appreciated.

Thanks
Douglas

    if (empty($_POST["cphone"])) {
        $phoneErr = "Phone Number is a Requested Entry.";
    }elseif(($_POST["cphone"] != preg_replace('~.*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4}).*~', '$1-$2-$3', $_POST["cphone"])) || (strlen($_POST["cphone"]) != 12 )){
        $phoneErr = "Please use this format: 000-000-0000.";
    }else{
        $cphone = $_POST["cphone"];
    }
2
Contributors
1
Reply
17
Views
3 Years
Discussion Span
Last Post by AndrisP
0
preg_match('~^(\d{3}\-){2}\d{4}$~', '000-000-0000')

and don't need check strlen

Edited by AndrisP

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.