-1

Im wondering if there is a regular expression pattern that will validate if a credit card expiration date is before todays date.

In my form i have users entering in credit card expiration dates, and need some sort of client validation as we use a direct post API with a gateway provider and no server side validation can occur during this process.

The date fields are in 2 seperate fields with month and year but we need to ensure that we catch them before they are submitted to the gateway incase it fails, as the gateway returns ugly string messages that we cant avoid.

Any ideas on this would be greatly appreciated.

user125264
  • 1,711
  • 1
  • 23
  • 47

2 Answers2

0

You can do simple check like this:

function isCCExpired(month, year) {
    var d = new Date();

    return (d.getFullYear() < year || d.getMonth() < month)
}
anubhava
  • 664,788
  • 59
  • 469
  • 547
  • `d.getFullYear() < year || d.getFullYear() === +year && d.getMonth() < month`, actually. – raina77ow Oct 24 '13 at 07:28
  • Also, you don't need to write `if (condition) return true; else return false;`. All you need is `return condition;`. (In some cases you may want to cast to a boolean, but in this case it doesn't matter.) – David Knipe Oct 26 '13 at 21:17
  • Thanks @david, I update it. – anubhava Oct 27 '13 at 00:51
0
new Date().getTime() < new Date(year, month + 1).getTime()

The + 1 compensates for the fact that we want the end of the month, not the start. (This assumes the months are numbered starting from 0.) According to the docs page for Date, it's OK if the month parameter is out of bounds as a result.

David Knipe
  • 3,141
  • 1
  • 15
  • 17