Javascript validating phone number field

Posted by / 12-Oct-2019 17:04

Javascript validating phone number field

Here is the last full working code with only numbers input also keeping in mind backspace button key event for user if something number is incorrect.Google's libphonenumber is very helpful for validation and formatting of phone numbers all over the world.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.Detect arrow key press in Java Script This is really so simple form as this will be used only as an example for learning.In the above code, we have set the id of the form as “my Form” and the id of the phone input field as “phone” which is going to be used in our j Query code.If so, you don't want to prevent those people from entering a perfectly valid [international] number. Remember that client-side validation is only a convenience you provide to the user; you still need to validate all input (again) on the server.

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.

Here is my code and a link to a jsfiddle I've created: Javascript: The first thing one may point out is allowing repetition of "ext", but the purpose of this regex is to prevent users from accidentally entering email ids etc.

Would someone a little smarter than myself be able to help me with this function?

because your regular expression does match the input.

It's just that the input also includes the extra characters.

javascript validating phone number field-22javascript validating phone number field-8javascript validating phone number field-26

By starting your regex with '^' and not ending it with '$', you match lines that start with a sequence matching your regex, but lines can have anything else trailing the matching sequence.

One thought on “javascript validating phone number field”