I am new to Angular. I am using angular 4 reactive forms and figured out how to perform custom validations. Following is my implementation for numeric
function numberValidator(c: AbstractControl): { [key: string]: boolean } | null {
if (c.pristine) {
return null;
}
if (c.value.match(/.*[^0-9].*/)) {
return { 'numeric': true };
}
return null;
}
phoneControl: ['', [Validators.required, Validators.minLength(10), Validators.maxLength(10), numberValidator]],
I am trying to find out how to perform currency (with or without two decimal places) and most importantly Date field.
Forgive me if this was answered elsewhere but I cannot find any samples for angular(4)
Thanks for your time
What kind of date validation do you need? Just that the value is a valid date? If you set the type="date"
on the input element the browser will ensure that only a valid date is entered.
Same for your shown number validator and for any currency validator. You should be able to set the type="number"
on the input element and you won't need a validator.
If you still do want to perform your own validation, you can use regular expressions just as in your example.
Just look up regular expressions for currency and date. For the date, something like this: Javascript - Regex to validate date format