...

# Regular expression troubles

taypandt
10-03-2012, 08:04 PM
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.

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!

10-03-2012, 08:16 PM
/^\d{1,2}[\/-]\d{2,4}\$/ should do it. One or two numbers, followed by either / or -, followed by either 2 or 4 numbers.

If you want to make sure that the month or year is within a certain range http://www.the-art-of-web.com/javascript/validate-date/ scroll to #2.

taypandt
10-03-2012, 08:31 PM
/^\d{1,2}[\/-]\d{2,4}\$/ should do it. One or two numbers, followed by either / or -, followed by either 2 or 4 numbers.

But that expression also allows the year to be a 3-digit number since using the syntax n{x,y} allows a number of digits anywhere between and including x and y. I need it to just accept two or four digits, and not anything else.

mrhoo
10-03-2012, 09:19 PM
It isn't much more trouble to make sure the first number group
is between 01(or 1) and 12.

var datestring= '02/2014';
var rx=/^(0?[1-9]|1[0-2])[\/-](\d\d(\d\d)?)\$/g;

// The parentheses and /g flag can be used to test the expiration date-

// The parentheses and /g flag can be used to test the expiration date-

var datestring= '10/12';
var rx=/^(0?[1-9]|1[0-2])\D(\d\d(\d\d)?)\$/g;

var day= rx.exec(datestring);

if(day){
var today= new Date(),
ty= today.getFullYear(), tm= today.getMonth(),
y= day[2]- 0;

if(y<100) y+= 2000;
alert(y>ty || (y== ty && day[1]-1>= tm))
}

10-03-2012, 09:21 PM
Yah.. I'm tired.. :)

/^\d{1,2}[\/-](\d{2}|\d{4})\$/

Philip M
10-03-2012, 10:26 PM
It may be a good idea to check that the entered date is valid (not just correct format) and the card has not expired.

<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)) {
// clear the field
return false;
}

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

if (yr<now.getFullYear()) {
// clear the field
return false;
}

if ((yr == now.getFullYear()) && (mth < now.getMonth()+1 )) { // months in Javascript are 0-11
// clear the field
return false;
}

if (yr > (now.getFullYear() + 7)) {
// clear the field
return false;
}

}

</script>

taypandt
10-04-2012, 08:57 AM
Okay, thanks everyone! Now I've got multiple solutions to choose from, haha. :) Thank you!

Philip M
10-04-2012, 09:06 AM
Okay, thanks everyone! Now I've got multiple solutions to choose from, haha. :) Thank you!

Yes, that is one of the great benefits of a forum such as this. :)
As we often say, there is usually more than one way to skin a cat.