I'm trying to send an alert saying "Thanks for Registering" after someone fills out my form correctly, but even if there is a mistake in filling out the form, sometimes it still says "Thanks for Registering". I'm using jQuery.
Can someone tell me what they think?
http://codepen.io/dpcarlson/pen/nvwqk
HTML:
<h2>Form</h2>
<h1>Validator</h1>
<div>
<p id="labelforusername">Your Name</p>
</div>
<div>
<input type="text" id="username" >
</div>
<div>
<p id="labelforpassword">Your Password</p>
</div>
<div>
<input type="password" id="password" >
</div>
<div>
<p id="labelforverify">Re-Enter Your Password</p>
</div>
<div>
<input type="password" id="verify" >
</div>
<div>
<p id="labelforemail">Your Email</p>
</div>
<div>
<input type="text" id="email" >
</div>
<div id="submit">SUBMIT</div>
JS:
$(document).ready(function () {
//Validate Form **********************
$('#submit').click(function() {
if($('#username').val().length === 0) {
alert("Please Enter Your Name"), $('#username').css('background','rgba(0,0,600,0.3)');
} else {
$('#username').css('background', 'rgba(0,0,0,0.3)' );
};
if($('#password').val().length === 0) {
alert("Please Enter Your Password"), $('#password').css('background','rgba(0,0,600,0.3)'), $('#verify').css('background','rgba(0,0,600,0.3)');
} else if ($('#password').val().length <= 4) {
alert("Please Make Your Password At Least 5 Characters"), $('#password').css('background','rgba(0,0,600,0.3)'), $('#verify').css('background','rgba(0,0,600,0.3)');
} else {
$('#password').css('background', 'rgba(0,0,0,0.3)'),
$('#verify').css('background', 'rgba(0,0,0,0.3)');
};
if($('#password').val() !== $('#verify').val()) {
alert("Oops, Seems Like Your Passwords Don't Match!"), $('#password').css('background','rgba(0,0,600,0.3)'), $('#verify').css('background','rgba(0,0,600,0.3)');
};
if($('#email').val().length === 0) {
alert("Please Enter Your Email"), $('#email').css('background','rgba(0,0,600,0.3)');
} else {
$('#email').css('background', 'rgba(0,0,0,0.3)');
};
if ($('#password').val().length > 4 &&
$('#password').val().length !== 0 &&
$('#verify').val().length > 4 &&
$('#verify').val().length !== 0 &&
$('#username').val().length > 0 &&
$('#username').val().length !== 0 &&
$('#email').val().length !== 0 &&
$('#email').val().length > 0 &&
$('#password').val() === $('#verify').val()){
alert("Thanks For Registering!")
}
});
//End Validate Form **********************
Cheers.
It is because of the way your if statement is structured. In the current design, an expression is used with a list of expressions in order to determine a logical state. However, it doesn't build when a comma delineated list is used in an expression, it just evaluates one at a time.
As a result, the last value is the one which is compared with true in your if statement. That means something this alerts true
if((false,false,false,true)===true)alert("true");
So really in order to show the "Thanks for Registering" alert all that has to be true is
$('#password').val() === $('#verify').val()
when in reality you probably wanted this
if ($('#password').val().length > 4 &&
$('#password').val().length !== 0 &&
$('#verify').val().length > 4 &&
$('#verify').val().length !== 0 &&
$('#username').val().length > 0 &&
$('#username').val().length !== 0 &&
$('#email').val().length !== 0 &&
$('#email').val().length > 0 &&
$('#password').val() === $('#verify').val()){
alert("Thanks For Registering!")
}