Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJames Barrett
13,253 PointsJavaScript form returns correct error messages, however form still submits!
Hi,
I have a login form which correctly returns false and displays the error messages on the screen. However this happens only for a split second before the form is actually submitted; which shouldn't happen!
HTML:
<div class="wrapper">
<h1>Login to FootDrive</h1>
<?php if (isset($error_message)) {
echo "<p>".$error_message."</p>";
}
?>
<form action="login.php" name="login" id="name" method="post">
<div class="social-register">
<a href="#"><img class="twitter" src="img/twitter.png"></a>
<a href="#"><img class="facebook" src="img/facebook.png"></a>
</div>
<p class="or">or</p>
<input type="text" id="email" name="user_email" placeholder="Email">
<span id="erroremail" class="error">Please Enter An Email</span>
<input type="password" id="password" name="user_password" placeholder="Password">
<span id="errorpassword" class="error">Please Enter a Password</span>
<button type="submit" value="Submit" onclick="loginValidate();">Login</button>
<a href="#">
<p class="forgot-password">Forgotten your password?</p>
</a>
</div>
</form>
JavaScript:
function loginValidate() {
var error = 0;
var erroremail = document.getElementById("erroremail");
var user_email = document.login.user_email.value;
var atpos = user_email.indexOf("@");
var dotpos = user_email.lastIndexOf(".");
if (user_email === "") {
erroremail.style.visibility = "visible";
error = 1;
} else if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 > user_email.length) {
document.getElementById("erroremail").textContent="Invalid Email Address";
erroremail.style.visibility = "visible";
error = 1;
} else {
erroremail.style.visibility = "hidden";
}
var errorpassword = document.getElementById("errorpassword");
var user_password = document.login.user_password.value;
if (user_password === "") {
errorpassword.style.visibility = "visible";
error = 1;
} else {
errorpassword.style.visibility = "hidden";
}
if (error == 0) {
return true;
} else {
return false;
}
}
Can't put my finger on why the form is submitting? Any suggestions would be great.
Thanks, James.
1 Answer
David Bath
25,940 PointsTwo ideas: You could try this:
<button type="submit" value="Submit" onclick="return loginValidate();">Login</button>
Or you could put the function in the form's submit event handler the same way:
<form action="login.php" name="login" id="name" method="post" onsubmit="return loginValidate()">
I think as it is now the function returns false, but that doesn't mean that value gets applied to the submit event. It's like saying onclick="false", which won't do anything.
James Barrett
13,253 PointsJames Barrett
13,253 PointsLooks like I forgot the return keyword, thanks!