Moving focus to next field

V

venky

Hello all,

I have a form and for phone fields i have three text boxes for first
three area code and next three and then last 4. I have the max
characters as 3, 3 and 4 and which works fine but once in the first
phone field after the user enters 3 characters, how can i move the
focus automatically to next phone field and then from second to third.
Any ideas, how i can do this in asp.net? I am using C# as code behind
 
A

agapeton

This is client-side, so you have to use ECMAScript (you might call it
JavaScript)...

Here's a sample...

<input id="txtPhone1" maxlength="3"/>
<input id="txtPhone2" maxlength="3"/>
<input id="txtPhone3" maxlength="4"/>

var txtPhone1Obj;
var txtPhone2Obj;
var txtPhone3Obj;
window.onload = function(evt) {
txtPhone1Obj = document.getElementById('txtPhone1');
txtPhone2Obj = document.getElementById('txtPhone2');
txtPhone3Obj = document.getElementById('txtPhone3');

txtPhone1Obj.onkeypress = function(evt) {
if(txtPhone1Obj.value.length == txtPhone1Obj.maxLength) {
txtPhone2Obj.focus( );
}
};

txtPhone2Obj.onkeypress = function(evt) {
if(txtPhone2Obj.value.length == txtPhone2Obj.maxLength) {
txtPhone3Obj.focus( );
}
}
}

This meets all the requirements for being legal for use on the public
Internet: it's standard. More importantly, it's therefore future
proof.

David Betz
http://davidbetz.net/dynamicbliss/
http://davidbetz.net/winfx/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top