Saturday, 25 March 2017

Compare dates in dd/mm/yyyy format using ASP.Net CompareValidator.

Compare dates in dd/mm/yyyy format using ASP.Net CompareValidator

Description:

in this example we explain that how to compare dates in dd/mm/yyyy format using compare validator in asp.net.by default compare validator are worked in mm/dd/yyyy format so there are many question are raised in our mind like compare validator are not worked for dd/mm/yyyy format or for dates compare validator are not works.

So to fire compare validator in dd/mm/yyyy format we need to change the Culture property of the page to “en-GB” in the @Page directive of the asp.net Page. if you are using the Master page concept and if the compare validator are not worked for dd/mm/yyyy format then you can set the page culture in code behind page of the aspx page like.
AspxPage:

<%@ Page Language="C#" AutoEventWireup="false" CodeFile="CompareValidator.aspx.cs"
    Inherits="CompareValidator" 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 id="Head1" runat="server">
    <title>Compare Validator for dd/mm/yyyy format in asp.net using compare validator.
    </title>
</head>
<body>
    <form id="form1" runat="server">
    From Date:
    <asp:TextBox ID="txtFromDate" runat="server" Text="18/02/2015"></asp:TextBox>&nbsp;
    To Date:
    <asp:TextBox ID="txtToDate" runat="server" Text="19/12/2016"></asp:TextBox><br />
    <asp:CompareValidator ID="CompareValidator1" ValidationGroup="Date" ForeColor="Red"
        runat="server" ControlToValidate="txtFromDate" ControlToCompare="txtToDate" Operator="LessThan"
        Type="Date" ErrorMessage="Start date must be less than End date."></asp:CompareValidator>
    <br />
    <asp:Button ID="btnCompare" runat="server" Text="Compare" ValidationGroup="Date" />
    </form>
</body>
</html>




0 comments:

Post a Comment