Solved

Focus on Textboxes Auto Tab and Shift-Tab

Posted on 2011-09-25
7
1,071 Views
Last Modified: 2012-05-12
Experts,

On my web page there are 7 textboxes that can hold only one character each.  When the user types a letter in the textbox, I want the focus to be set to the next textbox automatically.  

Here is the problem.  I also want the user to be able to go back to the previous textbox or any of the textboxes, if they wish, and be able to enter a new character.  The user should be able to use the Shift-Tab keyboard combination that would take them to the previous textbox and highlight whatever is in that box so that the information in the textbox can be changed.  If they are on the last textbox, I want them to be able to use the Shift-Tab keyboard combination several times and be able to go to the first textbox and enter all new information if they wish.

The code behind is Visual Basic on this webpage that is being developed in Visual Studio 2010.
 
Thanks,
An old Marine
0
Comment
Question by:us1975mc
  • 4
  • 3
7 Comments
 
LVL 16

Expert Comment

by:disrupt
ID: 36597065
Here you go:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="WebApplication30._default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
  <script type="text/javascript">
      $(document).ready(function () {
          $('#<%= TextBox1.ClientID %>').keyup(function (event) {
              var c = String.fromCharCode(event.keyCode);
              var isWordcharacter = c.match(/\w/);

              if (isWordcharacter) {
                  $('#<%= TextBox2.ClientID %>').focus();
              }

          });
          $('#<%= TextBox2.ClientID %>').keyup(function (event) {
              var c = String.fromCharCode(event.keyCode);
              var isWordcharacter = c.match(/\w/);

              if (isWordcharacter) {
                  $('#<%= TextBox3.ClientID %>').focus();
              }
          });
          $('#<%= TextBox3.ClientID %>').keyup(function (event) {
              var c = String.fromCharCode(event.keyCode);
              var isWordcharacter = c.match(/\w/);

              if (isWordcharacter) {
                  $('#<%= TextBox4.ClientID %>').focus();
              }
          });
          $('#<%= TextBox4.ClientID %>').keyup(function (event) {
              var c = String.fromCharCode(event.keyCode);
              var isWordcharacter = c.match(/\w/);

              if (isWordcharacter) {
                  $('#<%= TextBox5.ClientID %>').focus();
              }
          });
          $('#<%= TextBox5.ClientID %>').keyup(function (event) {
              var c = String.fromCharCode(event.keyCode);
              var isWordcharacter = c.match(/\w/);

              if (isWordcharacter) {
                  $('#<%= TextBox6.ClientID %>').focus();
              }
          });
          $('#<%= TextBox6.ClientID %>').keyup(function (event) {
              var c = String.fromCharCode(event.keyCode);
              var isWordcharacter = c.match(/\w/);

              if (isWordcharacter) {
                  $('#<%= TextBox7.ClientID %>').focus();
              }
          });
      });
  </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
    <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br />
    <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox><br />
    <asp:TextBox ID="TextBox6" runat="server"></asp:TextBox><br />
    <asp:TextBox ID="TextBox7" runat="server"></asp:TextBox><br />
    </form>
</body>
</html>

Open in new window

0
 

Author Comment

by:us1975mc
ID: 36599900
disrupt,

The code works except for one thing. When I Shift-Tab back to the beginning it highlights the one that I land on but then when it goes to the next field it does not highlight that field and a 2nd character is added to each textbox.  
0
 
LVL 16

Accepted Solution

by:
disrupt earned 500 total points
ID: 36599925
If you want it to highlight add this:

 $('#<%= TextBox2.ClientID %>').select();

to each one after the focus like so:

          $('#<%= TextBox1.ClientID %>').keyup(function (event) {
              var c = String.fromCharCode(event.keyCode);
              var isWordcharacter = c.match(/\w/);

              if (isWordcharacter) {
                  $('#<%= TextBox2.ClientID %>').focus();
                  $('#<%= TextBox2.ClientID %>').select();

              }

          });
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Closing Comment

by:us1975mc
ID: 36599952
Perfect!  Just what the Doctor Called For!

Thanks!
0
 

Author Comment

by:us1975mc
ID: 36601035
Is there a way in the javascript to allow only A-Z, 0-9, -, ', or a space in this code?
0
 
LVL 16

Expert Comment

by:disrupt
ID: 36601087
Yes using regex please create a new question
0
 

Author Comment

by:us1975mc
ID: 36602029
disrupt,

I did enter a new question, it is titled "A-Z 0-9 dash space or apostrophe in javascript".

Don't know if you saw it or not.

Thanks
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now