Friday, August 10, 2012

Javascript From Date Less Than Current Date Validation


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>

<!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>Untitled Page</title>
<script type="text/javascript">

function dateValidation() {

var obj = document.getElementById("<%=txtfrom.ClientID%>");
var day = obj.value.split("/")[1];

var month = obj.value.split("/")[0];
var year = obj.value.split("/")[2];


////todate
var obj2 = document.getElementById("<%=txtto.ClientID%>");
var day2 = obj2.value.split("/")[1];

var month2 = obj2.value.split("/")[0];
var year2 = obj2.value.split("/")[2];



if ((day < 1 || day > 31) || (month < 1 && month > 12) && (year.length != 4)) {

alert("Invalid Format"); return false;
}

else {

var dt = new Date(year, month - 1, day);
var today = new Date();

var dt2 = new Date(year2, month2 - 1, day2);
var today2 = new Date();

if ((dt.getDate() != day) || (dt.getMonth() != month - 1) || (dt.getFullYear() != year) || (dt < today)) {

//alert("Invalid from Date");
alert("From Date cannot be less than Current date");
return false;
}

if ((dt2.getDate() != day2) || (dt2.getMonth() != month2 - 1) || (dt2.getFullYear() != year2) || (dt2 < today2)) {
alert("To Date cannot be less than Current date");
// alert("Invalid to Date");
return false;
}

if(dt > dt2) {
alert("Start date can not greater than End date.");
return false;
}

// CompareDate(txtfrom, txtto);

}

}


function CompareDate(txtfrom, txtto) {
var From = txtfrom.value;
var To = txtto.value;
var strSplitFrom = From.split('/')
var myDateFrom = new Date();
myDateFrom.setFullYear(strSplitFrom[2], strSplitFrom[1] - 1, strSplitFrom[0]);
var strSplitTo = To.split('/')
myDateTo = new Date();
myDateTo.setFullYear(strSplitTo[2], strSplitTo[1] - 1, strSplitTo[0]);
if (myDateFrom > myDateTo) {
alert("Start date can not greater than End date.");
txtfrom.focus();
return false;
}
else {
return true;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<p><b>DD/MM/YEAR Format</b></P>
From Date<asp:TextBox ID="txtfrom" runat="server"></asp:TextBox>
<br/>
To Date
<asp:TextBox ID="txtto" runat="server"></asp:TextBox>
<br/>
<asp:Button ID="Button1" runat="server" Text="Click" OnClientClick="return dateValidation()" />
</div>
</form>
</body>
</html>

0 comments:

Post a Comment