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 :: how to get class weights while using keras imagedatagenerator 
Typescript :: find common elements in two flutter 
Typescript :: +github graphql api get commits from repo 
Typescript :: typescript type specific strings 
Typescript :: laravel no tests executed 
Typescript :: how to compile ts in cmd 
Typescript :: get distinct elements in table psql 
Typescript :: promise.all inside useEffect 
Typescript :: rest parameters in typescript 
Typescript :: typescript string 
Typescript :: angular validations 
Typescript :: typescript syntax 
Typescript :: symbol typescript 
Typescript :: socket.io auth 
Typescript :: download blob typescript 
Typescript :: grid implementation html canvas 
Typescript :: empty form elements except jquery 
Typescript :: firebase typescript 
Typescript :: minuts bwtewwn two date laravel 
Typescript :: typescript implement 
Typescript :: graphql mutation is not displaying array of objects in express-graphql 
Typescript :: run a python module with imports from parent 
Typescript :: remove white border around components angular 
Typescript :: how to convert an array of other types in java 8 
Typescript :: fputs c++ 
Typescript :: no database host was found that meets the requirements for this server. 
Typescript :: typescript override 
Typescript :: testing without requirements 
Typescript :: a file consists of mcq 
Typescript :: Basic structure of named imports and exports 
ADD CONTENT
Topic
Content
Source link
Name
9+8 =