vinpkl Posted September 30, 2012 Share Posted September 30, 2012 (edited) hi all if both login and password fields are empty then after click of ok button of alert box, the focus should go to login input field, but it goes of password input field <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> <script type="text/javascript"> function isFormValid() { var retVal = true; var errorMsg = ""; if(document.loginform.username.value=='') { errorMsg = "Must Enter Login-ID\n"; document.loginform.username.focus(); retVal = false; } if(document.loginform.userpassword.value=='') { errorMsg = errorMsg + "Password is required\n"; document.loginform.userpassword.focus(); retVal = false; } if (!retVal) { alert(errorMsg); } return retVal; } </script> </head> <body> <form id="loginform" name="loginform" method="post" onsubmit="return isFormValid();" action=""> <p><label>Enter User Id</label><input type="text" id="username" name="username" class="logininput" /></p> <p><label>Enter Password</label><input type="password" name="userpassword" id="userpassword" class="logininput" /></p> <input type="submit" value="submit" class="login_bt" name="login_submit" /></p> </form> </body> </html> Edited September 30, 2012 by vinpkl Quote Link to comment Share on other sites More sharing options...
Christian F. Posted September 30, 2012 Share Posted September 30, 2012 (edited) That is what you've told the code to do, as it checks the password status after the username field. Then, if there is no password, you're setting the focus to the password field. Without checking any previous status. Easiest solution is to check the retVal status, before setting the focus in the password field. Edited September 30, 2012 by Christian F. Quote Link to comment Share on other sites More sharing options...
vinpkl Posted September 30, 2012 Author Share Posted September 30, 2012 thanks chris i will modify it accordingly vineet Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.