/    Sign up×
Community /Pin to ProfileBookmark

How to validate that a date is later than TODAY?

This is the page where I need the date validation to work:
[url]http://watkinsfamilywinery.com/testing/fax.html[/url]

And here’s the HTML for the date as it is:

[CODE]<tr>
<td class=”reqd” align=”right” valign=”top”>*&nbsp;</td>
<td class=”form”>Expiration Date:<br> (ie. 06/07)</td>
<td class=”copy”>month <select name=”ccexpmon” id=”ccexpmon” style=”margin: 0px; font-family: Arial,Helvetica,sans-serif; font-size: 11px; color: #333333; border: solid 1px;”>
<option value=”” selected></option>
<option value=”01″>01</option>
<option value=”02″>02</option>
<option value=”03″>03</option>
<option value=”04″>04</option>
<option value=”05″>05</option>
<option value=”06″>06</option>
<option value=”07″>07</option>
<option value=”08″>08</option>
<option value=”09″>09</option>
<option value=”10″>10</option>
<option value=”11″>11</option>
<option value=”12″>12</option>
</select> / year <input type=”text” name=”ccexpyr” id=”ccexpyr” value=”” style=”margin: 0px; font-family: Arial,Helvetica,sans-serif; font-size: 11px; color: #333333; width: 40px; border: solid 1px;” maxlength=”2″></td>
</tr>[/CODE]

I just want to make sure that the month/year is LATER than today’s date. If it is later, than go ahead and submit the form. If not, then alert the user that the date is invalid.

Here’s the HTML for the button that should call the function:
<a href=”#” onClick=”javascript:return Validator(this) && checkEmail(this) && Mod10(document.myform.CreditCard.value) && summary();”><img src=”images/order_summary.gif” width=”167″ height=”14″ alt=”printer friendly order summary” title=”printer friendly order summary” border=”0″></a>

to post a comment
JavaScript

20 Comments(s)

Copy linkTweet thisAlerts:
@palansanthuJul 19.2006 — U can use
<i>
</i>var today = new Date();
var year=today.getFullYear();
var month=today.getMonth();
var day=today.getDay();// &lt;--I am not sure bout syntax for this line
Copy linkTweet thisAlerts:
@konithomimoJul 22.2006 — That is easy enough:
function verifyExp()
{
var m = parseInt(document.getElementById('ccexpmon').value)-1;
var y = parseInt('20'+document.getElementById('ccexpyr').value);
var now = new Date();
if((y&gt;now.getFullYear())||((y==now.getFullYear())&amp;&amp;(m&gt;now.getMonth())))
return true;
else
alert('I am sorry, but that expiration date that you have entered has passed');
return false;
}
Copy linkTweet thisAlerts:
@JuuitchanJul 22.2006 — getDay() gives the day of the week (Sunday = 0, Monday = 1, etc.).

getDate() gives the day of the month (example: 5 = 5th day of the month).
Copy linkTweet thisAlerts:
@CharlesJul 22.2006 — And Date.getTime() gives you the number of ticks from the dawn of time (1 January 1970). But Date.getTime is called in the background whenever you use a Date as a number so you just need to use if (new Date (year, month - 1, day) &gt; new Date())
Copy linkTweet thisAlerts:
@JuuitchanJul 22.2006 — The parser will try to treat the date as a string before it tries to treat it as a number.

Try these:

javascript:alert(new Date()+0)

javascript:alert(new Date()-0)
Copy linkTweet thisAlerts:
@CharlesJul 22.2006 — The parser will try to treat the date as a string before it tries to treat it as a number.[/QUOTE]No, as with any object when used as a string the Date's toString method is called and when used as a number its valueOf is called. When you use a relational operator you are using both operands as a number. The Date's valuOf method is the same as getTime. &lt;script type="text/javascript"&gt;
alert (new Date() - 1 &lt; new Date()) // true
alert (new Date() - 1 &gt; new Date()) // false
&lt;/script&gt;
Copy linkTweet thisAlerts:
@JuuitchanJul 23.2006 — Konithomimo:

What does parseInt("08") do? Try it.

Also, do we want to be getting today's date from the client?
Copy linkTweet thisAlerts:
@chausfeldauthorJul 23.2006 — Ok, now I'm getting lost... Should I try Konithomimo's solution, or are all of the subsequent replies implying that it won't work?

We won't be getting today's date from the client.

Thanks for everyone's input thus far!
Copy linkTweet thisAlerts:
@chausfeldauthorJul 23.2006 — well, I tried Konithomimo's solution and it tells me that the expiration date has passed, if it's within 2 months of today. I put in 8/06 and 9/06 and it gave me the alert. Once I put in 10/06 it allowed the Order Summary page to appear.

can we get it to allow the following month from today's date? so for now, it would allow 8/06 and after but not 7/06 or before. possible?

thanks!
Copy linkTweet thisAlerts:
@KorJul 23.2006 — parseInt() is a tricky method. It was actually designed to transform a number from a certain base to the base 10, so that, if you omit the base, you might have unpleasent surprises

alert(parseInt('08'))// will return 0, not 8, as the interpretor believes that the number is an octal

use

parseInt('08',10)

