-1

I have created a regular expression for email validations.

var regex = "^([a-zA-Z]+([\.-_]?[a-zA-z0-9]+)*)\@([a-zA-Z0-9]+)([-][0-9a-z]+)?\.([a-z-]{2,20})(\.[a-z]{2,3})?$"

To match emails:

1. update@update 
2. mohit.bhagat@B-9com.com
3. mohit.Bhagat@us.thalesgroup.com
4. mohit@gmail.com.com.com

If you run this over online website 1 and 4th will fail while 2, 3 will pass.

But when I run this code in Javascript( Browser console ), 1st also passes the validation.

I am using Angular application.

Mohit Bhagat
  • 245
  • 5
  • 14
  • Can you please tell what is wrong in my written code? – Mohit Bhagat Jul 24 '20 at 15:19
  • Yes, the [regex isn't this one](https://stackoverflow.com/a/201378/542251). – Liam Jul 24 '20 at 15:45
  • The best way to validate an email address is to send an email and check the return value. 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 Jul 24 '20 at 16:24

1 Answers1

-1

The problem is how JS ignores \. If you do following

var regex = "[a-z]+@[a-z]+\.[a-z]{2,3}"

Resultant string stored is

"[a-z]+@[a-z]+.[a-z]{2,3}"

And if you do this

 var regex ="[a-z]+@[a-z]+[\.][a-z]{2,3}"

Resultant string stored is

"[a-z]+@[a-z]+[.][a-z]{2,3}"

Pay attention to [.] with this now i was able to get validation error for 1st email.

Complete regex: "^([a-zA-Z]+([-_.]?[a-zA-Z0-9])*)@([a-zA-Z0-9]+([-][a-z0-9A-Z]+)?)[.]([a-z]+[-_]?[a-z]+)([.][a-z]{2,3})?$"

Update: Or you can do this: var regex ="[a-z]+@[a-z]+\\.[a-z]{2,3}"

As mentioned in comments below, you can do this way to consider . by including it in [.], but its a hack and not origional way to do it. Best way to have . included in your regex is \.

Mohit Bhagat
  • 245
  • 5
  • 14
  • 1
    `.` and `\.` do VERY different things. Your issue isn't adding a group `[]` it's that you [need to escape the backslash](https://stackoverflow.com/questions/3903488/javascript-backslash-in-variables-is-causing-an-error), try the same regex but either use `\\.` or backticks. – Liam Jul 27 '20 at 07:57
  • okay so is this \. and [.] will be different ? – Mohit Bhagat Jul 27 '20 at 08:41
  • yes [`\.` is a literal `.`](https://stackoverflow.com/questions/10397968/escape-dot-in-a-regex-range). [`[.]` is a group](https://learn.microsoft.com/en-us/dotnet/standard/base-types/character-classes-in-regular-expressions#positive-character-group--) containing a dot. Your using a group to escape because you need to [escape the backslash](https://stackoverflow.com/questions/8618374/escaping-backslash-in-string-javascript) in your string as in JS `\ ` is reserved in strings. so the actual correct thing to do is `"\\."` – Liam Jul 27 '20 at 08:46
  • Its worth noting from the link you have shared is "Remember that the dot is not a metacharacter inside a character class, so we do not need to escape it with a backslash." – Mohit Bhagat Jul 27 '20 at 10:28
  • Yes but your adding it to a character class in order to escape it. You escape a dot using `\ ` you use `[]` to create groups. The fact that `[]` also escapes it is just a side effect. Anyone looking at your regex is going to be confused by the `[.]`. Essentially this is an unnecessary hack, there is a proper way to escape the dot and you should use that – Liam Jul 27 '20 at 13:03