Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular pattern Validator is not working as expected

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 Answers

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

'^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-zA-Z]{2,4}$'
like image 186
Andy Ray Avatar answered Apr 19 '26 11:04

Andy Ray



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!