AaronCampbell
10-08-2004, 07:35 PM
Here is the script:<script language="javascript">
function getKey(e) {
if (e.keyCode) return String.fromCharCode(e.keyCode);
if (e.which) return String.fromCharCode(e.which);
}
function limitEntry(e,field) {
var el = 0, f = field.form, keyChar = getKey(e);
if (/\D/.test(keyChar)) return false; //digits only
while (f.elements[el]) {
if (f.elements[el++] == field) break; //find current field
}
if ((field.value.length + 1 == field.maxLength) && f.elements[el]) {
f.elements[el-1].value += keyChar;
f.elements[el].focus();
return false;
}
return true;
}
</script>Here is a form you might use it with:<input name="phone" id="phone" type="text" class="Form" onkeypress="return limitEntry(event,this)" maxlength="3" size="3" />-
<input name="phone2" id="phone2" type="text" class="Form" onkeypress="return limitEntry(event,this)" maxlength="3" size="3" />-
<input name="phone3" id="phone3" type="text" class="Form" onkeypress="return limitEntry(event,this)" maxlength="4" size="4" /><br />
<input name="fax" id="fax" type="text" class="Form" onkeypress="return limitEntry(event,this)" maxlength="10" size="10" />
Ok, basically, the script allows only digits to be entered into the box, and when it reaches it's max, it focus' you on the next element in the form. The problem is, for the LAST digit in any given box, it adds it to the end of the value, rather than returning true. This means that if you have 619555124 and you move your cursor between the 2 & the 4 to enter a 3, you get 6195551243 rather than 6195551234...and this can be annoying.
The problem is, if you simply return true, you can't then focus on the next element. Any ideas?
function getKey(e) {
if (e.keyCode) return String.fromCharCode(e.keyCode);
if (e.which) return String.fromCharCode(e.which);
}
function limitEntry(e,field) {
var el = 0, f = field.form, keyChar = getKey(e);
if (/\D/.test(keyChar)) return false; //digits only
while (f.elements[el]) {
if (f.elements[el++] == field) break; //find current field
}
if ((field.value.length + 1 == field.maxLength) && f.elements[el]) {
f.elements[el-1].value += keyChar;
f.elements[el].focus();
return false;
}
return true;
}
</script>Here is a form you might use it with:<input name="phone" id="phone" type="text" class="Form" onkeypress="return limitEntry(event,this)" maxlength="3" size="3" />-
<input name="phone2" id="phone2" type="text" class="Form" onkeypress="return limitEntry(event,this)" maxlength="3" size="3" />-
<input name="phone3" id="phone3" type="text" class="Form" onkeypress="return limitEntry(event,this)" maxlength="4" size="4" /><br />
<input name="fax" id="fax" type="text" class="Form" onkeypress="return limitEntry(event,this)" maxlength="10" size="10" />
Ok, basically, the script allows only digits to be entered into the box, and when it reaches it's max, it focus' you on the next element in the form. The problem is, for the LAST digit in any given box, it adds it to the end of the value, rather than returning true. This means that if you have 619555124 and you move your cursor between the 2 & the 4 to enter a 3, you get 6195551243 rather than 6195551234...and this can be annoying.
The problem is, if you simply return true, you can't then focus on the next element. Any ideas?