-1

I would like to prohibit . in front of the @ in email validation in javascript.

What I have:

^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$

So email.@domain.com is prohibited, but email.email@domain.com should be accepted.

What could I do?

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
Jaczura Zoltán
  • 269
  • 3
  • 16
  • 2
    Why would you "validate" email addresses based on your own loose idea of how a valid email address looks like? See [How can I validate an email address using a regular expression?](https://stackoverflow.com/questions/201323/how-can-i-validate-an-email-address-using-a-regular-expression) – Ted Lyngmo Jan 20 '23 at 20:38
  • what do you mean loose idea? dot is not allowed in front of @ – Jaczura Zoltán Jan 20 '23 at 20:39
  • 2
    Yes, `.` is not allowed in front of `@`. There are many many many many rules about what is a valid regex. Far better to use existing code than rolling your own. – Andy Lester Jan 20 '23 at 20:44
  • My email has . in front of the @ and I send and receive emails all the time. – Patrick87 Jan 26 '23 at 13:52

1 Answers1

0

Here is slightly modified version of your regex. Basically, ^([a-zA-Z0-9_\-\.]+) was changed to ^([a-zA-Z0-9_\-]+)(\.[a-zA-Z0-9_\-]+)*:

^([a-zA-Z0-9_\-]+)(\.[a-zA-Z0-9_\-]+)*@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$
Mike Mozhaev
  • 2,367
  • 14
  • 13