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">
    <form id="form1" runat="server">
     <tr >
     <td> Date Format must be like (dd-Mmm-yyyy) :</td>
     <td><asp:TextBox ID="txtDate"  runat="server" ></asp:TextBox></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>
      <tr><td> Date Format must be like (dd/mm/yyyy) :</td>
      <td> <asp:TextBox ID="txtDate2" runat="server"></asp:TextBox></td>
     <asp:RegularExpressionValidator ID="regDate2" runat="server"
     ErrorMessage="Please Enter a valid From Date(dd/mm/yyyy)"
     ControlToValidate="txtDate2"  CssClass="color "
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" />

 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.