Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Very simple email validation, need to make sure that there are 2 or more characters after the LAST period

Tags:

regex

firebase

the program I'm using has its own email validation built in and it only accept emails where the last group of characters after the LAST period is 2 or more. I have this regex statement so far:

/^[^@]+@[^@]+\\.[^@]+$/i

This is acceptable for what I'm trying to do, not trying to be super fancy here. Just want to check:

  • There is an @ sign with something before it that is not an @ sign
  • There is something before the last .
  • There are at least 2 characters after the last .

I'm just not sure how to test for the LAST . as opposed to ANY . after the @ sign.

Any help?

like image 481
shan Avatar asked Dec 06 '25 06:12

shan


1 Answers

You can impose this check at the beginning with a positive look-ahead:

/^(?=.*\.[^.]{2,}$)[^@]+@[^@]+\.[^@]+$/i
  ^^^^^^^^^^^^^^^^

See demo

The (?=.*\.[^.]{2,}$) look-ahead will find the last . and then will try to match 2 or more characters other than a dot before the end of string. If there is fewer (just 1) the match will be failed (there will be no match).

UPDATE

As you might be unable to use the regex having ^/$ anchors in other places than start/end of string, here is another solution:

^[^@]+@[^@]+\.[^@.]{2,}$

See demo

Regex breakdown:

  • ^ - start of string
  • [^@]+ - 1 or more characters other than @
  • @ - literal @ symbol
  • [^@]+ - 1 or more characters other than @ as many as possible (thus, no [email protected] is possible)
  • \. - a literal dot
  • [^@.]{2,} - 2 or more characters other than @ and . up to...
  • $ - end of string
like image 115
Wiktor Stribiżew Avatar answered Dec 08 '25 18:12

Wiktor Stribiżew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!