Validation pattern for date in DD/MM/YYYY format using angular2

前端 未结 3 470
北荒
北荒 2021-01-14 01:00

How to add validation pattern for Date in DD/MM/YYYY format using angular2 Validator.pattern.

I have the \"required\" validator in place. Cannot find one for date pa

3条回答
  •  忘掉有多难
    2021-01-14 01:56

    You can also create a service( not mandatory, i.e the following could easily be changed to a regular function ) as follows:

    import { Injectable } from '@angular/core';
    import { FormControl } from '@angular/forms';
    
        interface Validator {
          (c: T): { [error: string]: any };
        }
    
        @Injectable()
        export class DateValidatorClass {
    
          constructor() { }
          dateValidator(c: FormControl) {
    
            const dateRegEx = new RegExp(yourRegExInHere);;
    
            return c.value.test(dateRegEx) ? null : {
              dateValidator: {
                valid: false
              }
        };
      }
    }
    

提交回复
热议问题