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.
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.valueandtheForm.ExpMonth.value.Note: many people dislike statements of the form
if (today.getTime() > expiry.getTime()) return false; else return true;when you can simply writereturn today.getTime() <= expiry.getTime();.