Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@Paden 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
You can’t perform that action at this time.