-
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
-
Try this:
PHP Code:
<script type= "text/javascript">
function chkHyphen(fld, pos) {
var s = fld.value, reNonDigits = /[^\d]/g, p;
// Remove existing hyphens and non-digits
s = s.replace(reNonDigits, '');
// Insert hyphens
for(var i = pos.length - 1; i >= 0; i--) {
p = pos[i];
if(s.length > p) {
s = s.substring(0, p) + '-' + s.substring(p);
}
}
fld.value = s;
}
</script>
And shouldn't it be "chkHyphen(this, [3, 5]);" instead of [3, 6]?
-
Thanks Nathan, I am using chkHyphen(this, [3, 5]);
I tried your code, and now the box clears itself after every keystroke.
-
Weird. The forum removed the backslash in the regular expression. Change
reNonDigits = /[^d]/g
to
reNonDigits = /[^\d]/g
-
That works well, except it's missing a digit. It shows xxx-xxx-xxx.
Any way to make it xxx-xx-xxxx?
Thanks!
Richard
-
<input type="text" size="10" name="ssn" onkeyup="chkHyphen( this, [3,5]);" maxlength="11"/>
-
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
Forum Rules
|
|
Bookmarks