Validate from and to date through java script in asp.net c#

Posted by Gopal_nivas under ASP.NET on 8/4/2011 | Points: 10 | Views : 3484 | Status : [Member] | Replies : 2
i am using ajax calender. in that i was giving two dates in two text boxes i.e)from date and to date.

now i want to validate the from date and to date through javascript.

validation:
- validate check start date and enddate
- check from date year should be less than the to date.

need ur suggestions with examples


regards
gopal.s




Responses

Posted by: Hmanjarawala on: 8/4/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi Gopal, here is javascript for same

function compare(fromdate, todate)
{
if (Date.parse(fromDate) > Date.parse(toDate)) {
alert("Invalid Date Range!\nStart Date cannot be after End Date!")
return false;
}
return true;
}

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Gopal_nivas, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nithadeepak on: 8/4/2011 [Member] Bronze | Points: 25

Up
0
Down
function ValidateVenueDate() 

{
var startDate = document.getElementById("txtSdate").value;
var endDate = document.getElementById("txtEdate").value;
CheckDates(startDate, endDate);

}
function CheckDates(strdate,enddate)
{
var sd = strdate.slice(3, [5]);
var ed = enddate.slice(3, [5]);
var sm = strdate.slice(0, [2]);
var em = enddate.slice(0, [2]);
var sy = strdate.slice(6, [11]);
var ey = enddate.slice(6, [11]);
var start = new Date();
start.setFullYear(sy);
start.setMonth(sm);
start.setDate(sd);
var end = new Date();
end.setFullYear(ey);
end.setMonth(em);
end.setDate(ed);
var oneDay = 1000 * 60 * 60 * 24;
var difMilli = Math.ceil(end.getTime() - start.getTime());
var diff = Math.round(difMilli / oneDay);
if (diff < 0)
{
alert("End Date Should come after Start Date");
return false;
}
}


Nitha Deepak

Gopal_nivas, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response