Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-03-2012, 07:04 PM   PM User | #1
taypandt
New Coder

 
Join Date: Aug 2012
Posts: 24
Thanks: 5
Thanked 0 Times in 0 Posts
taypandt is an unknown quantity at this point
Regular expression troubles

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!
taypandt is offline   Reply With Quote
Old 10-03-2012, 07:16 PM   PM User | #2
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 950
Thanks: 7
Thanked 98 Times in 98 Posts
WolfShade is an unknown quantity at this point
/^\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.
__________________
^_^

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..
WolfShade is offline   Reply With Quote
Old 10-03-2012, 07:31 PM   PM User | #3
taypandt
New Coder

 
Join Date: Aug 2012
Posts: 24
Thanks: 5
Thanked 0 Times in 0 Posts
taypandt is an unknown quantity at this point
Quote:
Originally Posted by WolfShade View Post
/^\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.
taypandt is offline   Reply With Quote
Old 10-03-2012, 08:19 PM   PM User | #4
mrhoo
Regular Coder

 
Join Date: Mar 2006
Posts: 708
Thanks: 30
Thanked 127 Times in 118 Posts
mrhoo will become famous soon enoughmrhoo will become famous soon enough
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);

Last edited by mrhoo; 10-03-2012 at 08:34 PM..
mrhoo is offline   Reply With Quote
Old 10-03-2012, 08:21 PM   PM User | #5
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 950
Thanks: 7
Thanked 98 Times in 98 Posts
WolfShade is an unknown quantity at this point
Yah.. I'm tired..

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

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".
WolfShade is offline   Reply With Quote
Old 10-03-2012, 09:26 PM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 10-04-2012, 07:57 AM   PM User | #7
taypandt
New Coder

 
Join Date: Aug 2012
Posts: 24
Thanks: 5
Thanked 0 Times in 0 Posts
taypandt is an unknown quantity at this point
Okay, thanks everyone! Now I've got multiple solutions to choose from, haha. Thank you!
taypandt is offline   Reply With Quote
Old 10-04-2012, 08:06 AM   PM User | #8
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by taypandt View Post
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.
__________________

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.
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:14 AM.


Advertisement
Log in to turn off these ads.