Validators
提供一组内置验证器,可用于各种表单控件。
Provides a set of built-in validators that can be used by form controls.
class Validators {
static min(min: number): ValidatorFn
static max(max: number): ValidatorFn
static required(control: AbstractControl): ValidationErrors | null
static requiredTrue(control: AbstractControl): ValidationErrors | null
static email(control: AbstractControl): ValidationErrors | null
static minLength(minLength: number): ValidatorFn
static maxLength(maxLength: number): ValidatorFn
static pattern(pattern: string | RegExp): ValidatorFn
static nullValidator(control: AbstractControl): ValidationErrors | null
static compose(validators: ValidatorFn[]): ValidatorFn | null
static composeAsync(validators: AsyncValidatorFn[]): AsyncValidatorFn | null
}
参见
说明
验证器就是一个函数,它可以处理单个 FormControl
或一组控件,并返回一个错误映射表(map)或 null。null 表示验证已通过了。
A validator is a function that processes a FormControl
or collection of controls and returns an error map or null. A null map means that validation has passed.
静态方法
此验证器要求控件的值大于或等于指定的数字。 它只有函数形式,没有指令形式。 Validator that requires the control's value to be greater than or equal to the provided number. See also:
| |||
参数
返回值如果验证失败,则此验证器函数返回一个带有
| |||
使用说明验证至少为 3Validate against a minimum of 3
|
此验证器要求控件的值小于等于指定的数字。 它只有函数形式,没有指令形式。 Validator that requires the control's value to be less than or equal to the provided number. See also:
| |||
参数
返回值如果验证失败,则此验证器函数返回一个带有
| |||
使用说明验证最大为 15Validate against a maximum of 15
|
此验证器要求控件具有非空值。 Validator that requires the control have a non-empty value. See also:
| |||
参数
返回值如果验证失败,则此验证器函数返回一个带有
| |||
使用说明验证该字段不是空的Validate that the field is non-empty
|
此验证器要求控件的值为真。它通常用来验证检查框。 Validator that requires the control's value be true. This validator is commonly used for required checkboxes. See also:
| |||
参数
返回值如果验证失败,则此验证器函数返回一个带有
| |||
使用说明验证字段值为真Validate that the field value is true
|
此验证器要求控件的值能通过 email 格式验证。 Validator that requires the control's value pass an email validation test. See also:
| |||
参数
返回值如果验证失败,则此验证器函数返回一个带有
| |||
使用适合普通用例的正则表达式模式测试值。该模式基于 WHATWG HTML 规范中有效电子邮件地址的定义,并进行了一些增强以支持更多的 RFC 规则(例如与域名相关的规则以及地址不同部分的长度)。 Tests the value using a regular expression pattern suitable for common usecases. The pattern is based on the definition of a valid email address in the WHATWG HTML specification with some enhancements to incorporate more RFC rules (such as rules related to domain names and the lengths of different parts of the address). 与 WHATWG 版本的区别包括: The differences from the WHATWG version include:
如果此模式不能满足你的业务需求,则可以使用 If this pattern does not satisfy your business needs, you can use | |||
使用说明验证该字段匹配有效的 email 格式。Validate that the field matches a valid email pattern
|
此验证器要求控件值的长度大于等于所指定的最小长度。当使用 HTML5 的 Validator that requires the length of the control's value to be greater than or equal to the provided minimum length. This validator is also provided by default if you use the the HTML5 See also:
| |||
参数
返回值如果验证失败,则此验证器函数返回一个带有
| |||
使用说明验证该字段至少有 3 个字符Validate that the field has a minimum of 3 characters
|
此验证器要求控件值的长度小于等于所指定的最大长度。当使用 HTML5 的 Validator that requires the length of the control's value to be less than or equal to the provided maximum length. This validator is also provided by default if you use the the HTML5 See also:
| |||
参数
返回值如果验证失败,则此验证器函数返回一个带有
| |||
使用说明验证该字段最多具有 5 个字符Validate that the field has maximum of 5 characters
|
此验证器要求控件的值匹配某个正则表达式。当使用 HTML5 的 Validator that requires the control's value to match a regex pattern. This validator is also provided by default if you use the HTML5 See also:
| |||
参数
返回值如果验证失败,则此验证器函数返回一个带有
| |||
使用说明验证该字段只包含字母或空格Validate that the field only contains letters or spaces
带有全局或粘性(sticky)标志的匹配模式Pattern matching with the global or sticky flag当要连续运行验证时,使用传递给
|
此验证器什么也不做。 Validator that performs no operation. See also:
| |||
参数
返回值
|
把多个验证器合并成一个函数,它会返回指定控件的各个错误映射表的并集。 Compose multiple validators into a single function that returns the union of the individual error maps for the provided control.
参数
返回值如果验证失败,则此验证器函数返回各个验证器所返回错误对象的一个并集,否则为
| |||
参数
返回值
|
把多个异步验证器合并成一个函数,它会返回指定控件的各个错误映射表的并集。 Compose multiple async validators into a single function that returns the union of the individual error objects for the provided control. See also:
| |||
参数
返回值如果验证失败,则此验证器函数返回各异步验证器所返回错误对象的一个并集,否则为
|