www.webdeveloper.com
Results 1 to 7 of 7

Thread: Auto hyphenate is slow

  1. #1
    Join Date
    May 2012
    Posts
    4

    Auto hyphenate is slow

    Hi guys, I'm using the following to auto-hyphenate a social security number as the user types it in:

    <script type= "text/javascript">
    function chkHyphen( fld, pos ) {
    for (var i = 0; i < pos.length; i++) {
    if ( pos[i] == fld.value.length ) fld.value += '-';
    }
    };
    </script>

    <input type="text" size="10" name="ssn" onkeyup="chkHyphen( this, [3,6]);" maxlength="11"/>

    The problem is, if you type it in really fast it skips a hyphen sometimes. If you type it nice and slowly it works fine.

    Any thoughts on how I can get it to respond to fast typists as well?

    Thanks,

    Richard

  2. #2
    Join Date
    Apr 2012
    Posts
    55
    Try this:

    PHP Code:
    <script type"text/javascript">
        function 
    chkHyphen(fldpos) {
            var 
    fld.valuereNonDigits = /[^\d]/gp;
            
    // Remove existing hyphens and non-digits
            
    s.replace(reNonDigits'');
            
    // Insert hyphens
            
    for(var pos.length 1>= 0i--) {
                
    pos[i];
                if(
    s.length p) {
                    
    s.substring(0p) + '-' s.substring(p);
                }
            }
            
    fld.value s;
        }
    </script> 
    And shouldn't it be "chkHyphen(this, [3, 5]);" instead of [3, 6]?

  3. #3
    Join Date
    May 2012
    Posts
    4
    Thanks Nathan, I am using chkHyphen(this, [3, 5]);

    I tried your code, and now the box clears itself after every keystroke.

  4. #4
    Join Date
    Apr 2012
    Posts
    55
    Weird. The forum removed the backslash in the regular expression. Change

    reNonDigits = /[^d]/g

    to

    reNonDigits = /[^\d]/g

  5. #5
    Join Date
    May 2012
    Posts
    4
    That works well, except it's missing a digit. It shows xxx-xxx-xxx.

    Any way to make it xxx-xx-xxxx?

    Thanks!

    Richard

  6. #6
    Join Date
    Apr 2012
    Posts
    55
    <input type="text" size="10" name="ssn" onkeyup="chkHyphen( this, [3,5]);" maxlength="11"/>

  7. #7
    Join Date
    May 2012
    Posts
    4
    Perfect! Thanks so much!

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