Javascript validating phone number field

Rated 4.30/5 based on 839 customer reviews

I can only accept 7 or 10 digit phone numbers, but characters such as (,), (-), etc are acceptable.If this box is empty or the phone number isn't in the correct format (not 7 or 10 numbers long, not a number) or has been left blank, I am supposed to add a red border around the text box.Its purpose is to validate a text input in a form, a phone number field that will only accept 0-9, dash and dot. function is Mobile Number(evt, sender) { // Allows only 10 numbers // ONKEYPRESS FUNCTION //evt = (evt) ? Now below is our j Query code: On submitting the form, the j Query first checks if the phone input field has the value with number and length is between 10. Live word counter from HTML textarea in Java Script Now let’s test it on your browser.The Java Script is Na N method is used to check if the input data contains the only number. You will see if any of the characters in the phone field is not a number or is less than 10 in length or greater than 10 in length, the form will not be submitted.

Will your application be used by someone with a phone number from outside North America? Finally, I get the feeling you're validating user input in a web browser.

You included '^' to signify the beginning of line, but (as Andy said) you should include '$' to signify the end of line.

If you start your regex with '^' and end it with '$', then it will only match lines that only match your regex.

I found this code in some website, and it works perfectly.

It validates that the phone number is in one of these formats: (123) 456-7890 or 123-456-7890 The problem is that my client (I don't know why, maybe client stuffs) wants to add another format, the ten numbers consecutively, something like this: 1234567890.

Leave a Reply