Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular email validator with whitespaces

I'm developing a new Angular based application, with a login form based on email and password.

It have a form for these fields, defined by the following lines:

let formConfig = {
  email: ['', [Validators.required, Validators.email]],
  password: ['', [Validators.required]],
};

this.form =  this.formBuilder.group(formConfig)

It works just as expected, except for one situation:

When the user uses Samsung's (and others) autocompletes to fill the email field, it inserts an empty space after the email, and the Validators.email assumes that it's an invalid email.

My question is how can I solve this particular situation?

I'm pretty sure that I can just put some existing email validation regex, but I hate to reinvent the wheel, if the validator exists, creating another one looks crazy.

It's possible to implement some kind of validator that modifies my form control value stripping out whitespaces?

like image 665
Elias Soares Avatar asked Jun 05 '26 21:06

Elias Soares


1 Answers

I experienced the same.

For everyone still encountering this issue;

adding type="email" to your <input> in combinitation with Validators.email should do the trick.

like image 73
Fer Bastiaens Avatar answered Jun 07 '26 13:06

Fer Bastiaens