You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
974 B
44 lines
974 B
import { Directive, Input } from '@angular/core';
|
|
import {
|
|
AbstractControl,
|
|
NG_VALIDATORS,
|
|
ValidationErrors,
|
|
Validator,
|
|
} from '@angular/forms';
|
|
|
|
@Directive({
|
|
selector: '[appDecimalValidator]',
|
|
providers: [
|
|
{
|
|
provide: NG_VALIDATORS,
|
|
useExisting: DecimalValidatorDirective,
|
|
multi: true,
|
|
},
|
|
],
|
|
})
|
|
export class DecimalValidatorDirective implements Validator {
|
|
@Input() digitLength!: number;
|
|
@Input() scaleLength!: number;
|
|
|
|
validate(control: AbstractControl): ValidationErrors | null {
|
|
if (
|
|
control.value === undefined ||
|
|
control.value === null ||
|
|
control.value === ''
|
|
) {
|
|
return null;
|
|
}
|
|
|
|
const regular = new RegExp(
|
|
`^\\d{1,${this.digitLength}}(\\.\\d{0,${this.scaleLength}})?$`
|
|
);
|
|
return regular.test(control.value)
|
|
? null
|
|
: {
|
|
decimal: {
|
|
digitLength: this.digitLength,
|
|
scaleLength: this.scaleLength,
|
|
},
|
|
};
|
|
}
|
|
}
|