Geeks With Blogs
Pankaj Tahiliani

There is no need to write any custom code if your are trying to validate your textboxes date values which are in UK format. You can use CompareValidator to perform this action. Download the attached project to see the solution in action. Make sure you have set up the page to use the desired culture with <%@ Page culture="your culture" %>.

Default.aspx
------------------
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" Culture="en-GB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>:: Date Validator ::</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table border="0" cellpadding="2" cellspacing="1">
            <tr>
                <td><asp:Label ID="lblMsg" runat="server" Text="Enter Start Date (dd/mm/yyyy)"></asp:Label></td>
                <td><asp:TextBox ID="txtDate1" runat="server"></asp:TextBox>&nbsp;<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="txtDate1" ErrorMessage="*" Operator="DataTypeCheck" Type="Date"></asp:CompareValidator></td>
            </tr>
            <tr>
                <td><asp:Label ID="lblMsg2" runat="server" Text="Enter End Date (dd/mm/yyyy)"></asp:Label></td>   
                <td><asp:TextBox ID="txtDate2" runat="server"></asp:TextBox>&nbsp;<asp:CompareValidator ID="CompareValidator2" runat="server" ControlToValidate="txtDate2" ErrorMessage="*" Operator="DataTypeCheck" Type="Date"></asp:CompareValidator></td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:CompareValidator ID="cmpStartDate" runat="server"
                        ControlToValidate="txtDate1"
                        ErrorMessage="Start date cannot be less than today's date"
                        Operator="GreaterThanEqual" Type="Date"></asp:CompareValidator>&nbsp;
                    <asp:CompareValidator ID="cmpEndDate" runat="server"
                        ErrorMessage="End date cannot be less than start date"
                        ControlToCompare="txtDate1" ControlToValidate="txtDate2"
                        Operator="GreaterThanEqual" Type="Date"></asp:CompareValidator>
                </td>
            </tr>
            <tr>
                <td colspan="2"><asp:Button ID="btnValidate" runat="server"  Text="Validate"/></td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

Default.aspx.vb
----------------------
Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        cmpStartDate.ValueToCompare = DateTime.Now.ToShortDateString()
    End Sub

    Protected Sub btnValidate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnValidate.Click
        If Page.IsValid Then
            Response.Write("Valid dates entered")
        End If
    End Sub
End Class

Posted on Saturday, July 4, 2009 2:35 AM | Back to top


Comments on this post: How to use compare validator with dd/MM/yyyy format of date

# re: How to use compare validator with dd/MM/yyyy format of date
Requesting Gravatar...
Very nice tip....
Left by Harichaitanya on Feb 28, 2010 11:29 PM

# re: How to use compare validator with dd/MM/yyyy format of date
Requesting Gravatar...
my request is to send the solution for ASP, not for ASP.Net, pl.
Left by manoj bhatt on Jan 13, 2011 4:31 AM

# re: How to use compare validator with dd/MM/yyyy format of date
Requesting Gravatar...
i placed your code but ,
error is coming:The value '' of the ValueToCompare property of 'cmpStartDate' cannot be converted to type 'Date'.
Left by santosh on Sep 24, 2011 4:51 AM

# re: How to use compare validator with MMM d, yyyy format of date?
Requesting Gravatar...
The comparison is not working for different formats, how to handle that?
Left by Prashanth on Dec 01, 2011 10:14 AM

# re: How to use compare validator with dd/MM/yyyy format of date
Requesting Gravatar...
please check the code its not working...........
Left by kapil dev on Dec 28, 2011 7:55 AM

# re: How to use compare validator with dd/MM/yyyy format of date
Requesting Gravatar...
if(datetime.parse(txtstartdate.text)>datetime.parse(txtenddte.text))
{
//comment
}
Left by dilshaf on Jul 21, 2012 12:34 AM

# re: How to use compare validator with dd/MM/yyyy format of date
Requesting Gravatar...
Thaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaanks a lot dude..
i really searched a lot...and u gave too much simple solution........it made me too much happy....once again bundle of thanks
Left by Atif on Aug 06, 2012 11:27 PM

# re: How to use compare validator with dd/MM/yyyy format of date
Requesting Gravatar...
Thank you so much, So useful.
Left by Beny on Oct 30, 2012 9:24 PM

# re: How to use compare validator with dd/MM/yyyy format of date
Requesting Gravatar...
Super!!! Thanks
Left by Saheel Ahmed on Dec 04, 2012 5:35 AM

# re: How to use compare validator with dd/MM/yyyy format of date
Requesting Gravatar...
Almost four years since 2009 and your post is still useful..
Superb..!! Many Thanks..
Left by Jordano on Feb 15, 2013 7:07 PM

# re: How to use compare validator with dd/MM/yyyy format of date
Requesting Gravatar...
Your post completed my 6-hour-long research.
Left by Tomas on May 14, 2013 11:26 AM

# In My database date format is dd/mm/yyyy and not able to apply between query on that format
Requesting Gravatar...

Meanz not find the correct result in between query

select * from tbstd_rec where joiningdate between '1/6/2013' and '10/6/2013'

Return all rows where data exists between 1/6 -- 10/6 either record is 2009,2011,2013 record..
Please help me
Left by Navi on Jun 10, 2013 12:35 PM

# re: How to use compare validator with dd/MM/yyyy format of date
Requesting Gravatar...
Many thanks
Left by Phan Công Phước on Jul 14, 2013 8:41 PM

# re: How to use compare validator with dd/MM/yyyy format of date
Requesting Gravatar...
Thanks for sharing !! This information has saved lot of my time... It helped.
Left by Akash on Jul 23, 2013 5:20 PM

Your comment:
 (will show your gravatar)


Copyright © Pankaj Tahiliani | Powered by: GeeksWithBlogs.net | Join free