Reputation: 2557
I copied this from RegEx Library to validate input date. It works fine except that it allows yy as well as yyyy. But the SQL Server rejects date with yy. So i want to restrict this RegEx to only accept yyyy. How do i change this to accept yyyy and not yy ?
^(((((((0?[13578])|(1[02]))[\.\-/]?((0?[1-9])|([12]\d)|(3[01])))|(((0?[469])|(11))[\.\-/]?((0?[1-9])|([12]\d)|(30)))|((0?2)[\.\-/]?((0?[1-9])|(1\d)|(2[0-8]))))[\.\-/]?(((19)|(20))?([\d][\d]))))|((0?2)[\.\-/]?(29)[\.\-/]?(((19)|(20))?(([02468][048])|([13579][26])))))$"
Upvotes: 0
Views: 52
Reputation: 517
Try the below regex to validate the MM/-dd/-yyyy
^(0[1-9]|1[0-2])[\.\-/]?(0[1-9]|1\d|2\d|3[01])[\.\-/](19|20)\d{2}$
Upvotes: 1
Reputation: 336128
You need to remove the ?
s after ((19)|(20))
to make those groups non-optional.
That said, do you really trust that regex? My eyes are glazing over from all those unnecessary parentheses, brackets and backslashes - if terseness of syntax is any indication, my guess is that whoever wrote this doesn't know too much about regexes.
Upvotes: 2