Angular Min Max validators


Angular Min Max validators

Angular Min Max validators Implementation of min/max value validator directives for Angular 2.0.0 that works with both Angular forms validation and the DOM. As best as I can tell, these two validator directives are absent from the initial Angular 2.0.0

Validators

Provides a set of built-in validators that can be used by form controls.

See more…

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
}

See also

  • Form Validation

Description

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.

Static methods

Validator that requires the control’s value to be greater than or equal to the provided number. The validator exists only as a function and not as a directive.

See also:

  • updateValueAndValidity()
static min(min: number): ValidatorFn
Parameters
minnumber
Returns

ValidatorFn: A validator function that returns an error map with the min property if the validation check fails, otherwise null.

Usage Notes

Validate against a minimum of 3
const control = new FormControl(2, Validators.min(3));

console.log(control.errors); // {min: {min: 3, actual: 2}}

Validator that requires the control’s value to be less than or equal to the provided number. The validator exists only as a function and not as a directive.

See also:

  • updateValueAndValidity()
static max(max: number): ValidatorFn
Parameters
maxnumber
Returns

ValidatorFn: A validator function that returns an error map with the max property if the validation check fails, otherwise null.

Usage Notes

Validate against a maximum of 15
const control = new FormControl(16, Validators.max(15));

console.log(control.errors); // {max: {max: 15, actual: 16}}

 

Leave a Reply

Your email address will not be published. Required fields are marked *