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

JavaScript

Answered

answered

1 Answer

Hey 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;
    }

Examples of validation:

marcus@ returns false

marcus@b returns false

marcus@b.b returns false

marcus@bob.b returns false

marcus@b.bo returns true

marcus@bob.bo returns true

Each 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.

Another 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):

!function() {
//validation code here
}();

I'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.

On 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.

Yeah, you're absolutely right. The first time I saw that the HTML5 email input accepted addresses like foo@bar, I was like, "whaaaat?!", though. xD

HTML5 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!