View Single Post
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