Validating an Input

Let's learn how we can validate the input for the phone numbers and handle error messages in forms.

FormControl parameters

The FormControl constructor has three optional parameters.

The first is a default value for the form element. The type of this parameter is also used to inform Angular about what sort of form element to expect to be attached to this FormControl.

The second and third parameters are arrays that contain the synchronous and asynchronous validation logic, respectively, for this individual element.

Validating forms

Each validation rule is a function that is given the form control element (this is where we use the AbstractControl that was imported earlier) and then returns either null, if there’s no error, or an object containing details about what’s gone wrong.

Get hands-on with 1200+ tech skills courses.