I've been looking at different threads about how to do this and got the following script together, however, even when I type into the inputs so they're not empty, I'm getting the alert pop up and the form doesn't submit.
Can anyone see what I'm doing wrong or if there's a simpler / more efficient way of doing this?
Thanks,
Osu
HTML:
<form action="#" method="post" class="signup-form">
<div class="required">
<label for="name">Name:</label>
<input type="text" name="cm-name" id="name" value=""><br />
</div>
<div class="required">
<label for="asd-asd">Email:</label>
<input type="text" name="cm-asd" id="asd-asd" value="">
<span class="btn"><input type="submit" name="submit" value="" id="signup"></span>
</div>
</form>
JS:
// Validate form fields in Sign up form
$(".signup-form").submit(function(){
var isFormValid = true;
$(".signup-form .required input").each(function(){
if ($.trim($(this).val()).length == 0){
$(this).parent().addClass("highlight");
isFormValid = false;
} else {
$(this).parent().removeClass("highlight");
}
});
if (!isFormValid) alert("Please fill in all the required fields (highlighted in red)");
return isFormValid;
});