Currently, my focus is on Angular 4 where I have developed a custom validator for checking CGPA values (to ensure it is between 2.0 and 4.0). Although the predefined `Validators.required` works fine, my custom validator seems to be not triggering as expected in the component.ts file.
Below is the implementation of the CGPA Validator:
function ValidCgpa(cgpa: FormControl): {[s: string]: boolean} {
let num = Number(cgpa);
if(num < 2.0 || num > 4.0) {
return {invalidCgpa: true};
}
}
Usage of Custom CGPA Validator:
this.educationalDetailsForm = new FormGroup({
cgpa: new FormControl('', Validators.compose([Validators.required, ValidCgpa]))
});
Here's the corresponding HTML Code snippet:
<input type="text" formControlName="cgpa" placeholder="CGPA">
<div *ngIf="educationalDetailsForm.hasError('ValidCgpa','cgpa') && educationalDetailsForm.get('cgpa').touched">
Enter a number greater than 2.0 and less than 4.0 for CGPA
</div>