PDA

View Full Version : Problem with login javascript validation



keyboard
10-15-2011, 10:30 PM
Hello everyone,

I'm using some simple javascript to display an error if they have left either the username or password field empty. Here's the code


<script type="text/javascript">
function validateForm()
{
var x=document.forms["myForm"]["username"].value;
if (x==null || x=="")
{
document.getElementById('boldStuff').innerHTML = 'Username must be filled out';
return false;
}

var z=document.forms["myForm"]["pass"].value;
if (z==null || z=="")
{
document.getElementById('boldStuff4').innerHTML = 'Password must be filled out';
return false;
}
}
</script>

<h1>Login</h1>
<form action="check.php" onsubmit="return validateForm()" name="myForm" method="post">
<div class="form_left">
Username:
</div>
<div class="form_right">
<input name="username" type="text" maxlength="20" />
</div>
<div class="clear"></div>
<div class="form_left">
Password:
</div>
<div class="form_right">
<input name="pass" type="password" maxlength="20" />
</div>
<div class="clear"></div>
<div class="form_left">&nbsp;</div>
<div class="form_right">
<input type="submit" value="Login" />
</div>
<div class="clear"></div>
</form>


<span id='boldStuff'></span>
<span id='boldStuff4'></span>


It works fine if they only left the password or the username box empty, but if they leave both empty, it only displays the "Username must be filled out" when I would like it to display both messages. Any help