Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Apr 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb Is dynamic validation for telephine number possible ?

    Hi!

    I wish to know if one can dynamically validate a textbox that accepts telephone numbers.

    I plan to do this by:
    first selecting a country form a dropdown box.
    then validate the text box for the corresponding country selected...

    but do not have any idea how to do it!


    can you help me ?

  • #2
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You have set yourself a pretty big task if you have no idea how to start. Maybe read some form validation tutorials/strategies first like this one

    http://www.xs4all.nl/~sbpoley/webmatters/formval.js

    david_kw

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    As with most other validations, over-elaboration of telephone number validation can be counter-productive.

    The main risk is that the user simply enters the wrong number (transposed digits etc.) and nothing can prevent this.

    I would advise that telephone number validation is simply a check that the input box contains only numbers (without specifying how many or their precise format which varies from country to country), and also + (international prefix), - (hyphens) and/or spaces.

    - and space can be stripped out if necessary.

    The regex is:-

    if (/[^0-9\s\-\+]/gi.test(phoneNumber.value) || (phoneNumber.value.length <9 )) { /// only digits, space, hyphen and plus sign allowed. Minimum of 9 characters.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    The regular expression I usually use for phone numbers is:

    Code:
    /^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,3})|(\(?\d{2,3}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/
    This should allow for most local and international formats that don't involve using letters to represent the numbers.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Stephen -

    That rejects my number which is something like 01483 674960

    01483 is the exchange, 674960 is the 6-digit subsciber number.

    Also rejects ++44 (international dialing code). In UK the ++ is replaced by 00.

    I prefer my simpler check!

  • #6
    New Coder
    Join Date
    Apr 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank a lot Philip M for your valuable suggestion.
    I too was thinking on those lines.

  • #7
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    The normal international convention is a single + to represent the international code otherwise I'd have set it up to accept ++++ since the international code here is four digits long.

    It would also be more normal to break a number up more with spaces to make it easier to read. three or four digits at a time is about all anyone would normally say without a pause. I had forgotten that there were still places using hundreds of area codes for the same town but you could fix that simply by allowing for more digits in each group.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Most UK numbers are a five- (or possibly four-) digit area code (starting with 0) followed by a six-digit subscriber number. 0xxxx xxxxxx

    But big towns and cities including London take the form 020 xxxx xxxx with either a space or a hyphen between the groups.

    There are not "hundreds of area codes for each town" but a single area code may encompass several medium sized towns. For example, 01483 is Guildford but extends to places such as Cranleigh, Woking and so on, say lesser towns within 15 miles radius.

  • #9
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    7
    Thanks
    0
    Thanked 1 Time in 1 Post
    This other thread now contains a more detailed description for the entire UK number range: http://www.codingforums.com/showthread.php?t=8307

    Hope it is useful!


  •  

    Posting Permissions

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