/    Sign up×
Community /Pin to ProfileBookmark

Date range validation

I am very new to javascript.
I want to validate two a date range using javascript, so that my program would check startdate less than end date.

The below function only check the year, and it works fine.

[CODE]function validate()
{

var start=document.entry.datum1.value;
year = start.substr(0, 4);
month = start.substr(5, 2);
dayval = start.substr(8, 2);

var end=document.entry.datum2.value;
year1 = end.substr(0, 4);
month1 = end.substr(5, 2);
dayval1 = end.substr(8, 2);

var myDate=new Date();
myDate.setFullYear(year,month,dayval);
var today = new Date();
today.setFullYear(year1,month1,dayval1);

if (myDate> today)
{
alert(“The start date must come before the end date”);
return false
}

return true;
}[/CODE]

But I have write a function for checking the whole date value and it did not work.
My date is in format-
2008-10-23(yyyy-mm-dd)

[CODE]function validate()
{
var start=entry.datum1.value;
year = start.substr(0, 4);
month = start.substr(5, 2);
dayval = start.substr(8, 2);

intyear = parseInt(year, 10);
intmonth = parseInt(month, 10);
intdayval = parseInt(dayval, 10);

var end=entry.datum2.value;
year1 = end.substr(0, 4);
month1 = end.substr(5, 2);
dayval1 = end.substr(8, 2);

intyear1 = parseInt(year1, 10);
intmonth1 = parseInt(month1, 10);
intdayval1 = parseInt(dayval1, 10);

var startDate = Date.parse(intyear,intdayval,intmonth);
var endDate = Date.parse(intyear1,intdayval1,intmonth1);

if (startDate > endDate)
{
alert(“The start date must come before the end date”);
return false;
}
return true;

}[/CODE]

Please help

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@justinbarneskinMay 24.2010 — You'll need to find a way to get your date formated so code can read it-
[code=html]var startDate="July 12, 1987";
alert(Date.parse(startDate))
[/code]
×

Success!

Help @newto spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...