Mihail Shishkov
Mihail Shishkov

Reputation: 15797

Regular Expression to describe Credit Card expiry (valid thru) date

I need a regular expression to validate credit card expiry date. Note that the format of the "date" is MM/YY where YY is the year without century and MM is the month (from 01 to 12). I do not need the YY to be in some limited range but only the MM. Thank you in advance.

Upvotes: 4

Views: 10978

Answers (3)

Poppy
Poppy

Reputation: 41

currentDate = new Date()
currenYear = currentDate.getFullYear()

ones_number = currenYear % 10
tens_number = Math.floor(currenYear % 100 / 10)

regrexStr   = '^((0[1-9])|(1[0-2]))\/?((' + tens_number + '[' + ones_number + '-9])|([' + (tens_number + 1) + '-9][0-9]))$'

this can run from now on to until the end of the Earth with format MMYY or MM/YY

Upvotes: 0

bell0
bell0

Reputation: 110

Revise the requirement on why you don't need a range for the year... because, 1. the regular expression you accepted as answer fails your acceptance criteria as "12/99900" 2. Even if you had a better regular expression, it's prone to be buggy and more maintenance work as expired date is allowed to be processed...

in general dynamic data whose limits are changing should not be validated using pattern matching -- unless the pattern changes as well

Upvotes: 0

Adam Zalcman
Adam Zalcman

Reputation: 27233

This regexp does the job:

(0[1-9]|1[0-2])\/[0-9]{2}

Note that it has a capturing group, depending on your regexp engine you can make it non-capturing like this:

(?:0[1-9]|1[0-2])\/[0-9]{2}

Upvotes: 19

Related Questions