www.webdeveloper.com
Results 1 to 2 of 2

Thread: Phone Number Input Mask

  1. #1
    Join Date
    May 2012
    Posts
    1

    Phone Number Input Mask

    Currently I have a working mask:

    | function phonify(p)
    | {
    | tel = '(';
    | var num = p.value.split('');
    | for(var i = 0; i < num.length; i++)
    | {
    | if(num[i] == '(')
    | num[i] = '';
    | if(num[i] == ')')
    | num[i] = '';
    | if(num[i] == '-')
    | num[i] = '';
    | if(num[i] == '')
    | num[i] = '';
    | }
    | for(var i = 0; i < num.length; i++)
    | {
    | if(i == 3)
    | num[i] = num[i] + ')';
    | if(i == 7)
    | num[i] = num[i] + '-';
    | tel = tel + num[i];
    | }
    | p.value = tel;
    | }
    |
    | --------------------------
    |
    | <input type="text" name="phone" maxlength="13" onkeypress="phonify(this);" />


    My problem is, how do I make it so you are only able to input numbers?

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    Code:
    function phonify(p){
    var digs_only=/^[\d]{1,}$/g,
    non_digs=/[^\d]{1,}/g,
    val=p.value;
    
    if(val.length>0){
    if(!digs_only.test(val)){p.value=val.replace(non_digs,'');p.focus();}
    }
    
    // and your code goes below
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

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