Solved

Focus on Textboxes Auto Tab and Shift-Tab

Posted on 2011-09-25
7
1,088 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

761 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