Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I've just had a web project land on my desk, I'm using Visual Studio 2008 and C#. The form has approximately 90 textboxes and 7 checkboxes, each textbox is set to a specific maxlength.

It's alright using an if statement so that if textbox length = max length focus next textbox but is long winded, there has to be a more productive way of doing this

I need to write a function in C# so that when the textbox has reached its maxlength it will autotab to the next enabled box

many thanks for your help

What I have tried:

I've looked at several options on the next but many fail to work
Posted
Updated 22-Apr-16 4:32am
Comments
ZurdoDev 22-Apr-16 10:14am    
1. No, you won't write this in C# because C# code runs on the server, so you have to post page back to do that.
2. Write the code in jquery. It can be done easily. I suggest a quick google search for it.
Richard Deeming 22-Apr-16 10:14am    
Don't write the code in C#! You don't want the form to post back to the server every time the user presses a key. Use Javascript instead.

1 solution

I would use JQuery

You can do it quite neatly in the client.

I haven't tested it so it may need tweaking.

JavaScript
$("input[type='text']").change(function() {
  var max = parseInt($(this).attr("maxlength"), 10);
  var len = $(this).val().length;

  if(len == max)
  {
    var tab = parseInt($(this).attr("tabindex"), 10);
    $("input[tabindex='" + (tab + 1) + "']").focus();
  }

});
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900