The "angular2.common Validators" is a built-in package library in Angular for validating form inputs. It contains different types of validators that can be used in forms to ensure that the input values provided by the user are valid. The following are some examples of Validators along with their code implementation:
1. Required Validator:
This is used to ensure that a particular field is not empty. If the field is empty, it displays an error message asking the user to provide a value.
import { Validators } from '@angular/common';
...
myForm = new FormGroup({
name: new FormControl('', Validators.required),
email: new FormControl('', Validators.required),
phone: new FormControl('', Validators.required)
});
2. Email Validator:
This is used to ensure that the email provided by the user is in a valid format. If the email is not in a valid format, it displays an error message asking the user to provide a valid email.
import { Validators } from '@angular/common';
...
myForm = new FormGroup({
email: new FormControl('', [Validators.required, Validators.email]),
});
3. Pattern Validator:
This is used to ensure that a particular field follows a specific pattern. For example, if we want to ensure that the phone number provided by the user is in a specific format, we can use the pattern validator.
import { Validators } from '@angular/common';
...
myForm = new FormGroup({
phone: new FormControl('', [Validators.required, Validators.pattern(/^([0-9]{3})-([0-9]{3})-([0-9]{4})$/)])
});
Overall, the "angular2.common Validators" package library in Angular provides a range of validators that can help validate form inputs.
TypeScript Validators - 30 examples found. These are the top rated real world TypeScript examples of angular2/common.Validators extracted from open source projects. You can rate examples to help us improve the quality of examples.