Move focus to next TextBox automatically when user types in the Textbox in Asp.Net

Introduction:

In this article I am going to explain how to move the focus to next textbox when user types in textbox and the total character count cross the limit of maxlength property.

Prerequisites:

You need to add the necessary JQuery files to your page prior to use this plugin. In this example I have used the online JQuery CDN files available. If you don’t want to use the online available cdn file then you can manually add the jquery files to your solution. I have explained the steps here in detail.

Implementation:

Here we are make use of .attr() method in Jquery to get the maxlength property of textbox at runtime and then checks if the textbox character count crosses the length. Based on this we will move the focus to next textbox.

Sample Code

 <script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
         <script>
             // A $( document ).ready() block.
             $(document).ready(function () {
                 //Attach key up event handler
                 $('#<%= TextBox1.ClientID %>').keyup(function () {
                     //check if user typed three characters
                     if (this.value.length == $(this).attr('maxlength')) {
                         //move the focus to another textbox
                         $('#<%= TextBox2.ClientID %>').focus();
                     }
                 });
                 $('#<%= TextBox2.ClientID %>').keyup(function () {
                    if (this.value.length == $(this).attr('maxlength')) {
                         $('#<%= TextBox3.ClientID %>').focus();
                     }
                 });
             });
         </script>
 
         <asp:TextBox runat="server" ID="TextBox1" MaxLength="3" Width="50"></asp:TextBox>
         <asp:TextBox runat="server" ID="TextBox2" MaxLength="2" Width="50"></asp:TextBox>
         <asp:TextBox runat="server" ID="TextBox3" MaxLength="2" Width="50"></asp:TextBox>
 

Demo

Advertisements

, , , ,

  1. #1 by willie on February 10, 2017 - 9:01 pm

    thank you!

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: