Wednesday, November 23, 2011

How to use CompareValidator to validate dates in different format (e.g., dd/MM/yyyy)?

If you want to use the ASP.NET CompareValidator control to validate dates in a different format, you can setup the Page to your desired culture. You can do this by either setting up the value in the page directive as shown below:

<%@ Page culture="your culture here" %>

Or by setting it up in the page load event:


protected void Page_Load(object sender, EventArgs e)
{
   if (!Page.IsPostBack)
   {
     Page.Culture = "your culture here";
   }
}

For example if you want to validate a date in Australian Date Format, which is dd/MM/yyyy, you can do:

<%@ Page culture="en-AU" %>

Or: 



protected void Page_Load(object sender, EventArgs e)
{
   if (!Page.IsPostBack)
   {
     Page.Culture = "en-AU";
   }
}

No comments:

Post a Comment