Thursday, January 17, 2008

To check whether a date is in the past using Javascript

With a valid date string format say mm/dd/yy, we can check whether a date is in the past or not using the javascript Date object.

<html:text property="trinDateStr" onblur="if(!checkDateNotInPast(this)){return false;}">

<script type="text/javascript">
function checkDateNotInPast(obj)
{
var vDate = Date.parse(obj);
var today = new Date().getTime();
if(vDate < today)
{
alert("Invalid: Date is in past");
obj.focus();
obj.select();
return false;
}
else
{
return true;
}
}
<script>

1 comment:

IBRAHIM said...

The date is not taking the current date .. any solutions?..