Page 1 of 1

JS date range validation ??

Posted: Thu Feb 22, 2007 2:48 pm
by PHPycho
I have two date field viz from-date and to-date
and i want to validate that from-date should be less than or equal to to-date
using javascript.
Note: date field should be in yy-mm-dd format
How is that possible ?
Please help me..
Thanks in advance..

Posted: Thu Feb 22, 2007 3:07 pm
by Christopher
Javascript has a Date class:

http://www.w3schools.com/js/js_obj_date.asp

Posted: Fri Feb 23, 2007 5:05 am
by Kieran Huggins

Posted: Fri Mar 02, 2007 8:41 am
by PHPycho
I got my problem solved as:

Code: Select all

//find out the current date in Y-m-d format
var currentTime = new Date();
var thisYear = currentTime.getFullYear();
var thisMon = currentTime.getMonth()+1;
var thisDay = currentTime.getDate();

var ymd = thisYear+"-"+thisMon+"-"+thisDay;
//get the value from date field
var selYmd = document.getElementById("from/to").value;

if(selYmd < ymd)
{
alert("Error message goes here..");
return false;
}