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"];
    }
preg_match('~^(\d{3}\-){2}\d{4}$~', '000-000-0000')

and don't need check strlen

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.