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>

Edited by vinpkl
Link to comment
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.

Edited by Christian F.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.