Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
$('.form').submit(function() {
var valid = true;
var errors = '';
$(this).find('.required').each(function() {
if ($(this).val() == '') {
$(this).addClass('field-error');
errors += 'The "' + $(this).attr('title') + '" field is required\n';
if (valid) {
$(this).focus();
}
valid = false;
} else {
$(this).removeClass('field-error');
}
});
$(this).find('.email').each(function() {
var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
if (!pattern.test($(this).val())) {
$(this).addClass('field-error');
errors += 'The email address you entered is not valid\n';
if (valid) {
$(this).focus();
}
valid = false;
} else {
$(this).removeClass('field-error');
}
});
if (!valid) {
alert(errors + '\n ');
}
return valid;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment