JS date range validation ??

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

JS date range validation ??

Post 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..
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Javascript has a Date class:

http://www.w3schools.com/js/js_obj_date.asp
(#10850)
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Post 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;
}

Post Reply