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!
If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
* The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
Last edited by WolfShade; 10-03-2012 at 07:19 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.
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;
alert(rx.test(datestring));
// The parentheses and /g flag can be used to test the expiration date-
Code:
// 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))
}
else alert(NaN);
If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
* The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
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.