Created
December 8, 2011 05:15
-
-
Save sbussard/1446185 to your computer and use it in GitHub Desktop.
quick email validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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);