views:

92

answers:

3

I'm building a password box that holds three text areas. Each text area has one character. After I type the first character of the password, I have to press tab or use the mouse to get to the second text area to type the second character of the password. I would like to make this happen automatically (cursor movement) just right after I type in the first text area.

how can I achieve this ?

If you may ask, I'm using Visual Studio .NET 2008 in C# I'm a perfect newbie in .net and I don't know how to ask this question with the appropiate words.

Thank you.

+3  A: 

Try onKeyPress. That should take care of what you are looking for.

<input type="text" name="password" onKeyPress="autoTab()" />

<script type="text/javascript" language="JavaScript">
   function autoTab() {
     //do stuff
   }
</script>

Here's a tutorial that deals with changing the cursor position of a field.

http://www.webdeveloper.com/forum/showthread.php?t=91817

This suggests your autoTab() function should look like this.

function autoTab(field,nextFieldID){
  if(field.value.length >= field.maxLength){
    document.getElementById(nextFieldID).focus();
  }
}
Robert Greiner
Thank you for your help. I appreciate it.Problem is, in my case, it is not an input field.As it is written in .net , we're using a line just like this:<asp:TextBox runat="server" CssClass="yay" Align="right" TextMode="Password" Font-Size="Small" ID="txtPwd1" MaxLength="1" Width="10px" style="margin-left: 0px"></asp:TextBox>
UXdesigner
You should be fine with that, just put your `onKeyPress="autoTab()"` element in that asp:TextBox and use `txtPwd2` is the input to autoTab() when you go from field 1 to field 2.
Robert Greiner
A: 
if textbox1.text.length > 0 then textbox2.focus();
Laplace
+1  A: 

Are you using jQuery in your project?

<!DOCTYPE html>
<html>
<head>
<title>example</title>
<style type="text/css">
    *
    {
        margin: 0;
        padding: 0;
    }
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
<script type="text/javascript">

    $(function () {

        $('#data1').keyup(function () {

            if ($(this).val().length == 1) {

                $('#data2').focus();
            }

        });

    });

</script>
</head>
<body>
<input id="data1" type="text" value="" style="width: 10px" /><br />
<input id="data2" type="text" value="" style="width: 10px" />
</body>
</html>
aherrick
Even if you're not: `document.getElementById('data1').onkeyup = function() { if (this.value.length == 1) document.getElementById('data2').focus(); };`
nickf
I tried this solution and it works. Now I've got to try it with .NET to see if it does the same. .NET is wacky. I'm a newbie in this framework.
UXdesigner