www.webdeveloper.com
Results 1 to 3 of 3

Thread: JavaScript Phone Validation - As it's typed in

  1. #1
    Join Date
    Apr 2009
    Posts
    346

    JavaScript Phone Validation - As it's typed in

    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.

  2. #2
    Join Date
    Jan 2013
    Posts
    4
    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/

  3. #3
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,272
    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}

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles