javaregexvalidationdate

validate the credit card expiry date using java?


I am looking to validate the credit card expiry date in MM/YY format . I have no clue on how to validate , whether to go for Simple date format / Regex .

Appreciate your help.


Solution

  • Playing devil's advocate...

    boolean validateCardExpiryDate(String expiryDate) {
        return expiryDate.matches("(?:0[1-9]|1[0-2])/[0-9]{2}");
    }
    

    which translates as:

    ...so this version requires zero-padded months (01 - 12). Add a ? after the first 0 to prevent this.