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 :: nodemailer typescript 
Typescript :: wordpress number of posts by user 
Typescript :: whats $_.FullName in powershell 
Typescript :: extending a type in typescript 
Typescript :: react typescript cheat sheet 
Typescript :: json to object typescript 
Typescript :: fgets input from user 
Typescript :: pass data through router angular 
Typescript :: typescript object destructuring 
Typescript :: Strong typed variables typescript 
:: calling contract from ethereum 
::  
Typescript ::  
Typescript :: Angular import from local library 
Typescript :: typescript union types 
Typescript :: 2. Write a program to draw this. Assume the innermost square is 20 units per side, and each successive square is 20 units bigger, per side, than the one inside it. 
Typescript :: dota 2 space to center hero 
Typescript :: preventing letters from being placed in an input ts 
Typescript :: scripted testing and exploratory testing 
Typescript :: two absolute elements are overlapping css help 
Typescript :: findbyidandupdate 
Typescript :: update object in array in ngxrx store in angular 
::  
Typescript :: typescript class 
::  
Typescript :: typescript dynamic interface 
Typescript :: generator typescript 
Typescript :: ts Facade pattern 
Typescript :: serenity-is change button text 
Typescript :: Checking if multiple elements are rendering using jasmine 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =