Hi
How to put validations. I am using Reactive form
Code
New Employee
Name
Name is required.
Name should be 3 character.
-->
Email
Contact No
Email is required.
Name should be 3 character.
-->
Address1
Address2
Pin Code
City
State
Save
Cancel -->JavaScript
employeeArray: any[] = [];
objEmployee: any = {
"empId":0,
"empName": "",
"empContactNo": "",
"empEmail": "",
"addressLine1": "",
"addressLine2": "",
"pinCode":"",
"city": "",
"state": "",
}
constructor(private empSrv:EmployeeService){}
form = new FormGroup({
empName: new FormControl('', [Validators.required, Validators.minLength(3),Validators.maxLength(50),]),
empContactNo: new FormControl('', [Validators.required, Validators.minLength(3),Validators.maxLength(50),]),
email: new FormControl('', [Validators.required, Validators.email]),
addressLine1: new FormControl('', [Validators.required, Validators.minLength(3),Validators.maxLength(50),]),
addressLine2: new FormControl('', [Validators.required, Validators.minLength(3),Validators.maxLength(50),]),
pinCode: new FormControl('', [Validators.required, Validators.minLength(3),Validators.maxLength(50),]),
city: new FormControl('', [Validators.required, Validators.minLength(3),Validators.maxLength(50),]),
state: new FormControl('', [Validators.required, Validators.minLength(3),Validators.maxLength(50),])
});
get f(){
return this.form.controls;
}Thanks

Jayraj ChhayaRank #304 · 8.2K reputationPosted May 21, 2024, 3:20 AM EDT
To add validations to your Angular Reactive Form in a modal, you can use the built-in Validators class provided by Angular. Here's how you can implement validations in your code:
import { FormGroup, FormControl, Validators } from '@angular/forms';form = new FormGroup({ empName: new FormControl('', [Validators.required, Validators.minLength(3), Validators.maxLength(50)]), empContactNo: new FormControl('', [Validators.required, Validators.minLength(3), Validators.maxLength(50)]), email: new FormControl('', [Validators.required, Validators.email]), addressLine1: new FormControl('', [Validators.required, Validators.minLength(3), Validators.maxLength(50)]), addressLine2: new FormControl('', [Validators.required, Validators.minLength(3), Validators.maxLength(50)]), pinCode: new FormControl('', [Validators.required, Validators.minLength(3), Validators.maxLength(50)]), city: new FormControl('', [Validators.required, Validators.minLength(3), Validators.maxLength(50)]), state: new FormControl('', [Validators.required, Validators.minLength(3), Validators.maxLength(50)]) });formControlNamedirective:empNamewith the respective form control name.In your component class, you can access the form controls and their validation status using the
form.controlsproperty:get f() { return this.form.controls; }Now, when a user interacts with the form, the validations will be triggered and the corresponding error messages will be displayed if the input is invalid.
Remember to import the necessary modules and classes, and make sure to include the
ReactiveFormsModulein your app module.