Phone validation script

Phone validation script

Hello,

I am using the following validation script to change some phone numbers that are being sent to one of my forms.

if (!input.Phone_Number.matches("[0-9]{10}"))
{
    alert "Enter the numbers only (Example : 1234567890)";
    cancel submit;
}
else
{
    input.Phone_Number = (((("(" + input.Phone_Number.subString(0,3)) + ") ") + input.Phone_Number.subString(3,6)) + "-") + input.Phone_Number.subString(6);
}

While it works when the phone number comes in as all numbers (i.e., 5557771212), I also need it to do the following:

1. ALLOW numbers entered in the following format to pass through to unchanged:

(555) 777-1212

Currently, the validation script rejects any numbers coming in that are formatted in this way.

2. CONVERT numbers submitted in these formats:

(555)777-1212
555-777-1212

It should transform numbers submitted in this format to what the script currently calls for.

Appreciate any help.