quma
quma

Reputation: 5753

angular 5 - email validator

I use this statement in order to validate the email:

email: ['', [Validators.email, Validators.minLength(2)]]

It is not necessary in my application to enter an email. Is it possible to specify something like Validators.notRequired?

Upvotes: 1

Views: 1038

Answers (1)

Suraj Rao
Suraj Rao

Reputation: 29635

The issue seems to be Validators.email didn't allow optional input. It seems to be fixed in angular 6.0.0-beta4.

You can try workaround discussed here.

function emailOrEmpty(control: AbstractControl): ValidationErrors | null {
    return control.value === '' ? null : Validators.email(control); 
 }

And your field:

email: ['', [this.emailOrEmpty, Validators.minLength(2)]]

Upvotes: 2

Related Questions