liveBook · Manning

Validation rule

As explained in Pro Angular 16

A validation rule is a condition that input data must satisfy to be considered valid.

A validation rule states a condition that input data must satisfy before it is treated as valid. Angular extends familiar HTML validation attributes and applies them to form controls. section 13.4.2

Common rules include:

  • required demands a value; minlength and maxlength constrain character counts. section 13.4.2
  • email checks address formatting, min and max constrain values, and pattern requires a regular-expression match. section 13.4.2
  • Angular requires a form and a name attribute for validated template controls. section 13.4.2

Product name

Use name="name", required, minlength="5", and pattern="^[A-Za-z ]+$" on an input bound to newProduct.name. Display feedback when the control is dirty and invalid. section 13.4.2

Table 13.4. The built-in Angular validation attributes
Attribute
Description
email
This attribute is used to specify a well-formatted email address.
required
This attribute is used to specify a value that must be provided.
minlength
This attribute is used to specify a minimum number of characters.
maxlength
This attribute is used to specify a maximum number of characters. This type of validation cannot be applied directly to form elements because it conflicts with the HTML5 attribute of the same name. It can be used with model-based forms, which are described later in the chapter.
min
This attribute is used to specify a minimum value.
max
This attribute is used to specify a maximum value.
pattern
This attribute is used to specify a regular expression that the value provided by the user must match.

Compare Angular’s built-in validation attributes and the conditions each one enforces. section 13.4.2

How Pro Angular 16 covers it

How deep it goes
Explains built-in attributes, form integration, state classes, and messages; defers custom validation rules.
Examples
Demonstrates product-name validation using required, minlength, pattern, CSS classes, and conditional error messages.
What it assumes
Assumes familiarity with HTML input attributes while teaching Angular’s form requirement and control identification from the example.
What you can do after
Apply validation through Angular template attributes and display feedback from control state and errors.
How it connects
Connects validation rules with Angular forms, HTML attributes, regular expressions, validation errors, and form state.

Also explained in

Compare how 5 books explain validation rule →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro Angular 16. The section links point to the parts of the book it draws on.