Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi experts,

on my webpage i have Textboxes to enter the information.

I have a Textbox with MobileNo as TxtMobileNo, In this the user will enter mobileno. it has set Maximum length to 10.

if the user enters 10 numbers and press on any key the cursor must go the other textbox.

thats it.

help me thanks.
Posted

you have to handle keypress event of the textbox.

JavaScript
$('#txtPhoneNumber').keyPress(function() {
    if($(this).val().length == 10) {
        $('#<next-element-id>').focus();
    }
});
 
Share this answer
 
v2
Create one javascript method.
in that method check the length of the textbox's text. if it is 10 then set focus to another control.

and call this method on onkeypress event of the textbox.
 
Share this answer
 
Comments
HYD Webdeveloper 14-Aug-13 6:05am    
please can you show me once.
CodeBlack 14-Aug-13 6:39am    
I have submitted my code as a solution number 5.
Dont forget to mark it as answer and rate this solution.
hii

please add javascript function in textbox key pressed event and maintain
textbox focus base on maxlength

Example

XML
<asp:TextBox ID="txt1" MaxLength="4" runat="server" onkeypress="myFunction(event)"  ></asp:TextBox>
       <asp:TextBox ID="txt2" MaxLength="4" runat="server"></asp:TextBox>
       <script type="text/javascript" >
           function myFunction(event) {
               var str = document.getElementById('<%=txt1.ClientID%>').value;
               if (event.keyCode == 8) {
                   document.getElementById('<%=txt1.ClientID%>').focus();
               }
               else if (str.length == 4) {
                   document.getElementById('<%=txt2.ClientID%>').focus();
               }
               else { }
           }
       </script>
HTML




here we have used keycode 8 for backspace
check it
 
Share this answer
 
ASPX Code for TextBox :

XML
<asp:TextBox runat="server" ID="textMobileNo" MaxLength="10" onkeypress="ManageFocus(event)"></asp:TextBox>
<asp:TextBox runat="server" ID="textNew"></asp:TextBox>


Javascript Method:

C#
function ManageFocus(e) {
           var textLength = document.getElementById('<%=textMobileNo.ClientID%>').value.length;
           var maxLength = document.getElementById('<%=textMobileNo.ClientID%>').maxLength;

           if (textLength == maxLength) {
               document.getElementById('<%=textNew.ClientID%>').focus();
           }
       }
 
Share this answer
 
Comments
fjdiewornncalwe 16-Aug-13 9:35am    
Your answers are quite good, but might I suggest that it would be much easier to follow your train of thought if you were to use the "Improve Solution" widget to update your existing answer with updated information instead of posting a new solution each time. Cheers.
CodeBlack 16-Aug-13 9:39am    
right. thanks for the info :)

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