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

HTML

marsha grasett
marsha grasett
9,995 Points

Patterns for Email Validation. yay or nay?

I have used one in a form but now am reading: http://html5pattern.com/ which states:

Please do not use a pattern for email validation. Every regular expression for email validation is missing something. Lots of people gone through a process with the conclusion, that it is nearly impossible to get a perfect validation. The good part about is, we can leave this to our browser developers. They got it quite right and we should use their standard for email validation now.

<input type="email" name="" value="" required />

1 Answer

James Barnett
James Barnett
39,199 Points

As with all HTML5 features, support for type=email varies from browser to browser. So I'm not sure that relying on browser devs is the best idea.

Always remember the first rule of form validation, validate server-side in addition to client-side. So you are still going to need a regex for your PHP (or whatever you are using server side.)

Next question, what is your goal of validating an email address client side. Are you just creating a basic sanity check. For that I'd suggest: pattern="[^@]+@[^@]+\.[a-zA-Z]{2,6}"

If your goal is to make sure your the email addresses you collect are actually valid, maybe because you send out a large-scale mailing list and are charged per email address you can go for a more heavy handed solution.

There's mailcheck.js or verimail.js they will both check for common typos in email addresses.

If you want to match every possible valid email address (on principle* then try:

/^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i

source: http://www.mi-ange.net/blog/msg.php?id=79&lng=en