0

I Have this regex a perfect to to match mm/dd/yyyy on all condition time format but i need to modify it a little bit

if someone insert 09/04/1980 its work perfect, but this 5/9/1990 (1 digit) will error :(

can someone fix it for me ?

this is my current regex :

(?:(?:0[1-9]|1[0-2])/(?:0[1-9]|1[0-9]|2[0-9])|(?:(?!02)(?:0[1-9]|1[0-2])-(?:30))|(?:(?:0[13578]|1[02])-31))/(?:19|20)[0-9]{2}
himawan_r
  • 1,760
  • 1
  • 14
  • 22

2 Answers2

1
(?:(?:0?[1-9]|1[0-2])\/(?:0?[1-9]|1[0-9]|2[0-9])|(?:(?!02)(?:0?[1-9]|1[0-2])-(?:30))|(?:(?:0?[13578]|1[02])-31))\/(?:19|20)[0-9]{2}

Try this.Made 0 optional.

See demo.

http://regex101.com/r/dZ1vT6/65

vks
  • 67,027
  • 10
  • 91
  • 124
0

DON'T use regex for this. I'll provide pesudocode since you've not specified language. You just need to split on \ and convert the array elements to numbers and then do simple if condition.

if(arr[0] < 0 && arr[0] > 32){
   // validated the month
}
Amit Joki
  • 58,320
  • 7
  • 77
  • 95