Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Moving focus from field to field

Status
Not open for further replies.

erinql

Programmer
Oct 19, 2001
13
0
0
US
I have a javascript that no longer works, and I'm not a JS programmer (yet). It's setup for 4 text fields for entering IP octets, and it's supposed to change focus after entering the 3rd character in each box. I'm not sure why it fails now.

Also, I want to adjust the script so that when it shifts focus, it selects all the existing text in the box, not put the cursor at the beginning of the text.

Here's the old scripting:

<SCRIPT LANGUAGE=&quot;JAVASCRIPT&quot;>
// Set some variables
var octet1Length = 2
var octet2Length = 2
var octet3Length = 2
//
// This function checks if the given character is a digit.
function isDigit (c)
{
return ((c >= &quot;0&quot;) && (c <= &quot;9&quot;))
}
//
// This function checks if the given string is an integer
function isInteger (s)
{ var i;
for (i = 0; i < s.length; i++) {
var c = s.charAt(i);
if (!isDigit(c)) return false;
}
return true;
}
//
function handleTabs(name) {

if (name == &quot;octet1&quot;) {

if ( ( isInteger(document.form1.octet1.value) ) &&

( document.form1.octet1.value.length == octet1Length )

) {

document.form1.octet2.focus();

}

}

if (name == &quot;octet2&quot;) {

if ( ( isInteger(document.form1.octet2.value) ) &&

( document.form1.octet2.value.length == octet2Length )

) {

document.form1.octet3.focus();

}

}


if (name == &quot;octet3&quot;) {

if ( ( isInteger(document.form1.octet3.value) ) &&

( document.form1.octet3.value.length == octet3Length )

) {

document.form1.octet4.focus();

}
}
}
</script>

--------later in the body:

<TD>
<input NAME=&quot;octet1&quot; TYPE=&quot;text&quot; SIZE=&quot;3&quot; MAXLENGTH=&quot;3&quot; VALUE=&quot;1&quot; onKeyPress=&quot;handleTabs('octet1')&quot;>
<input NAME=&quot;octet2&quot; TYPE=&quot;text&quot; SIZE=&quot;3&quot; MAXLENGTH=&quot;3&quot; VALUE=&quot;1&quot; onKeyPress=&quot;handleTabs('octet2')&quot;>
<input NAME=&quot;octet3&quot; TYPE=&quot;text&quot; SIZE=&quot;3&quot; MAXLENGTH=&quot;3&quot; VALUE=&quot;1&quot; onKeyPress=&quot;handleTabs('octet3')&quot;>
<input NAME=&quot;octet4&quot; TYPE=&quot;text&quot; SIZE=&quot;3&quot; MAXLENGTH=&quot;3&quot; VALUE=&quot;1&quot; onKeyPress=&quot;handleTabs('octet4')&quot;> </td>
</td>

Thanks,
Eman Erin Quick-Laughlin
DBA, PHP/MySQL programmer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top