0

Trying to validate the date format in Angular "DD/MM/YYYY" using regx in pattern

Pattern I used

    public static readonly TAG_DATE_FORMAT_VALUE_REGX : string = '/^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$/';
 public static readonly TAG_DATE_FORMAT_VALUE_REGX : string = '^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$';

 public static readonly TAG_DATE_FORMAT_VALUE_REGX : string = '  ^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$';

 public static readonly TAG_DATE_FORMAT_VALUE_REGX : string = '^[0-3]?[0-9]/[0-3]?[0-9]/(?:[0-9]{2})?[0-9]{2}$';

And Many others. None of them are working for me.

TS Code

eventStartDateTime: [null, Validators.compose([Validators.required, Validators.pattern(ConstantValues.TAG_DATE_FORMAT_VALUE_REGX)])],

HTML code

<div *ngIf="(f.eventStartDateTime.invalid && eventform.submitted) || (f.eventStartDateTime.invalid && (f.eventStartDateTime.dirty || f.eventStartDateTime.touched))"
                    class="invalid-feedback">
                    <div *ngIf="f.eventStartDateTime.errors.required"> Please provide a starting date/time for this
                       professional learning event. </div>
                    <div *ngIf="f.eventStartDateTime.errors.pattern">Invalid Date Format. Valid format - DD/MM/YYYY
                    </div>
                  </div>

References

Javascript date regex DD/MM/YYYY

Regex to validate date format dd/mm/yyyy

https://www.regextester.com/99555

San Jaisy
  • 15,327
  • 34
  • 171
  • 290
  • @DanielB- I used the regx from that site. But none of them are working for me. If you check the regx that is from that website – San Jaisy May 23 '19 at 06:59
  • You either use a regex literal or a string pattern, do not mix them as you did. Either `'^(0?[1-9]|[12][0-9]|3[01])[/-](0?[1-9]|1[012])[/-]\\d{4}$'` or `/^(0?[1-9]|[12][0-9]|3[01])[\/-](0?[1-9]|1[012])[\/-]\d{4}$/` – Wiktor Stribiżew May 23 '19 at 07:04

0 Answers0