Tuesday 5 November 2013

Validate Date using RegularExpressionValidator in ASP.NET like dd-Mmm-yyyy and dd/mm/yyyy

Hi friends,in this article i will explain about Validate Date using RegularExpressionValidator in ASP.NET like dd-Mmm-yyyy and dd/mm/yyyy.
I already explained in the previous articles about How to Bind Nested Gridview from Database in asp.net with C#/VB.NET,Marquee tag or How to Scroll Text From left to right or How to Move the Text in HTML,C#/VB.NET:Save the generated pdf directly to the server directory folder without user prompt in ASP.NET and JQuery:How to Display Current Date and Time on Webpage using ASP.NET
In ASP.NET page write the below code.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Validate Date using RegularExpressionValidator in ASP.NET </title>
    <style  type ="text/css">
    .color
    {
        color:Red;
    }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <table>
     <tr >
     <td> Date Format must be like (dd-Mmm-yyyy) :</td>
     <td><asp:TextBox ID="txtDate"  runat="server" ></asp:TextBox></td>
     <td>
    <asp:RegularExpressionValidator ID="regDate1" runat="server" SetFocusOnError="true" CssClass="color"
      ControlToValidate="txtDate"  ErrorMessage="Please Enter a valid From Date(dd-Mmm-yyyy)"
     ValidationExpression="^((31(?!(Apr|Jun|Sep|Nov)))|((30|29)(?!Feb))|(29(?= Feb(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))|(0?[1-9])|1\d|2[0-8])[-](Jan|Feb|Mar|May|Apr|Jul|Jun|Aug|Oct|Sep|Nov|Dec)[-]((1[6-9]|[2-9]\d)\d{2})$"> </asp:RegularExpressionValidator>
      </td>  
      </tr>
      <tr><td> Date Format must be like (dd/mm/yyyy) :</td>
      <td> <asp:TextBox ID="txtDate2" runat="server"></asp:TextBox></td>
     <td>
     <asp:RegularExpressionValidator ID="regDate2" runat="server"
     ErrorMessage="Please Enter a valid From Date(dd/mm/yyyy)"
     ControlToValidate="txtDate2"  CssClass="color "
     SetFocusOnError="True"
     ValidationExpression="^(?=\d)(?:(?:31(?!.(?:0?[2469]|11))|(?:30|29)(?!.0?2)|29(?=.0?2.(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(?:\x20|$))|(?:2[0-8]|1\d|0?[1-9]))([-./])(?:1[012]|0?[1-9])\1(?:1[6-9]|[2-9]\d)?\d\d(?:(?=\x20\d)\x20|$))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\x20[AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$"></asp:RegularExpressionValidator>
     </td>
    </tr>
    </table>
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
    </div>
    </form>
</body>
</html>



 The output of the above as shown below,when we enter invalid date formats it shows the  errors as shown in the below figure
"If you like my blog or articles, you can appreciate by leaving your comments or Liking my Facebook pageAspdotnet-kishore, following on Google+ Aspdotnet-Kishore, Twitter  on AspdotnetKishore, Linked in Aspdotnet-Kishore, stumbling my posts on stumble upon and subscribing on  RSSfeed Aspdotnet-Kishore for free updates directly to your Email inbox . Watch my blog  for more articles."

No comments:

Post a Comment

© 2012-2018 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.