Jump to content

Focus Should Go Into Username Input


vinpkl

Recommended Posts

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.