I'm trying to make a form that validates a credit card number, and in the field where the user enters the expiration date, I want to validate the date to make sure it's in either mm/yyyy or mm/yy format.
Code:
function tellValidDate(dateA) {
var pattern = /^\d{1,2}(\/)|-(\d{2}$)|(\d{4}$)/;
if (pattern.test(dateA))
return true;
else
return false;
}
The problem is that this returns true when it shouldn't. If a user enters a number without any dashes or hyphens (e.g. "2", "3245", etc.), it returns false. But if a user enters a number followed by a dash or hyphen, regardless of the number of integers that follow it (e.g. "12/", "2/1", "4/43214", etc.) it returns true.
Is there something wrong with my expression? Thanks in advance to anyone who helps!