Manu Chadha
Manu Chadha

Reputation: 16723

Validators.email directive not working correctly

I am using the Validators.email directive (https://angular.io/api/forms/EmailValidator) to check that the structure of the email id is correct. However, I notice that it passes hello.com@com input. Is this expected behavior? The code example is at https://stackblitz.com/edit/angular-xesgxe

Check createForm() function in signup-component.component.ts

Upvotes: 2

Views: 610

Answers (1)

Sudarshana Dayananda
Sudarshana Dayananda

Reputation: 5265

As enf0rcer said in his comment it is the correct behaviour. But if you are not satisfied with it you can use below code in your signup-component.component.ts.

export class SignupComponentComponent implements OnInit{

  EMAIL_PATTERN = /^(([^<>()\[\]\\.,;:\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,}))$/;

  constructor() {}

  createForm(){
    this.signupForm = this.fb.group({
      email:[null,[Validators.required,Validators.pattern(this.EMAIL_PATTERN)]],
    });
  }
}

See the updated example.

Upvotes: 1

Related Questions