Auto-Adding the Hyphens in the phone # field

Information on ASP, ASP.NET, PHP, HTML, Javascript and anything else WWW related.

Auto-Adding the Hyphens in the phone # field

Postby admin on Wed Oct 29, 2008 10:03 pm

Add this to the page:
Code: Select all
<script type="text/javascript" language="javascript">
<!--
    function phone(obj, e)
    {
                        if (e.keyCode)
                                    keycode=e.keyCode;
            else
                                    keycode=e.which;
        strKey = String.fromCharCode ( keycode );
        var reValidChars = /\d/;
        var text = obj.value;
                        if (reValidChars.test(strKey))
        {
            if(text.length == 3 || text.length == 7)
            {
                        obj.value += '-';
                                    }
        }
                        else if(keycode != 8 // backspace
                                    && keycode != 9  // tab
                                    && keycode != 13 // enter
                                    && keycode != 46 // delete
                                    && keycode != 37 // left arrow
                                    && keycode != 39 // right arrow
                                    )
                        {
                                    return false;
                        }
    }
//-->
</script>

Then reference the code from the phone input field like this:
Code: Select all
<input type="text" name="Phone" value="" size="14" maxlength="12" onKeyPress="return phone(this, event)">
admin
Site Admin
 
Posts: 6
Joined: Wed Oct 29, 2008 12:44 pm

Return to Web Site Assistance

Who is online

Users browsing this forum: No registered users and 1 guest

cron