From the course: Angular Essential Training (2019)

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Validation: Custom

Validation: Custom - Angular Tutorial

From the course: Angular Essential Training (2019)

Start my 1-month free trial

Validation: Custom

- [Instructor] Okay, let's build a custom validator. To build your own custom validators, you need to create a function that will receive an object, which angular will pass in as either a form control, form group or form array. And that function needs to return no invalid and return an object if invalid. Let's built a year validator for the year field, which is a form control. In the media dash item dash form dot component dot Ts file, we can create a method named year validator and give it a parameter named control that we can set to a type of form control. We want the year field to be optional. So the first bit of logic we will add is to check if the value is empty and if so, return no telling the control that the field is valid. If it makes it past that, we want to convert the value, which is a string into a number. So we can call parseInt on control dot value and capture it to a local variable. And then we will set…

Contents