Pankaj Tahiliani

  Home  |   Contact  |   Syndication    |   Login
  29 Posts | 0 Stories | 43 Comments | 0 Trackbacks

News

Twitter












Archives

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

Feedback

# re: How to use compare validator with dd/MM/yyyy format of date 2/28/2010 11:29 PM Harichaitanya
Very nice tip....

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


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

# re: How to use compare validator with MMM d, yyyy format of date? 12/1/2011 10:14 AM Prashanth
The comparison is not working for different formats, how to handle that?

# re: How to use compare validator with dd/MM/yyyy format of date 12/28/2011 7:55 AM kapil dev
please check the code its not working...........

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

# re: How to use compare validator with dd/MM/yyyy format of date 8/6/2012 11:27 PM Atif
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

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

# re: How to use compare validator with dd/MM/yyyy format of date 12/4/2012 5:35 AM Saheel Ahmed
Super!!! Thanks

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

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

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: