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 a correct format and It is used to protect from the 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 is used to required the form field value.
Use this into componentName.component.ts file
this.FormName = this.fb.group({
FieldName: ['', Validators.required]
});
}
Note:- where fb is a FormBuilder.
2) Validators.minLength(lengthNumber):- this validation is used to define the minimum length of the field value.
this.FormName = this.fb.group({
FieldName: ['', Validators.minLength(lengthNumber)]
});
}
3) Validators.maxLength(lengthNumber):- this validation is used to define the maximum length of the field value.
this.FormName = this.fb.group({
FieldName: ['', Validators.maxLength(lengthNumber)]
});
}
4) Validators.pattern(pattern):- this validation is used to define the pattern of the field value like define email validation etc.
this.FormName = this.fb.group({
FieldName: ['', Validators.pattern(pattern)]
});
}