Skip to content

Instantly share code, notes, and snippets.

@magadanskiuchen
Created January 30, 2013 06:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magadanskiuchen/4671302 to your computer and use it in GitHub Desktop.
Save magadanskiuchen/4671302 to your computer and use it in GitHub Desktop.
$('.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