By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,550 Members | 2,725 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,550 IT Pros & Developers. It's quick & easy.

JavaScript: Move focus on keypress if textfield is finished

Expert 100+
P: 210
I have a form where users will input phone numbers. The way it is set up is with three text boxes. First for phone prefix( 314) then second for the middle number (546) and finally third for last four digits (4563). What i am being asked to do is the cursor need to jump to second text box as soon as user inputs 3 digits on the first text box. and so on. Is there a way to accomplish this. Please help!!

Thank you in advance.
Feb 5 '08 #1
Share this Question
Share on Google+
9 Replies


Plater
Expert 5K+
P: 7,872
You can but you need to be pretty carefull about it.
You need to watch the keypress events for the textboxes (javascript event)
Then you have to watch to see if:
A) the maximum allowed characters are already in the box (for the first box it would be 3)
B) that the key being pressed is a character key and not a control key (like arrow keys, backspace, delete, etc, etc) so that you don't keep jumping to another textbox when someone presses the arrow key.
Then if the end is reached, set focus to your next control.

The folks over in the javascript section might be able to explain it a bit better.
Feb 5 '08 #2

Expert 100+
P: 210
Thanks ,

This means that i will have to use AJAX for this. I know how to do this in classic asp but i am new to AJAX so way confused . Isnt there any other simple solution ??
Feb 5 '08 #3

Plater
Expert 5K+
P: 7,872
Well, you don't HAVE to use ajax for this. I don't think you need to have it keep coming back to the server for anything?
Feb 5 '08 #4

Expert 100+
P: 210
can you provide me with example on how can it be done? please
Feb 5 '08 #5

Plater
Expert 5K+
P: 7,872
I will do you one better and move the question over to the section where people could give you a good example.

MODERATOR
Feb 6 '08 #6

acoder
Expert Mod 15k+
P: 16,027
Here's a good example.
Feb 6 '08 #7

Expert 100+
P: 210
The posting is for simple html form. Can you please provide me with the example on how to do this with asp.net form.

thank you in advance.
Feb 6 '08 #8

Plater
Expert 5K+
P: 7,872
The posting is for simple html form. Can you please provide me with the example on how to do this with asp.net form.

thank you in advance.
Same thing. ASP.NET still just makes a regular HTML form.
And you're going to be doing this with javascript anyway.
Feb 6 '08 #9

acoder
Expert Mod 15k+
P: 16,027
Yes, that's right. When the server-side code gets to the browser it's been translated/converted into client-side HTML markup. View source in your browser and you'll see.
Feb 7 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.