How about:
/^(?=2.*\d)[a-zA-Z0-9]{8}$/
if the number 2
counts for one of the 2 required number.
/^(?=2.*\d.*\d)[a-zA-Z0-9]{8}$/
if the number 2
doesn't count for one of the 2 required number.
explanation:
The regular expression:
(?-imsx:^(?=2.*\d)[a-zA-Z0-9]{8}$)
matches as follows:
NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching
) (matching whitespace and #
normally):
----------------------------------------------------------------------
^ the beginning of the string
----------------------------------------------------------------------
(?= look ahead to see if there is:
----------------------------------------------------------------------
2 '2'
----------------------------------------------------------------------
.* any character except
(0 or more times
(matching the most amount possible))
----------------------------------------------------------------------
\d digits (0-9)
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
[a-zA-Z0-9]{8} any character of: 'a' to 'z', 'A' to 'Z',
'0' to '9' (8 times)
----------------------------------------------------------------------
$ before an optional
, and the end of the
string
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------