Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

from date and to date validation in angular 8

export class CustomeDateValidators {
    static fromToDate(fromDateField: string, toDateField: string, errorName: string = 'fromToDate'): ValidatorFn {
        return (formGroup: AbstractControl): { [key: string]: boolean } | null => {
            const fromDate = formGroup.get(fromDateField).value;
            const toDate = formGroup.get(toDateField).value;
           // Ausing the fromDate and toDate are numbers. In not convert them first after null check
            if ((fromDate !== null && toDate !== null) && fromDate > toDate) {
                return {[errorName]: true};
            }
            return null;
        };
    }
}

/*--- implementations ---*/
this.form = this.fb.group({
  fromDate: null,
  toDate: null,
}, { validator: [
  //Default error with this validator:  {fromToDate: true}
  CustomeDateValidators.fromToDate('fromDate', 'toDate')
  
  // For custome error name like: {customeErrorName: true}, pass third optional parameter with custome name
  // CustomeDateValidators.fromToDate('fromDate', 'toDate', 'customeErrorName')
]});
Comment

angular from date to date validation

export class CustomeDateValidators {
    static fromToDate(fromDateField: string, toDateField: string, errorName: string = 'fromToDate'): ValidatorFn {
        return (formGroup: AbstractControl): { [key: string]: boolean } | null => {
            const fromDate = formGroup.get(fromDateField).value;
            const toDate = formGroup.get(toDateField).value;
           // Ausing the fromDate and toDate are numbers. In not convert them first after null check
            if ((fromDate !== null && toDate !== null) && fromDate > toDate) {
                return {[errorName]: true};
            }
            return null;
        };
    }
}

/*--- implementations ---*/
this.form = this.fb.group({
  fromDate: null,
  toDate: null,
}, { validator: [
  //Default error with this validator:  {fromToDate: true}
  CustomeDateValidators.fromToDate('fromDate', 'toDate')
  
  // For custome error name like: {customeErrorName: true}, pass third optional parameter with custome name
  // CustomeDateValidators.fromToDate('fromDate', 'toDate', 'customeErrorName')
]});

Comment

PREVIOUS NEXT
Code Example
Typescript :: Implement a groupByOwners function that: Accepts an associative array 
Typescript :: replace floats in dataframe 
Typescript :: O arquivo yarn.ps1 não pode ser carregado porque a execução de scripts foi desabilitada neste sistema 
Typescript :: findbyidandupdate 
Typescript :: destroy objects when they move below camera unity 
Typescript :: typescript foreach async await 
Typescript :: ts Strategy pattern 
Typescript :: print array elements with space c++ 
Typescript :: conditional statements in linux 
Typescript :: any typescript 
Typescript :: order documents in firestore 
Typescript :: typescript class 
Typescript :: open dialog 
Typescript :: code to run typescript with express <3 
Typescript :: Electron WebContents context-menu 
Typescript :: material dialog disable close 
Typescript :: robux 
Typescript :: typescript object of type interface 
Typescript :: laravel row exists or null 
Typescript :: cluster list values python 
Typescript :: highcharts turbothreshold not working 
Typescript :: puts with details ruby 
Typescript :: Route.component does not have any construct or call signatures - React Router with TypeScript 
Typescript :: conditional statements in ti-82 
Typescript :: get content of bucket objects s3 cli 
Typescript :: dto typescript 
Typescript :: array of linked lists in cpp 
Typescript :: angular stop dialog stacking 
Typescript :: vba check if two sheets are the same 
Typescript :: alternative for .include in typescript 
ADD CONTENT
Topic
Content
Source link
Name
8+7 =