There is an email validator in the @angular/forms.
You can get the regex from the email validators source :
const EMAIL_REGEXP =
/^(?=.{1,254}$)(?=.{1,64}@)[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+(\.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+)*@[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?(\.[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?)*$/;
Or simply use it to create a custom validator for multiple emails :
function validateEmails(emails: string) {
return (emails.split(',')
.map(email => Validators.email(<AbstractControl>{ value: email }))
.find(_ => _ !== null) === undefined);
}
function emailsValidator(control: AbstractControl): ValidationErrors | null {
if (control.value === '' || !control.value || validateEmails(control.value)) {
return null
}
return { emails: true };
}
Then use the custom validator :
this.sendEmailForm = this.fb.group({
'toAddress': ['', emailsValidator]
});
Here is a fork of your code with the suggestion solution.