Correct Login Form AutoFocus





10
Date Submitted Mon. May. 1st, 2006 8:26 AM
Revision 1 of 1
Coder mattrmiller
Tags Focus | Form | JavaScript | Password | Username
Comments 0 comments
This is a correct way to do login form auto-focus, checking to see if either username or password have focus already. This prevents someone from typing a username hitting TAB, then having JavaScript chime in, resetting focus to Username while user is typing in their password. In this case, the password is appended to the username which is in plaintext.


<html>
        <body onload="setFocus();">
       
                <script>
                        var bUsernameHasFocus = false;
                        var bPasswordHasFocus = false;
               
                        function setFocus()
                        {
                                // Set focus to username
                                if (bUsernameHasFocus == false && bPasswordHasFocus == false)
                                {
                                        document.getElementById("username").focus();

                                }
                        }
                </script>
               
                <form action="#" method="post" name="loginForm" id="loginForm">
                        <input name="username" onfocus="bUsernameHasFocus = true;" onblur="bUsernameHasFocus = false;" type="text" id="username" value="">
                        <input name="password" onfocus="bPasswordHasFocus = true;" onblur="bPasswordHasFocus = false;" type="password" id="password" value="">
                </form>
               
        </body>
</html>
 

Matthew R. Miller

www.bluecreststudios.com
=================
Matthew R. Miller

http://bluecreststudios.com
http://www.codeandcoffee.com

Comments

There are currently no comments for this snippet.

Voting