View Single Post
Old 10-03-2012, 09:26 PM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
It may be a good idea to check that the entered date is valid (not just correct format) and the card has not expired.

Code:
<script type = "text/javascript">

function checkCCExpiryDate() { 
var now = new Date();
var dateA = "9-2012";  // the date entered by the user
dateA = dateA.replace(/\-|:|\./g, "/"); // hyphen colon dot all allowed
var ds = dateA.split("/");

var mth = Number(ds[0])
if ((mth <1) || (mth > 12)) {
alert ("Invalid month.  Please re-enter 1-12");
// clear the field
return false;
}

var yr = Number(ds[1]);
if (yr<2000) {yr = yr+2000}

if (yr<now.getFullYear()) {
alert ("The card has expired. Please enter a valid expiry date.");
// clear the field
return false;
}

if ((yr == now.getFullYear()) && (mth < now.getMonth()+1 )) {  // months in Javascript are 0-11
alert ("The card has expired.  Please enter a valid expiry date.");
// clear the field
return false;
}

if (yr > (now.getFullYear() + 7)) {
alert ("Invalid expiry year too far in futue.  Please re-enter.");
// clear the field
return false;
}

}

</script>
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote