Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
garyguermantokman
21,097 PointsAnswered
answered
1 Answer
Marcus Parsons
15,719 PointsHey Gary,
You can use this function I use for validation in my contact form which is located here (mine is slightly modified). Just pass in the value of the email field to the function to check it. It uses a regular expression to test the email against valid standards of an email address including characters used and character length:
function isEmailValid (email) {
var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
var test = filter.test(email);
if (test) {
return true;
}
return false;
}
Marcus Parsons
15,719 PointsMarcus Parsons
15,719 PointsExamples of validation:
marcus@returnsfalsemarcus@breturnsfalsemarcus@b.breturnsfalsemarcus@bob.breturnsfalsemarcus@b.boreturnstruemarcus@bob.boreturnstrueEach email must have at least one character before the @, an @ symbol, one character after the @, and two characters after the dot after the @ symbol. All characters must be valid characters that can be used in an email address. This is in line with valid domain references and valid, acceptable characters of emails.
Marcus Parsons
15,719 PointsMarcus Parsons
15,719 PointsAnother key point I want to mention is that when using client side validation in your JavaScript, you'll want to separate any variables you might create from the global namespace so that they can't be easily altered in the console. You can do this by wrapping your validation code in an anonymous self-invoking function expression like this (there are several ways to do this):
Ryan Field
Courses Plus Student 21,242 PointsRyan Field
Courses Plus Student 21,242 PointsI'm not sure what the original question was, but it's worth noting that addresses such as
foo@bar(without top level domains) are valid email addresses, albeit somewhat rare.Marcus Parsons
15,719 PointsMarcus Parsons
15,719 PointsOn the public internet, there aren't any domains without a TLD. If you wanted to validate local emails for a private SMTP server, then sure, you would want to refine the regex here to validate emails without a TLD. But since this is for internet based communication, this regex does its job.
Ryan Field
Courses Plus Student 21,242 PointsRyan Field
Courses Plus Student 21,242 PointsYeah, you're absolutely right. The first time I saw that the HTML5
emailinput accepted addresses likefoo@bar, I was like, "whaaaat?!", though. xDMarcus Parsons
15,719 PointsMarcus Parsons
15,719 PointsHTML5 be cray cray ^_^ But seriously, email validation really is particularly difficult. All the regex does is make sure it doesn't violate character usage and certain lengths, but it doesn't mean the email is real haha Still, it helps to weed out spam, and you know contact forms are notorious for getting spam-a-lammed!