I am trying to validate my form. Below is the jquery. When I click on submit button, if form has <span class="error"> then it should alert faild if not alert success. The problem is it only alert ‘Faild’ whether there is <span class="error"> or not. please look at in jsfiddle
$(".submitBtn").click(function() {
if($("form").find('.error')) {
alert('Faild');
} else {
alert('Success');
//return true;
}
});
$("form").submit(function(e) {
e.preventDefault();
});
<form id=="subscribeForm" method="post" action="">
<input type="text" name="name" class="f-comp t1 name" />
<input type="text" name="age" class="f-comp t2 age" />
<input type="text" name="email" class="f-comp t3 email" />
<textarea name="detail" class="f-comp words"></textarea>
<input type="submit" value="Submit" class="submitBtn" />
<span class="error"></span>
</form>
You’re not doing a boolean operation.
Try this:
Notice that you’re checking the number of items that match class ‘error’, instead of whether you get a return or not.