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
ValidatorFnlinkA function that receives a control and synchronously returns a map of validation errors if present, otherwise null. interface ValidatorFn { (control: AbstractControl<any, any>): ValidationErrors | null }
The angular. isDate() function in AngularJS is used to determine whether the value of the date is valid or not. It returns true if the reference is a date else false.
You can subscribe to value changes of a control or the whole form. updateValueAndValidity allows you to modify the value of one or more form controls and the flag allows you to specify if you want this to emit the value to valueChanges subscribers.
Use a form as the parent element of the inputs (not necessary though). import { DatePipe } from '@angular/common'; import { Component, OnInit } from '@angular/core'; import { FormControl, FormGroup, Validators } from '@angular/forms'; @Component({ selector: 'app-root', templateUrl: './app.
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
This is another option to using Custom validators
import { FormControl } from '@angular/forms';
export class DateValidator {
static ptDate(control: FormControl): { [key: string]: any } {
let ptDatePattern = /^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/g;
if (!control.value.match(ptDatePattern))
return { "ptDate": true };
return null;
}
static usDate(control: FormControl): { [key: string]: any } {
let usDatePattern = /^02\/(?:[01]\d|2\d)\/(?:19|20)(?:0[048]|[13579][26]|[2468][048])|(?:0[13578]|10|12)\/(?:[0-2]\d|3[01])\/(?:19|20)\d{2}|(?:0[469]|11)\/(?:[0-2]\d|30)\/(?:19|20)\d{2}|02\/(?:[0-1]\d|2[0-8])\/(?:19|20)\d{2}$/;
if (!control.value.match(usDatePattern))
return { "usDate": true };
return null;
}
}
and use it this way for "dd/mm/yyyy" format:
this.formDetail = this.formBuilder.group({
date: ['', DateValidator.ptDate],
});
and use it this way for "mm/dd/yyyy" format:
this.formDetail = this.formBuilder.group({
date: ['', DateValidator.usDate],
});
I hope this help!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With