Monday, February 23, 2009

Custom Validator to validate Date

This is a custom validation that tests if the given date is in 30 days from today. (This validator throws error when the date is less than today or when the date is greater than 30 days from today)

--------------------------------------------------------------------------------------
This post is purely for my personal reference. :-)
-----------------------------------------------------------------------------------------


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>Untitled Page</title>
<script language="javascript" type="text/javascript">
function datevalidate(sender, args)
{
var dd = document.getElementById("<%=TextBox1.ClientID%>").value;
var mm = document.getElementById("<%=TextBox2.ClientID%>").value;
var yy = document.getElementById("<%=TextBox3.ClientID%>").value;
//Why can't I take date from Javascript ? (I want server's date as per my requirement)
var ddnow = document.getElementById("<%=hdnDay.ClientID%>").value;
var mmnow = document.getElementById("<%=hdnMonth.ClientID%>").value;
var yynow = document.getElementById("<%=hdnYear.ClientID%>").value;
mm = mm-1; // javascript month range 0 - 11
mmnow = mmnow-1; //Month ranges from 0-11 in Javascript
var tempDate = new Date(yy,mm,dd);
var today = new Date(2009,1,23);
var d = new Date();
d.setFullYear(yynow,mmnow,ddnow+30);
//First check if the given date is valid.
if ( (yy == tempDate.getFullYear()) && (mm == tempDate.getMonth()) && (dd == tempDate.getDate()) )
{
if(tempDate < today)
{
args.IsValid = false;
}
else
{
if(tempDate < d)
args.IsValid = true;
else
args.IsValid = false;
}
}
// if ( (yy == tempDate.getFullYear()) && (mm == tempDate.getMonth()) && (dd == tempDate.getDate()) )
// {
// args.IsValid=true;
// } else
// {
// args.IsValid=false;
// }
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:textbox id="TextBox1" runat="server"></asp:TextBox>
<asp:textbox id="TextBox2" runat="server"></asp:TextBox>
<asp:textbox id="TextBox3" runat="server"></asp:TextBox>
<asp:customvalidator id="custdate" runat="server" controltovalidate="TextBox3" clientvalidationfunction="datevalidate" errormessage="Invalid date"></asp:CustomValidator>
<br />
<asp:button id="Button1" runat="server" text="Button">
<input type="hidden" id="hdnDay" runat="server">
<input type="hidden" id="hdnMonth" runat="server">
<input type="hidden" id="hdnYear" runat="server">
</div>
</form>
</body>
</html>


-----------------------------------------------------------------
Code-behind
-----------------------------------------------------------------


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
hdnDay.Value = DateTime.Now.ToString("dd");
hdnMonth.Value = DateTime.Now.ToString("MM"); //m is for minutes. M is for month.
hdnYear.Value = DateTime.Now.ToString("yyyy");
}
}