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 5 of 5
  1. #1
    Regular Coder
    Join Date
    Feb 2006
    Posts
    112
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Form phone number validation

    Hi all,
    I need to get a form validation script working. What I would like is for the script to ensure that the phone number entered is either seven or ten digits. If it is seven digits, a default area code will automatically be added to the front of the number.

    Any ideas on how to do this. I've hit a mental brick wall. The rest of the script is just checking that a valid email address has been entered

    Thanks for any help.

    Best,
    C

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,518
    Thanks
    3
    Thanked 506 Times in 493 Posts
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <script language="JavaScript" type="text/javascript">
    /*<![CDATA[*/
    
    function zxcVerify(zxcfrm){
     var zxcels=zxcfrm.elements;
     var zxcmess=['Errors:'];
     for (var zxc0=0;zxc0<zxcels.length;zxc0++){
      if (zxcels[zxc0].className.match('Phone')){
       zxcels[zxc0].value=zxcels[zxc0].value.replace(/\D/g,'');
       var zxcdata=zxcels[zxc0].className.split('#');
       if (zxcels[zxc0].value.length<zxcdata[1]||zxcels[zxc0].value.length>zxcdata[2]){
        zxcmess.push((zxcels[zxc0].title||zxcels[zxc0].name||'')+' '+zxcdata[1]+' to '+zxcdata[2]+' Digits Required');
       }
       else if (zxcels[zxc0].value.length==zxcdata[1]){
        zxcels[zxc0].value=zxcdata[3]+zxcels[zxc0].value;
       }
      }
     }
     if (zxcmess.length>1){
      alert(zxcmess.join('\n'));
      return false
     }
     return true;
    
    
    }
    /*]]>*/
    </script></head>
    
    <body>
    <form onsubmit="return zxcVerify(this);" action="http://www.vicsjavascripts.org.uk" >
    <input title="Phone Number" class="RealClass Phone#7#10#023#" type="text"  value="12as3"/>
    <input type="submit" name="" value="Submit" />
    </form>
    </body>
    
    </html>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Not all phone numbers are that length. Phone numbers around here are all 8 digits long and elsewhere in the world are all different lengths. It is the WORLD WIDE web and unless your pages relate specifically to one place and use an appropriate domain name to indicate that the site is specific to that country then you will have visitors from all over the world complaining that your validation is rejecting their valid input.
    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.

  • #4
    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
    I agree with felgall, not all phone numbers are 10 digits.

    But here is what you want:-

    if (/(^[0-9]{7})$/gi.test(phoneNumber.value)) {
    phoneNumber.value = "012" + phoneNumber.value // add prefix
    return true;
    }
    if (/(^[0-9]{10})$/gi.test(phoneNumber.value)) {
    return true;
    }
    else {return false}

  • #5
    Regular Coder
    Join Date
    Feb 2006
    Posts
    112
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I haven't had access to the computer for a couple of days so it's a belated...Thanks! Very much appreciated.


  •  

    Posting Permissions

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