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




