Validating all form fields and know all is valid before submitting
I normally do my validation onsubmit but this time I was going to use jquery to speed things up. I added the validation to each input onblur when loading and it works fine but I don't know how to determine if all inputs are valid before submitting when doing it this way. I know HTML 5 supports this but I'm not looking to use HTML5 fully. There has to be a better way to do this or is there a way to make this work with what I have? Thanks
- $('.input').blur(function() {
val = $(this).val();
if(!$(this).attr('required')) {
return false;
}
if(!val.length) {
$(this).closest('dl').addClass('error');
return false;
}
switch ($(this).attr('type')) {
case 'email':
if(!validateEmail(val)) {
$(this).closest('dl').addClass('error');
return false;
}
break;
case 'phone':
if(!validateUSPhone(val)) {
$(this).closest('dl').addClass('error');
return false;
}
break;
case 'zip':
if(!validateUSZip(val)) {
$(this).closest('dl').addClass('error');
return false;
}
break;
case 'numeric':
if(!validateNumeric(val)) {
$(this).closest('dl').addClass('error');
return false;
}
break;
}
$(this).closest('dl').removeClass('error');
});