I'm coding an online application form that requires very basic validation. I have denoted required fields in HTML by adding the class requiredField to each. The script examines each required field, and if it is empty, returns false while adding a message to an error well.
I'm having issues... The script correctly adds the error messages when any field is empty, but if the fields are filled out it refuses to send the message. Can anyone help my understand why the "return true" under my "else" condition is not going through? Any help would be much appreciated.
Thanks!
Here is my validation script:
<script type="text/javascript">
jQuery('#contactForm').submit(function(e) {
jQuery( ".requiredField" ).each(function( index ) {
if( jQuery(this).val().length == 0 ) {
jQuery("#formErrors").show();
jQuery( "#formErrors" ).append( "<p>The <b>" + jQuery(this).attr('placeholder') + "</b> field is required, but was left empty.</p>" );
jQuery(this).addClass('errorField');
jQuery(window).scrollTop(0);
return false;
}
else{
return true;
};
});
});
});
</script>