How do I add a expiration date validation to this credit card form
<table border="1">
<tr>
<td>
<label for="month">Expiration Month</label>
<select name="month">
<script language="JavaScript" type="te开发者_JAVA百科xt/javascript">
var month = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec");
for (var m=0; m<month.length; m++)
{
document.write("<option>" +month[m]+ "</option>");
}
</script>
</select>
</td>
<td>
<label for="year">Expiration Year</label>
<select name="year">
<script language="JavaScript" type="text/javascript">
var year = new Date();
var year2 = year.getFullYear();
for (y=0; y<6; y++)
{
document.write("<option>" +(year2+y)+ "</option>");
document.write("<br />");
}
</script>
</select>
</td>
</tr>
<tr><td>
<input type="submit" value="Submit">
<input type="reset" value="reset">
</tr>
</td>
</form>
</table>
</div>
</body>
</html>
Here's the first response i got when searching for "credit card expiration validation javascript" on Google.
http://perezj.blogspot.com/2008/02/credit-card-expiration-date-validation.html
Always makes me wonder why people post questions on StackOverflow before doing a search on the internet themselves.
Here's the function (if the link doesnt work):
function ValidateExpDate()
{
var ccExpYear = 20 + $F('<%= txtCCExpirationYear.ClientID%>');
var ccExpMonth = $F('<%= txtCCExpirationMonth.ClientID%>');
var expDate=new Date();
expDate.setFullYear(ccExpYear, ccExpMonth, 1);
var today = new Date();
if (expDate<today)
{
// Credit Card is expire
return false;
}
else
{
// Credit is valid
return true;
}
}
精彩评论