Web Development HTML Guide - Learn HTML
Developer Tutorials
ASP
CGI & Perl
CSS
Flash
HTML
Java
JavaScript
Linux
MySQL
PHP
Photoshop
Python
Wireless
XML



Developer Manuals
Learn HTML
Learn PHP
Learn MySQL
Learn CSS
Learn Pear


Developer Scripts
ASP Scripts
ASP.NET Scripts
CGI & Perl Scripts
Flash Scripts
Java Scripts
JavaScript Scripts
PHP Scripts
Python Scripts
Remotely Hosted Scripts
Tools & Utilities Scripts
XML Scripts

Developer Resources
Developer Tools
Developer News
Developer Forums
Developer Content
Developer Book Reviews
Survey Software

Web Hosting Directory
Budget Web Hosting
ColdFusion Hosting
Dedicated Servers
Domain Hosting
E-Commerce Hosting
Email Hosting
Free Web Hosting
Linux Web Hosting
Managed Hosting
Reseller Hosting
Small Business Hosting
Windows Web Hosting

Automatically Jumping To The Next Field

If your form contains data that is of a fixed length it may be convenient to have the cursor automatically jump to the next field when the proper amount of data is filled in. For example, a social security number consists of three fields of 3, 2, and 4 characters long. We can set those three fields so that the cursor jumps automatically from one to the next as the data is filled in.

First, copy this script exactly as-is into the <HEAD> section of your document.

<SCRIPT TYPE="text/javascript">
<!--
var downStrokeField;
function autojump(fieldName,nextFieldName,fakeMaxLength)
{
var myForm=document.forms[document.forms.length - 1];
var myField=myForm.elements[fieldName];
myField.nextField=myForm.elements[nextFieldName];

if (myField.maxLength == null)
   myField.maxLength=fakeMaxLength;

myField.onkeydown=autojump_keyDown;
myField.onkeyup=autojump_keyUp;
}

function autojump_keyDown()
{
this.beforeLength=this.value.length;
downStrokeField=this;
}

function autojump_keyUp()
{
if (
   (this == downStrokeField) && 
   (this.value.length > this.beforeLength) && 
   (this.value.length >= this.maxLength)
   )
   this.nextField.focus();
downStrokeField=null;
}
//-->
</SCRIPT>

Now, suppose we have this form in which the first three fields represent the social security number. We create the fields as usual without any special markup.

<FORM ACTION="../cgi-bin/mycgi.pl" METHOD=POST>

SSN: 
<INPUT TYPE=TEXT NAME="ssn_1" MAXLENGTH=3 SIZE=3> -
<INPUT TYPE=TEXT NAME="ssn_2" MAXLENGTH=2 SIZE=2> -
<INPUT TYPE=TEXT NAME="ssn_3" MAXLENGTH=4 SIZE=4><BR>

email: <INPUT TYPE=TEXT NAME="email"><BR>

</FORM>

To set the three fields as automatics jumpers, we add a short script immediately after closing the form. The script must come immediately after the form before any other forms. We use the autojump() command once for each field which must auto-jump. autojump() takes three argument: the name of the field which should auto-jump, the name of the field to jump to, and how many characters fills up the field. So these commands set ssn_1 to jump to ssn_2 after three characters, ssn_2 to jump to ssn_3 after two characters, and ssn_3 to jump to email after four characters.

<SCRIPT TYPE="text/javascript">
<!--
autojump('ssn_1', 'ssn_2', 3);
autojump('ssn_2', 'ssn_3', 2);
autojump('ssn_3', 'email', 4);
//-->
</SCRIPT>

This gives us this form:

SSN: - -
email:

Copyright Idocs, Inc. Written by Miko Sullivan











About the NetVisits, Inc Network | Advertise
Developer Tutorials hosted by HostGator.
Copyright ©2007 NetVisits, Inc Network. All Rights Reserved. Privacy Policy.
Visit other NetVisits, Inc. sites: