Adding validations

Dealing with forms is often a pain for developers because you obviously can't trust the inputs provided by the user. It is either because they are just not paying attention to what you expect in your forms or because they want to break things. Validating inputs incoming from a form is painful in every language, both server and client-side.

Now, the Angular team came up with a rather simple way to validate inputs by defining what is expected from each field right at the form's creation using Validators. Angular contains the following built-in Validators that we can use:

  • required: Requires a non-empty value
  • minLength(minLength: number): Requires the control value to have a minimum length of minLength
  • maxLength(maxLength: ...

Get Expert Angular now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.