How to validate email in angular 4 using formgroup or custom regexp?



How to validate email in angular 4 using formgroup or custom regexp?..

Answer / Digvijay Bisht

To validate an email input field in Angular 4, you can use FormGroup and a custom validator function. First, define the FormGroup for your form:
```typescript
emailFormControl = new FormControl('', [Validators.required, this.validateEmail]);
```
Create a custom validateEmail function to check the email format using a regular expression:
```typescript
validateEmail(control: AbstractControl) {
const regex = new RegExp(/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$/);
return (regex.test(control.value)) ? null : {'invalidEmail': true};
}
```

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More AngularJS Interview Questions

What is bazel in angular 8?

2 Answers  


What selector forces a style down through the child component tree into all the child component views?

1 Answers  


Difference betweek javascript, jquery and angularjs?

1 Answers  


What sre the directives in angularjs?

1 Answers  


What is route guards?

1 Answers  


What is ngmodel in angular?

1 Answers  


What do double curly brackets are used in angular 5?

1 Answers  


How to load select box during page initialization using AngularJS?

1 Answers  


Explain the procss about angular project bootstrap?

1 Answers  


What is difference between javascript and angular?

1 Answers  


What is impure pipe?

1 Answers  


List out the differences between config and run methods in angularjs?

1 Answers  


Categories