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 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 :: 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 :: embed youtube search results into website 
Typescript :: python convert long floats to usd 
Typescript :: ts generics 
Typescript :: basic variable typescript 
Typescript :: docker: Error response from daemon: Ports are not available: listen tcp 0.0.0.0:3306: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted. 
Typescript :: call function dynamically typescript 
Typescript :: how to send attachments to node mailer file not found 
Typescript :: props vue typescript 
Typescript :: typescript get the promise return type 
Typescript :: avatar image mui not centeered 
Typescript :: getstaticpaths errors after new posts 
Typescript :: clean broken shortcuts in windows start menu 
Typescript :: tsconfig.json, Typescript 
Typescript :: Warning: call_user_func_array() expects parameter 1 to be a valid callback 
Typescript :: +github graphql api get commits from repo 
Typescript :: angular sort string 
Typescript :: typescript get type from promise 
Typescript :: react fc typescript 
Typescript :: switch in typescript 
Typescript :: typescript export interface array 
Typescript :: can you make twitter bots in node.js 
Typescript :: props react typescript 
Typescript :: typescript ingerit 
Typescript :: upload keystore file to secrets github actions 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =