Compare two date using CompareValidator and Javascript

If you want to Use two Date like from date and to date and From date is Always
less than to date than write script for compare two date and call function to
ClientValidationFunction


  <script type="text/javascript" language="javascript">
function CompareDates(source, args)
{
var str1 = document.getElementById("txtFromDate").value;
var str2 = document.getElementById("txtToDate").value;
var dt1 = parseInt(str1.substring(0,2),10);
var mon1 = parseInt(str1.substring(3,5),10);
var yr1 = parseInt(str1.substring(6,10),10);
var dt2 = parseInt(str2.substring(0,2),10);
var mon2 = parseInt(str2.substring(3,5),10);
var yr2 = parseInt(str2.substring(6,10),10);
var date1 = new Date(yr1, mon1, dt1);
var date2 = new Date(yr2, mon2, dt2);
if(date2 < date1)
{
//alert("To date cannot be greater than from date");
args.IsValid = false;
}
else
{
//alert("Submitting ...");
args.IsValid = true;
}
}
</script>


<table>
<tr>
<td>
From Date:
</td>
<td>
<asp:TextBox ID="txtFromDate" runat="server"></asp:TextBox>DD/MM/YYYY
</td>
</tr>
<tr>
<td>
To Date:
</td>
<td>
<asp:TextBox ID="txtToDate" runat="server"></asp:TextBox>DD/MM/YYYY
<asp:CustomValidator ClientValidationFunction="CompareDates" ID="cvTodate" runat="server"
ErrorMessage="To date cannot be greater than from date" ControlToValidate="txtToDate"></asp:CustomValidator>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnSaver" runat="server" Text="Save" />
</td>
</tr>
</table>
If you have any query Reagarding This then comment me.
Ebook Download
View all
Learn
View all