-1

I've taken the regular expressions from a website some time ago, and now I'm trying to fix some issues but I'm terrible with regular expressions, I'm just learning, any help would be appreciated.

What am I doing wrong here?

function validateEmail(email) {

    var reg1 = /^([a-zA-Z0-9])+\@([a-zA-Z0-9])+\.+([a-zA-Z0-9])+$/; // example@mail.com
    var reg2 = /^([a-zA-Z0-9])+\@([a-zA-Z0-9])+\.+([a-zA-Z0-9])+\.+([a-zA-Z0-9])+$/; // example@mail.test.com
    var reg3 = /^([a-zA-Z0-9])+\@([a-zA-Z0-9])+\.+([a-zA-Z0-9])+$/; // example.test@mail.test.com
    var reg4 = /^([a-zA-Z0-9])+\.+@([a-zA-Z0-9])+\.+([a-zA-Z0-9])+\.+([a-zA-Z0-9])+$/; // example.test@mail.com

    return reg1.test(email) || reg2.test(email) || reg3.test(email) || reg4.test(email)
  }

I need to accept mails in this formats:

example@mail.com
example@mail.test.com
example.test@mail.test.com
example.test@mail.com

reg1 and reg2 are working, reg3 and reg4 are not working.

Last edit:

I've tried to fix it like this, didn't work properly neither:

function validateEmail(email) {

    var reg1 = /^([a-zA-Z0-9])+\@([a-zA-Z0-9])+\.+([a-zA-Z0-9])+$/; // example@mail.com
    var reg2 = /^([a-zA-Z0-9])+\@([a-zA-Z0-9])+\.+([a-zA-Z0-9])+\.+([a-zA-Z0-9])+$/; // example@mail.test.com
    var reg3 = /^([a-zA-Z0-9])+\.+([a-zA-Z0-9])+\@([a-zA-Z0-9])+\.+([a-zA-Z0-9])+\.+([a-zA-Z0-9])+$/; // example.test@mail.test.com
    var reg4 = /^([a-zA-Z0-9])+\.+([a-zA-Z0-9])+\@([a-zA-Z0-9])+\.+([a-zA-Z0-9])+$/; // example.test@mail.com

    return reg1.test(email) || reg2.test(email) || reg3.test(email) || reg4.test(email)
  }
sisorken
  • 63
  • 6

2 Answers2

1

You can validate those simple email formats with a single regex:

^[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)?@[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+){1,2}$
  • ^[a-zA-Z0-9]+ - start with one or more alphanumeric chars
  • (?:\.[a-zA-Z0-9]+)? - optionally allow a period followed by one or more alphanumeric chars
  • @ - require the @ symbol
  • [a-zA-Z0-9]+ - require one or more alphanumeric chars
  • (?:\.[a-zA-Z0-9]+){1,2}$ - require one or two more sequences of a period followed by alphanumeric chars to end the string

https://regex101.com/r/wREGo4/1

MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
  • That's an answer that I can finally understand and meets my requirements. I appreciate it. Thank you. – sisorken Nov 22 '19 at 20:42
  • @sisorken You're welcome. I know regex can be cryptic especially when copy+paste+praying from a random site :) – MonkeyZeus Nov 22 '19 at 20:45
  • 1
    @sisorken I'm sorry to hear that but I'm glad you are recovering. regex101 is a really superb site to exercise your regex skills and expand your understanding. Much of the issue that I find with regex is not so much "What does this syntax do?" but more so "Why did the person write the syntax in this specific sequence? What scenario are they trying to account for?" Assuming your sample emails were all inclusive then the "What scenario are they trying to account for?" was pre-answered :) – MonkeyZeus Nov 22 '19 at 20:55
0

You don't validate emails beyond the simple

*@*.*

You send a validation email to the address and require them to click the validation link. You WILL get it wrong and lock people out of entering their email to your system.

Adrian Brand
  • 20,384
  • 4
  • 39
  • 60