Solved

Focus on Textboxes Auto Tab and Shift-Tab (continued)

Posted on 2011-09-28
7
471 Views
Last Modified: 2012-08-13
Experts,

I received code the other day from one of the Expert’s and I am trying to add a little more to that code.  

I have seven textboxes that can only hold one character each.  When a character is added in the textbox, focus should be set to the next textbox.  If the user uses Shift-Tab it should get focus of the previous textbox.  

What I need to add to the code is I only want the characters A-Z, 0-9, a dash (-), an apostrophe (‘) and a space ( ).  One of the experts stated that this could be done using regex and that I needed to create a new question.

 Well here it is…  any ideas?

<script type="text/javascript">
        $(document).ready(function () {
            $('#<%= Let1.ClientID %>').keyup(function (event) {
                var c = String.fromCharCode(event.keyCode);
                var isWordcharacter = c.match(/\w/);

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

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

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

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

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

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

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

Open in new window

0
Comment
Question by:us1975mc
  • 4
  • 2
7 Comments
 
LVL 35

Expert Comment

by:Terry Woods
ID: 36720331
Might be as simple as changing these regex lines:
                var isWordcharacter = c.match(/\w/);
to:
                var isWordcharacter = c.match(/[A-Za-z '-]/);

Note I've used an apostrophe, not a backtick as you put in your round brackets (`) - you can change it if you need to.
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 36720336
Oops, forgot the numbers:
                var isWordcharacter = c.match(/[A-Za-z\d '-]/);
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 36720340
And if you don't want lower case:
                var isWordcharacter = c.match(/[A-Z\d '-]/);
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:us1975mc
ID: 36789174
TerryAtOpus,

Below is my code.  Very straight forward, I thought, but I still cannot get it to work.

I can put a !, @, #, $, % ,^ ,& l,*, (, ), _ ,+ ,= and several other characters in the text box.

Any Ideas what I am doing wrong?

Totally confused in Missouri...


Demo.aspx
------------------------------------------------------------------

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Demo.aspx.vb" Inherits="Demo" %>

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

<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 () {
            $('#<%= Let1.ClientID %>').keyup(function (event) {
                var c = String.fromCharCode(event.keyCode);
                var isWordcharacter = c.match(/[A-Z\d '-]/);

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

            });
            $('#<%= Let2.ClientID %>').keyup(function (event) {
                var c = String.fromCharCode(event.keyCode);
                var isWordcharacter = c.match(/[A-Z\d '-]/);

                if (isWordcharacter) {
                    $('#<%= Let3.ClientID %>').focus();
                    $('#<%= Let3.ClientID %>').select();
                }
            });
            $('#<%= Let3.ClientID %>').keyup(function (event) {
                var c = String.fromCharCode(event.keyCode);
                var isWordcharacter = c.match(/[A-Z\d '-]/);

                if (isWordcharacter) {
                    $('#<%= Let4.ClientID %>').focus();
                    $('#<%= Let4.ClientID %>').select();
                }
            });
            $('#<%= Let4.ClientID %>').keyup(function (event) {
                var c = String.fromCharCode(event.keyCode);
                var isWordcharacter = c.match(/[A-Z\d '-]/);

                if (isWordcharacter) {
                    $('#<%= Let5.ClientID %>').focus();
                    $('#<%= Let5.ClientID %>').select();
                }
            });
            $('#<%= Let5.ClientID %>').keyup(function (event) {
                var c = String.fromCharCode(event.keyCode);
                var isWordcharacter = c.match(/[A-Z\d '-]/);

                if (isWordcharacter) {
                    $('#<%= Let6.ClientID %>').focus();
                    $('#<%= Let6.ClientID %>').select();
                }
            });
            $('#<%= Let6.ClientID %>').keyup(function (event) {
                var c = String.fromCharCode(event.keyCode);
                var isWordcharacter = c.match(/[A-Z\d '-]/);

                if (isWordcharacter) {
                    $('#<%= Let7.ClientID %>').focus();
                    $('#<%= Let7.ClientID %>').select();
                }
            });
            $('#<%= Let7.ClientID %>').keyup(function (event) {
                var c = String.fromCharCode(event.keyCode);
                var isWordcharacter = c.match(/[A-Z\d '-]/);

                if (isWordcharacter) {
                    $('#<%= btnView.ClientID %>').focus();
                }
            });
        });
    </script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="Let1" runat="server"></asp:TextBox>
        <br />
        <asp:TextBox ID="Let2" runat="server"></asp:TextBox>
        <br />
        <asp:TextBox ID="Let3" runat="server"></asp:TextBox>
        <br />
        <asp:TextBox ID="Let4" runat="server"></asp:TextBox>
        <br />
        <asp:TextBox ID="Let5" runat="server"></asp:TextBox>
        <br />
        <asp:TextBox ID="Let6" runat="server"></asp:TextBox>
        <br />
        <asp:TextBox ID="Let7" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="btnView" runat="server" Text="View" />
        <br />
        <br />
        <asp:Label ID="Lab1" runat="server" Text=""></asp:Label>

    </div>
    </form>
</body>
</html>

Demo.aspx.vb
------------------------------------------------------------------
Partial Class Demo
    Inherits System.Web.UI.Page

    Protected Sub btnView_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnView.Click
        Lab1.Text = Let1.Text & Let2.Text & Let3.Text & Let4.Text & Let5.Text & Let6.Text & Let7.Text
    End Sub
End Class

Open in new window

0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
ID: 36805956
Unfortunately my forte is regular expressions (and a little javascript), and I don't have an easy way of testing .asp code so I can't load the page as a whole. However, I'd suggest adding some alerts to ensure that string values are as you expect, eg

                var c = String.fromCharCode(event.keyCode);
                alert("Key pressed:"+c);
                var isWordcharacter = c.match(/[A-Z\d '-]/);

                if (isWordcharacter) {
                    alert("Is a word character.");
                    $('#<%= Let2.ClientID %>').focus();
                    $('#<%= Let2.ClientID %>').select();
                }
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36891418
I think doing a plain comparison on the ASCII value of the character would be easier as the allowed input is quite limited.

http://jalaj.net/2007/03/08/asc-and-chr-in-javascript/
0
 

Author Closing Comment

by:us1975mc
ID: 36894261
It took awhile but it is working.

Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

929 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

14 Experts available now in Live!

Get 1:1 Help Now