Reputation: 1
What can be the pattern for email validation with strict error even if I type - "[email protected]".
emailPattern = '^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,4}$';
initLoginForm() {
this.loginForm = this.formBuilder.group({
email: ['', Validators.compose([Validators.required, Validators.pattern(this.emailPattern)])],
password: ['', Validators.required]
});
}
But it does not error if I type ".co" instead of ".com". Can someone please tell what should be the regex exp to have this error too.
Upvotes: 0
Views: 3818
Reputation: 104
'.co' is a valid Internet country code top-level domain assigned to Colombia.
If you'd like to have a validator that is filtering out non-existent domains, that should be a back-end method because front-end solely is not capable of MX lookup.
In addition, I'd suggest using validator.js instead of writing your own regex.
Upvotes: 0
Reputation: 739
this regex can be helpful If you want to use it for email patterns in angular validation.
emailRegExp = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
I hope It can be helpful
Upvotes: 0
Reputation: 19474
Its because regexp allow it .[a-z]{2,4}$
this means that user can type from 2 to 4 characters in the end after dot.
Regexp will test only patterns but it does not have any other logic, so if your expectation is to make sure that domain exists then i would not recommend to do it.
This is why many platforms have email validation so user need to validate their email before account become active.
Upvotes: 2