How to apply regex for date format MM / DD / YYYY HH: MM: SS am / pm in ASP.NET
I have a TextBox where I accept input from the user as . Now I want to test it with a regular expression. I'm not sure how to apply the expression. I have added my code as well. dd/mm/yyyy hh:mm:ss
<tr>
<td style="width: 30%" class="EcommLabel">
Date From
</td>
<td style="width: 70%" class="EcommLabel">
<asp:TextBox ID="txtDateFrom" CssClass="EcommNormalTextBox" runat="server">
</asp:TextBox>MM/DD/YYYY<br />
<%-- <asp:RegularExpressionValidator ID="regDateFrom" ValidationExpression="^(((0?[1-9]|1[012])/(0?[1-9]|1\d|2[0-8])|(0?[13456789]|1[012])/(29|30)|(0?[13578]|1[02])/31)/(19|[2-9]\d)\d{2}|0?2/29/((19|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00)))$" ControlToValidate="txtDateFrom" ValidationGroup="Promotion" runat="server" ErrorMessage="Invalid Date"></asp:RegularExpressionValidator>--%>
<asp:RangeValidator runat="server" ID="rvDateFrom" Type="Date" ControlToValidate="txtDateFrom" MaximumValue="3000/12/31" MinimumValue="2000/1/1" ErrorMessage="Invalid Date" Display="Dynamic" ValidationGroup="Promotion" />
</td>
</tr>
+3
source to share
2 answers
use this expression "(\ d {2}): (\ d {2}): (\ d {4}): (\ d {2}): (\ d {2}): (\ d {2} )" as
<asp:RegularExpressionValidator ID="regDateFrom" ValidationExpression="(\d{2}):(\d{2}):(\d{4}):(\d{2}):(\d{2}):(\d{2})"
ControlToValidate="txtDateFrom" ValidationGroup="Promotion" runat="server"
ErrorMessage="Invalid Date"></asp:RegularExpressionValidator>
Also see the following questions on stackoverflow:
How to write regex for MM: DD: YYYY: HH: MM: SS
MM / DD / YYYY HH: MM: SS AM / PM regex for date validation in javascript
Hope it helps!
+1
source to share
Changing your regex would do it.
Use the following regex
^ ([1-9] | ([012] [0-9]) | (3 [01])) - ([0] {0,1} [1-9] | 1 [012]) - \ d \ d \ d \ d [012] {0,1} [0-9]: [0-5] [0-9]: [0-5] [0-9] $
It will check the value 01-12-2011 19:59:59
<asp:RegularExpressionValidator ID="regDateFrom"
ValidationExpression="^([1-9]|([012][0-9])|(3[01]))-([0]{0,1}[1-9]|1[012])-\d\d\d\d [012]{0,1}[0-9]:[0-5][0-9]:[0-5][0-9]$"
ControlToValidate="txtDateFrom" ValidationGroup="Promotion"
runat="server"
ErrorMessage="Invalid Date">
</asp:RegularExpressionValidator>
0
source to share