SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: "+" sign in phone number Javascript

  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2010
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    "+" sign in phone number Javascript

    Hi there, I would like to allow the "+" sign in my 3 textboxes. When the user does not put the "+" sign in the textboxes, and it is being validated, the system will allow it to pass.

    Also, it must check that it has at least 8 digits.

    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" >
    <head>
        <title>Untitled Page</title>
        <link rel="stylesheet" type="text/css" href="CSS files/specialAlignment.css" />
        <link rel="stylesheet" type="text/css" href="CSS files/externalCSS.css"/>    
        <script language = "javascript" type = "text/javascript">
        
    function check() 
    {
        var fields = new Array("Office Telephone", "Mobile Telephone","Fax Number");
        var telnr = /(^[\+]?[\d]{8,20}$)/;
    
        var index = new Array();  
        for(var i = 0; i < fields.length; i++)  
        {  
            var arrayFields = document.getElementsByName(fields[i]);  
            for(var j = 0; j < arrayFields.length; j++)  
            if(!(arrayFields[j].value) == "")  
            {   
                arrayFields[j].className = "defaultColor";              
            }  
            
            else  
            {  
                arrayFields[j].className ="changeToRed"; 
                index.push(fields[i]);                  
            }        
        }  
        
        if(index != 0)  
        {  
            joinComma = index.join(', ');  
            alert('The field(s) corresponding to '+ joinComma + ' is/are not filled in.');   
        }  
             
    }  
    
    function noSpace(e, dec)
    {
        var key;
        var keychar;
    
        if (window.event) 
        {
           key = window.event.keyCode;
        }
        else if (e) 
        {
           key = e.which;
        }
        else 
        {
           return true;
        }
        keychar = String.fromCharCode(key);
    
        if ((key==null) || (key==0) || (key==8) ||  (key==9) || (key==13) || (key==27) /*|| (key==107)*/) 
        {
           return true;
        }
        else if ((("0123456789").indexOf(keychar) > -1)&& check(telnr.test(tfld))) 
        {
           return true;
        }
        else if (dec && (keychar == ".")) 
        { 
          return true;
        }
        else
           return false;
            
    } 
        </script>
    </head>
    <body>
                            <label>
                                *Office Telephone:</label>
                            <input id="Text16" type="text" name="Office Telephone" onkeydown="return noSpace(event)" />
                            <br />
                            <br />
                            <label>
                                *Mobile Telephone:</label>
                            <input id="Text17" type="text" name="Mobile Telephone" onkeydown="return noSpace(event)" /><br />
                            <br />
                            <label>
                                *Fax Number:</label>
                            <input id="Text18" type="text" name="Fax Number" onkeydown="return noSpace(event)" />
                            <br />
                            <br />
                    <input id="Submit17" type="submit" value="Submit" onclick="return check()"/>
     
    </body>
    </html>

  2. #2
    i want cake and cookies Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    9,997
    Mentioned
    41 Post(s)
    Tagged
    1 Thread(s)
    Hi there, I would like to allow the "+" sign in my 3 textboxes. When the user does not put the "+" sign in the textboxes, and it is being validated, the system will allow it to pass.

    Also, it must check that it has at least 8 digits.
    I am not understanding you. You want an (optional) + sign to be included with the numbers, okay. But then you say if they don't include it, it's still okay.

    Does that mean you want the + to be required?

    What is your current code doing?

    I don't believe you are supposed to comment out the + inside the characterclass [] with a backslash. [\+] should not mean a literal +, but instead may be trying to preserve its special meaning instead.

    var telnr = /^[+]?\d{8,20}$/;
    or
    var telnr = /^(\+)?\d{8,20}$/; (not sure about this one as +? would mean nongreedy match) *edit wrapped + in parens

    I took out the parens because I didn't see you using \1 or anything with them... and if you wanted them literally, you would have needed to comment those out \( \)

  3. #3
    i want cake and cookies Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    9,997
    Mentioned
    41 Post(s)
    Tagged
    1 Thread(s)
    Hm, and, question, is that whole big function noSpace(e, dec) there just to remove spaces and stuff from the input? You may be able to use a little regex instead that may be faster... match whatever's a number and use string.replace() onkeydown.

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2010
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, what I meant was, if the user decides to key in the "+" sign, the system will accept it. Even if the user does not put in the "+" sign, the system will still allow the phone number.

    The noSpace function is actually used to deter users from keying in spaces. I find this method better than using a regex function, whereby it still has to alert the user that no spaces are to be allowed. IMO, I find it a hassle to do so.

  5. #5
    i want cake and cookies Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    9,997
    Mentioned
    41 Post(s)
    Tagged
    1 Thread(s)
    The noSpace function is actually used to deter users from keying in spaces. I find this method better than using a regex function, whereby it still has to alert the user that no spaces are to be allowed. IMO, I find it a hassle to do so.
    Ah.

    Hi, what I meant was, if the user decides to key in the "+" sign, the system will accept it. Even if the user does not put in the "+" sign, the system will still allow the phone number.
    So are you saying that currently, numbers with a + are failing validation? (I'm curious what exactly the HTML is supposed to be... it looks almost like a form but there are no form tags...)

    I still think that the way you've got it set in the var telnr is incorrect.

    Normally (JS noob that I am) I'll check my regex first, apart from the rest of the code, to make sure that it matches what I want. If I know my regex is good, then I can look at the rest of my code and know I've got an error elsewhere.

    I'm still leaning to var telnr = /^(\+)?\d{8,20}$/; which only matched a possible + and 8-20 digits (it does not match anyone adding in the () many people add). Though I also don't see it actually getting checked anywhere either (I see the telnr.test(tfld) but don't see tfld anywhere else).

Bookmarks

Posting Permissions

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