Surya sasidhar
Surya sasidhar

Reputation: 30343

Regular expression for date in asp.net?

In web application, i am using regular expression for date like "dd-mm-yyyy" format for this i get the validation but it is not working when we enter date like "12-02-2012" remaining condition it is working fine, can you help me this is my validation expression.

  <asp:RegularExpressionValidator ID ="myg" runat ="server" ControlToValidate ="txt" ErrorMessage ="Check"
      ValidationExpression ="^(((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))))$" >
  </asp:RegularExpressionValidator>

I solve the problem,

   ValidationExpression ="^(((0[1-9]|[12]\d|3[01])-(0[13578]|1[02])-((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)-(0[123456789]|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))))$"

Upvotes: 0

Views: 5208

Answers (2)

Surya sasidhar
Surya sasidhar

Reputation: 30343

I slove the problem,

 ValidationExpression ="^(((0[1-9]|[12]\d|3[01])-(0[13578]|1[02])-((19|[2-9]\d)\d{2}))|
    ((0[1-9]|[12]\d|30)-(0[123456789]|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))))$"

Upvotes: 0

codeandcloud
codeandcloud

Reputation: 55333

asp:RegularExpression is not the way to do this. For validating date using an ASP.NET Validator its ideal to use asp:CompareValidator

Here is a small mock-up

<asp:TextBox ID="txtDatecompleted" runat="server"/>
<asp:CompareValidator ID="dateValidator" runat="server"
    ControlToValidate="txtDatecompleted" 
    ErrorMessage="Please enter a valid date."
    Operator="DataTypeCheck"
    Type="Date"
    ValidationGroup="TestVGroup">
</asp:CompareValidator>
<br />
<asp:Button ID="ClickButton" Text="Click Me" runat="server" 
    ValidationGroup="TestVGroup"
    OnClick="ClickButton_Click" />

This will automatically check for different date formats and leap years

Upvotes: 2

Related Questions