Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

from date and to date validation in angular 9

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 start date end 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 :: get all elements with id starts and class 
Typescript :: python lists union 
Typescript :: typescript how to create an array instance 
Typescript :: how to create empty object typescript 
Typescript :: how push objects into a local stotage array 
Typescript :: typescript interface vs type 
Typescript :: typescript component props 
Typescript :: google sheets countif two conditions 
Typescript :: chevrons or angle brackets latex 
Typescript :: search an array of objects with specific object property value 
Typescript :: typescript generic function 
Typescript :: angle between two vectors 
Typescript :: Get Type of first element in Array TypeScript 
Typescript :: typescript object get value by key 
Typescript :: Round a float two decimal points 
Typescript :: angular build router-outlet not working 
Typescript :: pyton program acept user first and last name and prints in revese 
Typescript :: stripe create subscription 
Typescript :: typescript catch error type 
Typescript :: linux copy all directory contents to another directory 
Typescript :: O arquivo yarn.ps1 não pode ser carregado porque a execução de scripts foi desabilitada neste sistema 
Typescript :: listen to server sent events flutter 
Typescript :: different types of errors in numerical methods 
Typescript :: The Android Gradle plugin supports only Kotlin Gradle plugin version 1.3.40 and higher. flutter compressvideo 
Typescript :: python compare lists unordered 
Typescript :: get number of digits in an integer python without converting to string 
Typescript :: typeorm transaction example 
Typescript :: typescript omit 
Typescript :: callback ref typescript 
Typescript :: multicolor points in one legend entry python 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =