Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need a regex for ONLY Alphanumeric (no pure numbers or letters) AND limit to exactly 10 characters?

Tags:

regex

I've run into some issues with this one and cannot find it in past questions.

Criteria:

  1. Reject pure digits
  2. Reject pure letters
  3. Reject any symbols
  4. Accept ONLY Alphanumeric combo
  5. MUST be equal to 10 characters total

Here is what I have made and the problems with each:

  1. ^(?!^\d*$)[a-zA-Z\d]{10}$

    • This fails criteria #2
  2. ^[a-zA-Z0-9]{10}$

    • This fails criteria #1

I have tried some others that meet all criteria but fail the 10 char limit.

Any help is appreciated.

like image 777
LIMS Guru Avatar asked Mar 05 '23 04:03

LIMS Guru


1 Answers

You may use a second lookahead:

^(?!\d+$)(?![a-zA-Z]+$)[a-zA-Z\d]{10}$

See the regex demo and the Regulex graph:

enter image description here

Details

  • ^ - start of string
  • (?!\d+$) - a negative lookahead that makes sure the whole string is not composed of just digits
  • (?![a-zA-Z]+$) - the whole string cannot be all letters
  • [a-zA-Z\d]{10} - 10 letters or digits
  • $ - end of string.
like image 90
Wiktor Stribiżew Avatar answered May 18 '23 22:05

Wiktor Stribiżew