SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: Phone mask

  1. #1
    Froot r gewd SubKamran's Avatar
    Join Date
    May 2002
    Location
    North Star State
    Posts
    597
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Phone mask

    Here's what I need to do...

    When someone is typing in a phone number, after the first 3 chars, put a dash, then the next three chars, another dash.

    How hard is that to do, since my knowledge of javascript is horrible.
    "Sometimes little is more."
    Kamran A
    Web Dev/Designer
    Keyboard not found: Please Press F1 to Continue

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" 
        "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    <head>
    <title>untitled</title>
    <script type="text/javascript">
    
    function filterPhone(oText)
    {
    	oText.value = oText.value.replace(/-/g, '');
    	var phonenum = oText.value, oRegExp = /^\d{0,10}$/;
    	if (!oRegExp.test(phonenum))
    		oText.value = oText.prev_value || '';
    	oText.prev_value = oText.value;
    }
    
    function checkPhone(oText)
    {
    	var phonenum = oText.value, oRegExp = /^(\d{3})(\d{3})(\d{4})$/;
    	if (oRegExp.test(phonenum))
    		oText.value = phonenum.replace(oRegExp , '$1-$2-$3');
    	else
    	{
    		alert('Please enter your correct phone number (10 consecutive digits).\n\nThank you.');
    		setTimeout(function(){oText.focus()}, 50);
    		setTimeout(function(){oText.select()}, 100);
    	}
    }
    
    </script>
    </head>
    <body>
    <form>
    <strong>Phone number</strong> (<em>nnnnnnnnnn</em>) 
    <input type="text" name="phonenum" value="" 
    onkeyup="return filterPhone(this)" 
    onblur="checkPhone(this)" />
    </form>
    </body>
    </html>
    ::: certified wild guess :::


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
  •