Automatic Tabbing

Question: I need to restrict input characters in a text field, as part of a rather extensive input screen. Found your code snippet most helpful, thank you very much.

If the input passes validation, can it tab automatically to the next field? Each field only has one character, either Y/N or 1-9 etc.

I would appreciate any pointers, but in any event, thank you for the code on your site.

Answer: To transfer the focus to another field after a specified number of characters have been entered you test the field length and then set the focus.

eg. if the next field is called document.myform.field2 and you want to go there after one character has been entered in field1 (with y and n being the only valid characters) you would use:

onkeyup="res(this,'yn');if (this.value.length >= 1) document.myform.field2.focus();"

You can use this without testing for specific characters just by using the if statement by itself. Also if you want a field length greater than one just substitute the length you want into the if statement. The following form allows you to enter any three characters (whatever you like as it isn't validated) into the first field and then automatically tabs to the second field.

 

The code for the above form is as follows:

<form name="myform">
<input type="text" size="4" name="field1" onkeyup="if (this.value.length >= 3) document.myform.field2.focus();">
&nbsp; <input type="text" size="4" name="field2">
</form>

 

This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow
Donate