Solved

Focus on Textboxes Auto Tab and Shift-Tab

Posted on 2011-09-25
7
1,050 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

706 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

11 Experts available now in Live!

Get 1:1 Help Now