locked
Regular expression for Credit Card expiration date validation.

    Question

  • Hi
    I am developing a asp.net application which has a form for credit card validations.

    I am using regular expressions to validate the fields.

    The expiration date is entered into two different text boxes in the mm yy format.

    textbox1 : mm
    textbox2 : yy

    I need a regular expression to check for the validity of the expiration date, such that it also checks if the mm/yy combination is not older than the current dates mm/yy combination.

    example :
    current date's mm/yy is 08/09

    so the entered values should not be less than this date.

    Any help would be greatly appreciated.
    Wednesday, August 19, 2009 5:01 PM

Answers

  • I agree that the server side should do the ultimate validation.  And I agree that Regex doesn't understand current date.  However, you can use the ASP.NET RegularExpressionValidator control to do some of the clientside work.  Since you've split your month and year into different fields, each are just validated as reasonable 2 digit numbers.  The pattern for month would be (0[1-9]|1[12]), for year the pattern would be (09|[1-9][0-9]).  Notice the year does not allow 00-08 as those are old years.

    That being said, I would do these with drop down lists.  When a month is chosen, remove invalid years from the year dropdown.  And when the year is chosen, remove any invalid months from the month dropdown.  And by the way, there is usually a limit to the maximum expiration date, you may want to consider that, too.


    Les Potter, Xalnix Corporation, Yet Another C# Blog
    Thursday, August 20, 2009 2:57 PM

All replies

  • A single regular expression cannot take into account the current date.  This kind of data really needs to be validated server side, and for client-side validation, a javascript method would be far better than regex here.  Beware, however, client-side code is easily hacked, so ensure you're validating server-side as well, especially when dealing with information as sensitive as credit card information.
    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiTwitterLinkedInForumsBrowser
    • Proposed as answer by JohnGrove Thursday, August 20, 2009 1:00 AM
    Wednesday, August 19, 2009 5:06 PM
  • So how can I attach a javascript to the submit button in my form. I am new to programming and this is my first time that I am dealing with things like this.
    Wednesday, August 19, 2009 5:17 PM
  • Sure.  This question is off-topic for this forum, however. 

    Try re-asking this at http://forums.asp.net/.  That's where ASP.NET questions should be asked.
    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiTwitterLinkedInForumsBrowser
    Wednesday, August 19, 2009 5:19 PM
  • I agree that the server side should do the ultimate validation.  And I agree that Regex doesn't understand current date.  However, you can use the ASP.NET RegularExpressionValidator control to do some of the clientside work.  Since you've split your month and year into different fields, each are just validated as reasonable 2 digit numbers.  The pattern for month would be (0[1-9]|1[12]), for year the pattern would be (09|[1-9][0-9]).  Notice the year does not allow 00-08 as those are old years.

    That being said, I would do these with drop down lists.  When a month is chosen, remove invalid years from the year dropdown.  And when the year is chosen, remove any invalid months from the month dropdown.  And by the way, there is usually a limit to the maximum expiration date, you may want to consider that, too.


    Les Potter, Xalnix Corporation, Yet Another C# Blog
    Thursday, August 20, 2009 2:57 PM