Phone Number Validation

Hi everyone!

Does anyone have an idea how to put a phone validation on this website? on the homepage there is a “Get a Free Qoute” Contact form and I want to have the visitor put in a valid US phone number like this “(111)-111-1111”? I’m trying to do jQuery function for this. How can I prevent the form from submitting if the phone number is incorrect? Thanks in advance.

Would a regex like this help?

Bear in mind that front-end scripting cannot be relied upon entirely for validation, it will only help honest users from making errors. It can be bypassed or fail if js is not working for whatever reason.
Also in modern browsers, the pattern attribute can do all that bit for you anyway.
For secure validation, it has to be done server-side as part of the form processing.

it shouldn’t even be used any more at all now that you can apply the validation using HTML.

To validate a phone number as much as is appropriate in the browser you just need type=“tel” instead of type=“text” in the input tag. The rst of the validation can be done on the server (where it needs to be done anyway).

If you absolutely insist on rejecting phone numbers from most of the world and only want local numbers able to be entered then use a pattern attribute in the HTML (still no JavaScript required).

The HTML form validations are now at least as likely to run as any you try to apply with JavaScript.


The form was actually made from contact form 7. I’ve been searching how to add a filter and found some forums but the their code for validating phone number doesn’t work anymore. Probably because it was an old post.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.