or better use the [B]Number()[/B] method

Number('08')
Copy linkTweet thisAlerts:
@konithomimoJul 23.2006 — As kor said, simply replace parseInt with Number and it will work just fine. I updated the script shortly after I posted it, but i guess i forgot to update it in my post.
Copy linkTweet thisAlerts:
@chausfeldauthorJul 24.2006 — thanks so much - I'll give that a try and post my results!
Copy linkTweet thisAlerts:
@JuuitchanJul 24.2006 — [I]"We won't be getting today's date from the client."[/I]

You [I]will[/I] if you use this script! How do you think Date() gets today's date, black magic?
Copy linkTweet thisAlerts:
@konithomimoJul 24.2006 — If you want the date to be todays date, then you must use a server-side script. JS gets the date from the user's computer settings, thus it can be wrong.
Copy linkTweet thisAlerts:
@chausfeldauthorJul 24.2006 — Ok, the script seems to work... BUT.... I don't want to get the date from the user's computer, as konithomimo said, it can then be wrong. I am not clear from the replies - is this indeed how I am getting the date, or is there another way? If so, please advise.

Thanks so much for all your help!
Copy linkTweet thisAlerts:
@CharlesJul 24.2006 — Worse than that, for one out of every ten user JavaScript doesn't work at all. You're going to need some kind of server side scripting. What do you have available?
Copy linkTweet thisAlerts:
@konithomimoJul 24.2006 — with php:

<?php

$month = date("m");

$year = date("y");

?>

Then just pass those to your JS function.
Copy linkTweet thisAlerts:
@chausfeldauthorJul 25.2006 — I don't know what PHP is or how to "pass those to my JS function".... sorry, I'm a total beginner here. More details please?

Thanks!
Copy linkTweet thisAlerts:
@Angry_Black_ManJul 25.2006 — php and asp are server side languages. for your purposes here, it means that any results that those languages return will be based on what the server knows. that means if you use a server side language and tell it to return the date, it returns the date as the server knows it. you must already know what php or asp is, how to use it, and if your server supports these languages in order to make use of this.

people like to suggest using all kinds of cryptic features that have nothing to do with the topic of the forum. sorry you had to be exposed to that, as i know how unhelpful it is out of context.

anyway, javascript is a clientside language, so the clients computer determines the output in a lot of ways. for isntance, if they dont support javascript (or have it turned off), that affects output. if their system time is off, that affects javascript. etc.

there is a good chance, if you use a free web page provider, that they support "server side includes". this means that any web page that ends in an extention ".shtml" will allow you to use codes to return values based on the server. these codes are actually comments in HTML that have specific information inside them that the server can process. if the server undestands whats in there, it will return a dynamic value like the ever changing date, or a users ip address. if the server doesnt understand it, there is nothing shown on the page because HTML treats the output like a comment.

to test, create a web page on your server named "test.[b][i]s[/i]html[/b]"

put the following into your page:

<i>
</i>&lt;!--#echo var="REMOTE_ADDR"--&gt;
&lt;!--#echo var="DATE_LOCAL" --&gt;
&lt;!--#echo var="DATE_GMT" --&gt;
&lt;!--#echo var="REFERRER" --&gt;


if your server supports SSI and your page ends in .shtml, this should show your IP address, local date on the server, GMT based on the local date, and what page referred you to the page the SSI is being run on. check out [url="http://angelfire.com/nj/fgunlimited/test.shtml"]my site[/url] to see what a page that uses SSI should look like when its used properly with the above code.

[url=http://72.14.221.104/search?q=cache:Y1yh9remffoJ:fog.ccsf.edu/~edwong/135B/chapternotes/ch31-40/35/ssi.html+server+side+includes+list+%22REMOTE_ADDR%22+%22date_local%22&hl=en&gl=us&ct=clnk&cd=1&lr=lang_en]click here to see see more SSI examples[/url]

with the information you get from the server, you can then pass it as variables or values to javascript and then try what youre doing here. you just have to know the format your server will return the date, and then using that format, figure out how to use it to your liking.
Copy linkTweet thisAlerts:
@konithomimoJul 25.2006 — people like to suggest using all kinds of cryptic features that have nothing to do with the topic of the forum. sorry you had to be exposed to that, as i know how unhelpful it is out of context.
[/QUOTE]


So giving them the code to use for the value they need (month and year . . . if you had followed their posts you would know that they are trying to validate credit cards) is now referred to as using "cryptic features". that's funny . . . i don't remember seeing that in my programming manual . . . ?

All the OP has to say is what was said, that a further explanation is needed.

The code I provided pulls the month and year from the server (if PHP is installed) and then you can simply set a JS variable to them to use the values in your JS code. First you must test for PHP though. To do so simply create a test page. Here is a basic one:

&lt;p&gt;This page was created at &lt;b&gt;
&lt;?php echo date("h:i:s a", time()); ?&gt;
&lt;/b&gt; on a server running PHP.&lt;/p&gt;


copy that code to a new page and save it as test.php Then try and load the page. If you see the date displayed then you have php installed. If you do have it then post back and we can tell you how to get the values to JS.
×

Success!

Help @chausfeld 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,
)...