0

I am trying to make a regex e-mail checker. Here is my code:

document.onkeyup = function(event)
{
  userID = $("#email").val();
    if (userID.match("[a-zA-Z\d]+\@[a-zA-Z\d]+\.[a-zA-Z\d]+"))
{
    $("#emCheck").text("Thank you for your email address!");
    $("#emCheck").css("color", "black");
  }
  else
  {
    $("#emCheck").text("Please enter a valid email.");
  }

}

What's odd is this seems to work for the @ sign, but not the dot. "dennis" is not a valid e-mail, but "dennis@yahoo" is. Why is this? Why does the @ sign work but the dot does not?

DennisM
  • 359
  • 1
  • 3
  • 13
  • I'm trying to reproduce it in regexr but it looks like it works as intended so I'm not sure if the expression is wrong. https://regexr.com/4j15a – aadibajpai Aug 10 '19 at 21:42
  • 4
    Rewrite `"` in regex to `/` – Dominik Matis Aug 10 '19 at 21:42
  • Dominik's suggestion worked! – DennisM Aug 10 '19 at 21:47
  • 1
    In JS, there are 2 ways to compile a regex object `new RegExp("---")` or just `/---/`. Also, metachars need to be escaped in the windows programming JS world, so `"\\d"` is a regex digit character. IF you use the `/--/` notation, then you don't need to escape metachars. –  Aug 10 '19 at 23:04
  • Please, have a look at these sites: [TLD list](https://www.iana.org/domains/root/db); [valid/invalid addresses](https://en.wikipedia.org/wiki/Email_address#Examples); [regex for RFC822 email address](http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html) – Toto Aug 11 '19 at 10:41

0 Answers0