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

Joel Buzzanco
PLUS
Joel Buzzanco
Courses Plus Student 2,738 Points

How could you make browser know you left out @ or .com for email addresses?

This may be a question that is too advanced for the scopes of this course, but I thought I should ask.

I've noticed that some forms on websites/ apps can tell when you are signing in that you had a typo such as--you missed the @ symbol or didn't put .com or .net or . (dot) at the end.

For example, say my email is joel@gmail.com , and I put to sign in : joelgmail.com , or, joel@gmail , I think some websites/apps can tell you left one or more of the key characters out.

How can they tell?

It shows in the video how to get the browser to tell if the field is left blank, but what if there actually is the text in there, they just left some of those key characters out.

2 Answers

You'll want to check the email addresses against RFC 2822, which describes the syntax an email address must follow.

Looking at Stack Overflow's answer to a similar question, it looks like this would be a good way to validate;

function validateEmail(email) {
    var re = [a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)\b;
    return re.test(email);
}

This function should filter out any email addresses that aren't formatted correctly, and also any that have an incorrect or gibberish top level domain.

Joel Buzzanco
Joel Buzzanco
Courses Plus Student 2,738 Points

Thanks, wow that was more complex than I was thinking. I also looked at MDN's page on Regular Expressions and that seemed to help too.

It seems like the more I learn about JavaScript and Web Development, the more I feel like I know basically nothing... I guess just keep going though and eventually things won't seem so intimidating. Thanks again anyways.

You are absolutely dead-on with that sentiment, Joel; The more you learn, the more you realize is out there to learn. Try not to be overwhelmed by that, though, because you're right too that as long as you keep pushing yourself, you'll end up being more and more comfortable until you're an expert!

My mantra is, "The only bad coder is the one who gives up while they're still bad at coding."

Jeff Lemay
Jeff Lemay
14,268 Points
  • Check if an @ symbol exists
  • Check if there are characters before the @ symbol
  • Check if there are characters after the @ symbol
  • Check if there is a .dot somewhere after the @ symbol
  • Check if there are characters after the .dot