Skip to content

Instantly share code, notes, and snippets.

@sbussard
Created December 8, 2011 05:15
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 sbussard/1446185 to your computer and use it in GitHub Desktop.
Save sbussard/1446185 to your computer and use it in GitHub Desktop.
quick email validation
function validate(name, email) {
var errors = [];
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
if(name === '') {
errors.push('You forgot to enter your name');
}
if(email === '' || !emailReg.test(email)) {
errors.push('Your e-mail address is invalid');
}
if(errors.length) {
throw errors.join('\n');
}
}
$(function(){
$('form').submit(function(event){
var name = $(this).children('input[name="name"]').val();
var email = $(this).children('input[name="email"]').val();
try {
validate(name,email);
console.log('success');
} catch (err) {
alert(err);
event.preventDefault();
return false;
}
});
});
@Paden
Copy link

Paden commented Aug 29, 2012

I have a rule: Store a jQuery object in a var if used more than once. There's no point in creating more jQuery overhead.

var $self =. $(this);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment