What is Form Validation?
Form Validation is a client-side validation on the browser. Form Validation is basically used to rectify that form field values are in the correct format and It is used to protect from malicious data.
What is Angular Form Validation?
Angular Form Validation is a Client-side validation. It has four predefined validations in Angular.
1) Validators.required:- This validation ensures that the field is not left empty.
Use this in the componentName.component.ts file
this.FormName = this.fb.group({
FieldName: ['', Validators.required]
});
}
Note:- where fb is a FormBuilder.
2) Validators.minLength(lengthNumber):- this validation ensures the input has a minimum length
this.FormName = this.fb.group({
FieldName: ['', Validators.minLength(lengthNumber)]
});
}
3) Validators.maxLength(lengthNumber):- this validation ensures the input has a maximum length field value.
this.FormName = this.fb.group({
FieldName: ['', Validators.maxLength(lengthNumber)]
});
}
4) Validators.pattern(pattern):- this validation ensures the input against a regular expression. It is used to define the pattern of the field value like defining email validation etc.
this.FormName = this.fb.group({
FieldName: ['', Validators.pattern(pattern)]
});
}
Angular Form Validations – Interview Questions
Q 1: What is form validation?
Q 2: What are built-in validators?
Q 3: Where can validations be applied?
Q 4: Can validations be combined?
Q 5: Why is validation important?
Angular Form Validations – Objective Questions (MCQs)
Q1. Angular provides validation through:
Q2. Required field validation uses:
Q3. Which validation checks minimum length?
Q4. Email validation uses:
Q5. Validation errors are accessed using: