/    Sign up×
Community /Pin to ProfileBookmark

Validate Date

Hello all, hope you are well.

I have a script that allows users to select a date from three drop down lists.

What I want to do now is check to make sure the date selected is not before the current date.

I imagine I have to use the “new Date” function to compare however I am having trouble with it! Can any one point me in the right direction with this code or suggest a new method if this one is doomed! lol

[CODE]document.forms.order.os4.value[/CODE]

the above code points to the field where the date selected is stored and is in the format yyyy-mm-dd. I used the replace function to convert it to yyyy,mm,dd for the new Date function.

[CODE]<SCRIPT language=javascript type=text/javascript>
function validateForm()
{
var x=(document.forms.order.os4.value);
var y=x.replace(/-/g, “,”);
var z=new Date(y);
var today = new Date();

if (z<today)
{
alert(“Date selected is before today!”);
return false;
}
else
{
return true;
}
}
</SCRIPT>
[/CODE]

Thanks very much in advance for any input, it is much appreciated.

Kind regards, Mike

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@moritanaMar 25.2011 — well, first of all you should remember that the months in new date are always -1, like january is 0.

second, your date should be inside these '', aka:

var z=new Date("'"+y+"'");

and you should make sure the structure of the date matches.

someone else tried to do that here, and made it :

http://forums.asp.net/t/1193809.aspx
Copy linkTweet thisAlerts:
@sss_1234authorMar 26.2011 — Hi moritana, thanks a lot for your quick response.

I have managed to get it working in chrome and firefox however not in IE explorer. It doesn't display any errors, but just won't show the alert box either!

Any thoughts?

I had a quick look on google, and found that using a string as input in new Date is risky. Should I try convert it to a number?

Thanks again for your input.

Kind regards, Mike
×

Success!

Help @sss_1234 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.1,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...