Custom validator I am currently using is:
static digitOnly(digit: string): ValidatorFn {
return (control: AbstractControl): ValidationErrors | null => {
const restrict = [];
for (let i = 0; i < parseInt(digit); i++) {
restrict.push('0');
}
const val = control.value;
if (val === null || val === '') {
return null;
}
if (restrict.toString().replace(/,/g,'') === val.toString()) {
return { 'invalidNumber': true };
}
const pattern = new RegExp(`^[0-9]{${digit}}$`);
if (!val.toString().match(pattern)) {
return { 'invalidNumber': true };
}
return null;
}
}
Descrption:
I want to add a custom validator to an input field which accepts GPA, the requirement is: entered value cannot be more than 400 or empty or 0 or 1
right now: my custom validator is working for the following conditions: validating 000 as invalid which is correct validating 0 or 1 or numeric single digit as invalid which is correct
only problem is that it is accepting values above 400
looking forward for a quick help.