Regex to match a 2-digit number (to validate Credit/Debit Card Issue number)


I would like to use regex to match a string of exactly 2 characters, and both of those characters have to be between 0 and 9. The string to match against would be coming from a single-line text input field when an ASP.NET MVC view is rendered

So far, I have the regex


and from the following list of example string inputs

  • 456
  • 55 44
  • 12

the following matches are returned when I apply the regex

  • 45
  • 55
  • 12

So, I have kind of half the solution....what I actually want to enforce is that the string is also exactly 2 characters long, so that from the list of strings, the only one that should be matched is


I am an admitted amateur at regular expressions and am just using this to validate a card issue number on an ASP.NET MVC model as below....

public string IssueNumber { get; set; }

I'm sure that what i'm asking is quite simple but I wasn't able to find any examples that limited the length as part of the matching .

Thanks, in advance.

This question is tagged with regex

~ Asked on 2013-09-20 17:34:59

The Best Answer is


You can use the start (^) and end ($) of line indicators:


Some language also have functions that allows you to match against an entire string, where-as you were using a find function. Matching against the entire string will make your regex work as an alternative to the above. The above regex will also work, but the ^ and $ will be redundant.

~ Answered on 2013-09-20 17:36:55


You need to use anchors to match the beginning of the string ^ and the end of the string $


~ Answered on 2013-09-20 17:36:52

Most Viewed Questions: