Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
-3
rated 0 times [  2] [ 5]  / answers: 1 / hits: 22073  / 13 Years ago, tue, may 3, 2011, 12:00:00

having some trouble trying to figure this out, complete novice when it come to JavaScript. I'm trying to validate a form, more specific the expiry date of a credit card.
e.g jan 2011 would be invalid but dec 2011 would be valid etc.
This is what i have so far:



function ExpiryDate() {

var year
var months

today = new Date();
expiry = new Date(year, month);
if (today.getTime() > expiry.getTime())

return false;
else
return true;
};


my form reads:



<form id=myform method=post action=default.html onsubmit=return Validate(this);>

<p>Expiration Date: Month

<select name=ExpMon id=ExpMon title=select a month>
<option value=1>Jan</option>
<option value=2>Feb</option>
<option value=3>Mar</option>
<option value=4>Apr</option>
<option value=5>May</option>
<option value=6>June</option>
<option value=7>July</option>
<option value=8>Aug</option>
<option value=9>Sept</option>
<option value=10>Oct</option>
<option value=11>Nov</option>
<option value=12>Dec</option>
</select>

Year:
<select name=ExpYear id=ExpYear title=select a year>
<option value=2010>2010</option>
<option value=2011>2011</option>
<option value=2012>2012</option>
<option value=2013>2013</option>
<option value=2014>2014</option>
<option value=2015>2015</option>
</select></p>

</form


I'm not to sure how to use the id's to get what I want, any help would be appreciated, thanks in advance. note:I have a function called Validate which validates a text field in my form so i could attach it to this somehow.


More From » date

 Answers
22
var year = document.getElementById(ExpYear).value;
var month = document.getElementById(ExpMon).value;


If you prefer, you can use the names instead; you'd have to get Validate to pass the form element in as a parameter, but then you can use theForm.ExpYear.value and theForm.ExpMonth.value.



Note: many people dislike statements of the form if (today.getTime() > expiry.getTime()) return false; else return true; when you can simply write return today.getTime() <= expiry.getTime();.


[#92422] Monday, May 2, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
blair

Total Points: 384
Total Questions: 108
Total Answers: 86

Location: Northern Ireland
Member since Tue, May 5, 2020
4 Years ago
;