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">
------------------<%@ 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> <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> <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>
<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>
<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> <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> <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>
<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
----------------------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
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
If Page.IsValid Then
Response.Write("Valid dates entered")
End If
End Sub
End Class
No comments:
Post a Comment