Can anyone refer me to a JavaScript phone number validation script that will actually check that a real phone number is being input as it's typed in?
I can only find ones that validate after the form is submitted and that's not what I need.
Printable View
Can anyone refer me to a JavaScript phone number validation script that will actually check that a real phone number is being input as it's typed in?
I can only find ones that validate after the form is submitted and that's not what I need.
I cannot remember which is better, but you want to bind your input field to a keydown or keyup event. jQuery works well here and following page has an example utilizing the event:
http://api.jquery.com/keydown/
You can highlight the box red or something if the validation fails. All you need is a regex to validate the phone number against but I'm getting that you already have that based on your post. But, to be thorough, regex examples for phone number:
http://stackoverflow.com/questions/1...ber-validation
And a page to text your regex:
http://www.regextester.com/
For phone number the first test to realize, is to count the numbers ! This count could depends of an international prefix. But depends at first of countries (See this page).
For the first test you can remove all non numbers characters with something like this
Code:var phoneNumber=document.getElementById('phoneInput').value;
var numberLength=phoneNumber.replace(/\D+/g,'').length;
if (numberLength!=N || numberLength!=IN) {alert('Your phone number seems erroneus ');return}