Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Aug 2012
    Posts
    24
    Thanks
    5
    Thanked 0 Times in 0 Posts

    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!

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    /^\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.
    Last edited by WolfShade; 10-03-2012 at 08:19 PM.
    ^_^

    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".

  • #3
    New Coder
    Join Date
    Aug 2012
    Posts
    24
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    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 09:34 PM.

  • #5
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    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".

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,302
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    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.

  • #7
    New Coder
    Join Date
    Aug 2012
    Posts
    24
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Okay, thanks everyone! Now I've got multiple solutions to choose from, haha. Thank you!

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,302
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •