vinpkl Posted September 30, 2012 Share Posted September 30, 2012 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> Link to comment https://forums.phpfreaks.com/topic/268935-focus-should-go-into-username-input/ Share on other sites More sharing options...
Christian F. Posted September 30, 2012 Share Posted September 30, 2012 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. Link to comment https://forums.phpfreaks.com/topic/268935-focus-should-go-into-username-input/#findComment-1381898 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 Link to comment https://forums.phpfreaks.com/topic/268935-focus-should-go-into-username-input/#findComment-1381899 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.