Angular: Validators.email invalid if empty
eric2783's solution is pretty good, however - if, in future, the output of Validators.email
will change, then this custom validator will become not compatible with the angular's validator output.
Here's what you should do to keep the compatibility:
private customEmailValidator(control: AbstractControl): ValidationErrors {
if (!control.value) {
return null;
}
return Validators.email(control);
}
The best solution that i found was this:
<input type="email" name="email" [(ngModel)]="model.Email" [email]="model.Email!='' && model.Email!=null">