www.webdeveloper.com
Results 1 to 3 of 3

Thread: Phone number validation

  1. #1
    Join Date
    Jan 2010
    Posts
    2

    Phone number validation

    Hi there, I need to validate three textboxes and it will validate for numbers. How should I change my code to validate three textboxes?

    Code:
    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>
        <script type='text/javascript' language="javascript">
    
    var validPhoneChar = "+";
    // Minimum no of digits in an international phone no.
    var minDigitsInPhoneNumber = 10;
    
    function isInteger(s)
    {   
        var i;
        for (i = 0; i < s.length; i++)
        {   
            // Check that current character is a number.
            var c = s.charAt(i);
            if (((c < "0") || (c > "9"))) 
                return false;
        }
        // All characters are numbers.
        return true;
    }
    
    function stripCharsInBag(s, bag)
    {   
        var i;
        var returnString = "";
        // Search through string's characters one by one.
        // If character is not in bag, append to returnString.
        for (i = 0; i < s.length; i++)
        {   
            // Check that current character isn't whitespace.
            var c = s.charAt(i);
            if (bag.indexOf(c) == -1) returnString += c;
        }
        return returnString;
    }
    
    function checkRequirements(strPhone)
    {
        if(strPhone.indexOf("+") > 1)
            return false;
        s = stripCharsInBag(strPhone,validPhoneChar);
        return (isInteger(s) && s.length >= minDigitsInPhoneNumber);
    }
    
    function ValidateForm()
    {
        var Phone = document.getElementById("Text1");
        //var Phone1 = document.getElementById("Text23");
        if ((Phone.value == null)||(Phone.value == "")) //if match failed
        {
            alert("Please Enter your Phone Number.");
            Phone.focus();
                return false;
        }
        /*if ((Phone1.value == null)||(Phone1.value == "")) //if match failed
        {
            alert("Please Enter your Phone Number0.");
            Phone.focus();
                return false;
        }*/    
        if (checkRequirements(Phone.value) == false)
        {
            alert("Please Enter a Valid Phone Number.");
            Phone.value = "";
            Phone.focus();
                return false;
        }
        else    
            alert("YAY!");
        return true;
     }    
        
        
    function NotAllowSpace()
    {
        // Get the ASCII value of the key that the user entered
        var key = window.event.keyCode;
    
        // Verify if the key entered was a Space
        if ( key == 32 )
        {
            // If it was, then dispose the key and continue with entry
            window.event.returnValue = null; 
            alert("Invalid,Please check")
        }
        
        else
            // If it was not, then allow the entry to continue
            return;
    
    }
        </script>
    </head>
    <body>
    <label>Office Telephone:</label>
    <input id="Text1" type="text" onkeydown = "return (event.keyCode != 32)"/><br />
    <input id="Button1" type="button" value="button" onclick="return ValidateForm();" /><br />
    
    </body>
    </html>
    I need to use Javascript to validate 3 textboxes, whereby the users can only key in numbers (because they are phone numbers related fields). If any of the textbox is empty, display an alert message to show which textbox is empty. I do not want to show many alert messages to show that, for example, text1 and text2 are empty, it will show two alert messages. I would need to show one "summarized" alert message instead.



    Next, check if the textbox matches the pattern (which is to check if it has the skeleton of a phone number). If it is, show an alert message that it is alright. Else, show that it does not match.

  2. #2
    Join Date
    Oct 2006
    Posts
    939
    Is the plus ( + ) character valid in a phone number? I think that means international, can you give an example of an international phone number?

  3. #3
    Join Date
    Jan 2010
    Posts
    2
    Er... I'm not sure what you mean, but I'm guessing it's like this:

    852-9356-6547?

    Apart from this, what i want the system to accept is,

    +97464647
    97464647
    +85497464647

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