Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hsi Everyone
I have online registration in website,so i use six html input type text boxes,i have completed if first text box have 4 letters automatically move to next text box up to last text and below that code
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JavaScript to automatically move from one field to another field</title>
<script type="text/javascript">
function movetoNext(current, nextFieldID) {
if (current.value.length >= current.maxLength) {
document.getElementById(nextFieldID).focus();
}
}
</script>
</head>
<body>
Enter your Text:
<input type="text" id="first" size="4" onkeyup="movetoNext(this, 'second')" maxlength="3" />
<input type="text" id="second" size="4" onkeyup="movetoNext(this, 'third')" maxlength="3" />
<input type="text" id="third" size="5" maxlength="4" />
</body>
</html>


my next requirement is if wrongly enter serial number in all text box,user clear that value by using 'Backspace' from any text box,if text box clear automatically move to previous text box in last character .same like movetoNext() function above in code,like that i want one function movetoPrevious() function for move to previous text box in last letter so can clear text by using baskspace.
Note:only use backspace button ,support for all browser also like ie8,ie9,mozila and chrome

Pls reply asap
Regards
Aravind
Posted
Updated 17-Jul-13 21:00pm
v2
Comments
Shubhashish_Mandal 17-Jul-13 2:32am    
Well, what happen if user want to delete data from only one textbox.?
Aravindba 17-Jul-13 12:18pm    
if worngly enter serial no.so user delete data form text box.did u read above question ? if not read clearly and use that code ,same like i want clear data .
Shubhashish_Mandal 18-Jul-13 2:25am    
yes I did. But I want to know if user just make mistake to put correct serial no in one textFiled out of 4.In that case if user try to delete using backsapce , once the filed is clear , it goes to the next/previous textfiled to delete the content which are holding correct serial no. So is this a desired behaviour.
Aravindba 18-Jul-13 2:59am    
Thank u for u reply,but again u not read the question clearly,( movetoPrevious() function for move to previous text box in last letter so can clear text by using baskspace.)if cursor goes to previous again user press the backspace then only it clear. ok ? if one box is clear not automatically clear previous box all.
Shubhashish_Mandal 18-Jul-13 3:12am    
think it little bit practical. Suppose I have 2 textfield(tf1 and tf2)
in tf1="ABCD" and tf2="BCAD"
Now user try to delete the value of tf2 using backspace and he/she press the backspace four time and at the time fifth, cursor goes to the previous tf1 and wait for desired operation, right. Now my question is that if user press the backspace key more than 4 (let 10, would be vary by user to user) then what happen.

1 solution

I have some code for you
js
JavaScript
function move(txt) {

           var value = txt.value;
           var length = txt.value.length

           if (length == 0) {
               $(txt).prev().focus();
               return false;
           }
           else if (length > 4) {
               $(txt).next().focus().val(value.substr(4, 1));
               $(txt).val(value.substr(0, 4));
               return false;
           }
       }

html
ASP.NET
<asp:textbox runat="server" id="TextBox1" onkeyup="javascript:return move(this)" xmlns:asp="#unknown"></asp:textbox>
           <asp:textbox runat="server" id="TextBox2" onkeyup="javascript:return move(this)" xmlns:asp="#unknown"></asp:textbox>
           <asp:textbox runat="server" id="TextBox3" onkeyup="javascript:return move(this)" xmlns:asp="#unknown"></asp:textbox>
           <asp:textbox runat="server" id="TextBox4" onkeyup="javascript:return move(this)" xmlns:asp="#unknown"></asp:textbox>


Hope this helps
 
Share this answer
 
Comments
Aravindba 18-Jul-13 3:36am    
Thank for u reply,but i use onkeyup already,so cant use that one ,if any else there ? and support for all browser also like ie8,ie9,mozila,chrome....?
sumit_kapadia 18-Jul-13 3:52am    
onkeyup works in all browsers. If you are using same event for some other operation (other than move) you can add this function to same as per you logic.
sumit_kapadia 19-Jul-13 12:25pm    
If answer had helped you please "Accept Answer" which marked question as Solved and can help others

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