Form Validation Question Regarding Phone Numbers in JavaScript

100 views Asked by At

I need to validate a form in JavaScript where the phone number that a user provides is in the format of either (###)###-#### or (###) ###-#### with the difference being the space after the ). Why? I have no idea. It makes no sense to me and no phone number should ever look like that in the United States which for my purpose, it will be.

Anyways, the code that I'm currently using looks like this:

pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}"

This will ensure a phone number of the format: ###-###-####.

1

There are 1 answers

0
Mohammad Masoudi On

hey, i think this regex will help you:

/\([0-9]{3}\) ?[0-9]{3}\-[0-9]{4}/

enter image description here