3

To allow only emails with TLD (ending with .de or .com) I want to use the following pattern:

^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-zA-Z]{2,4}$

I tested this regular expression on regexr.com a couple of times and it worked good, for example it did not match with test@test.

But the Angular Validator says no error for test@test with this pattern Validator:

Validators.pattern('^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-zA-Z]{2,4}$')

How is that possible?

1
  • try Validators.pattern(/^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-zA-Z]{2,4}$/) Commented Nov 24, 2018 at 20:00

1 Answer 1

4

You have to escape the backslash, since it's a string.

'^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-zA-Z]{2,4}$'
Sign up to request clarification or add additional context in comments.

Comments