Reputation: 209
I have the regular expression for "dd/MM/yyyy" which works fine,
"^([0]?[1-9]|[1][0-2])[./-]([0]?[1-9]|[1|2][0-9]|[3][0|1])[./-]([0-9]{4}|[0-9]{2})$"
but I want to modify this so it also accepts 00/MM/2014. My program interpret this as all the days in a specific month. For example 00/04/2014 means all the dates in month of April.
Can someone tell me what kind of change I need to make to above script to make this happen?
Here is the code which I am using it in :
<asp:TextBox ID="TextBox5" runat="server" MaxLength="1" CssClass="MainContent"
style="text-align:justify" ValidationGroup="MKE" Width="130px" />
<asp:ImageButton ID="ImgBntCalc" runat="server" CausesValidation="False"
ImageUrl="images\calendar-schedulehs.png" />
<asp:MaskedEditExtender ID="MaskedEditExtender2" runat="server"
AcceptNegative="Left" DisplayMoney="Left" ErrorTooltipEnabled="True"
mask="99/99/9999" MaskType="Date" MessageValidatorTip="true"
OnFocusCssClass="MaskedEditFocus" OnInvalidCssClass="MaskedEditError"
TargetControlID="TextBox5" />
<asp:MaskedEditValidator ID="MaskedEditValidator2" runat="server"
ControlExtender="MaskedEditExtender2" ControlToValidate="TextBox5"
Display="Dynamic" EmptyValueBlurredText="*" ValidationExpression="^(?:[012]?[0-9]|3[01])[./-](?:0?[1-9]|1[0-2])[./-](?:[0-9]{2}){1,2}$"
InvalidValueMessage="Date is invalid" ValidationGroup="MKE1" />
<asp:CalendarExtender ID="CalendarExtender1" runat="server" Format="dd/MM/yyyy"
PopupButtonID="ImgBntCalc" TargetControlID="TextBox5" />
Upvotes: 1
Views: 22231
Reputation: 28403
Try this pattern
^([0]?[0-9]|[1][0-2])[.\/-]([0]?[1-9]|[1|2][0-9]|[3][0|1])[.\/-]([0-9]{4}|[0-9]{2})$
IN ASPX
<asp:TextBox ID="TextBox5" runat="server" MaxLength="1" CssClass="MainContent"
style="text-align:justify" ValidationGroup="MKE" Width="130px" />
<asp:ImageButton ID="ImgBntCalc" runat="server" CausesValidation="False"
ImageUrl="images\calendar-schedulehs.png" />
<asp:MaskedEditExtender ID="MaskedEditExtender2" runat="server"
AcceptNegative="Left" DisplayMoney="Left" ErrorTooltipEnabled="True"
mask="99/99/9999" MaskType="Date" MessageValidatorTip="true"
OnFocusCssClass="MaskedEditFocus" OnInvalidCssClass="MaskedEditError"
TargetControlID="TextBox5" />
<asp:MaskedEditValidator ID="MaskedEditValidator2" runat="server"
ControlExtender="MaskedEditExtender2" ControlToValidate="TextBox5"
Display="Dynamic" EmptyValueBlurredText="*" ValidationExpression="^([0]?[0-9]|[1][0-2])[.\/-]([0]?[1-9]|[1|2][0-9]|[3][0|1])[.\/-]([0-9]{4}|[0-9]{2})$"
InvalidValueMessage="Date is invalid" ValidationGroup="MKE1" />
<asp:CalendarExtender ID="CalendarExtender1" runat="server" Format="dd/MM/yyyy"
PopupButtonID="ImgBntCalc"
TargetControlID="TextBox5" />
CHECK HERE DATE VALIDATION
Upvotes: 0
Reputation: 6059
I'd submit that there's too much going on in this expression. Subjective, to be sure, but if this were my code I'd make a looser regex and do the validation in code. I find it's way easier to come back six months later and understand that, rather than a complicated regex.
That being said...
^(00|0?[1-9]|[12][0-9]|3[01])[./-]([0]?[1-9]|[12][0-9]|[3][01])[./-]([0-9]{4}|[0-9]{2})$
EDIT
By way of explanation, here's the day portion of the expression, commented:
^(
00 # match the literal string '00'
| 0?[1-9] # or, match 1-9, optionally prefixed with '0'
| [12][0-9] # or, match days 10-29
| 3[01] # or, match days 30 and 31
)
Judging by your comments, it sounds like there's a problem with how you're actually using the regex in code. Can you post the code where you're actually using the expression?
Upvotes: 0
Reputation: 91385
How about:
dd/mm/yyyy:
^(?:[012]?[0-9]|3[01])[./-](?:0?[1-9]|1[0-2])[./-](?:[0-9]{2}){1,2}$
mm/dd/yyyy:
^(?:0?[1-9]|1[0-2])[./-](?:[012]?[0-9]|3[01])[./-](?:[0-9]{2}){1,2}$
Upvotes: 0
Reputation: 152521
Well right now it's built for MM/dd/yyyy
(the first group can only contain 0-12). But here's one that works for dd/mm/yyyy
and allows for a 00
or 0
day:
"^([0]?[0-9]|[12][0-9]|[3][01])[./-]([0]?[1-9]|[1][0-2])[./-]([0-9]{4}|[0-9]{2})$"
Upvotes: 5
Reputation: 4372
You may also want to check out DateTime.ParseExact (format is described here), in most cases it's the easier way to parse dates...
Upvotes: 0