如何在此信用卡表单中添加到期日期验证

时间:2010-06-09 04:25:12

标签: javascript validation

<table border="1">
<tr>
<td>
<label for="month">Expiration Month</label>
<select name="month"> 
<script language="JavaScript" type="text/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>

1 个答案:

答案 0 :(得分:4)

这是我在Google上搜索“信用卡到期验证javascript”时获得的第一个回复。

http://perezj.blogspot.com/2008/02/credit-card-expiration-date-validation.html

总是让我想知道为什么人们在自己在互联网上搜索之前就会在StackOverflow上发布问题。

这是函数(如果链接不起作用):

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;

    }

}