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)]
});
}