I am building a user registration form using C# with .NET.
I have a requirement to validate user entered password fields.
Validation requirement is as below.
- It should be alphanumeric (a-z , A-Z , 0-9)
- It should accept 6-10 characters (minimum 6 characters, maximum 10 characters)
- With at least 1 alphabet and number (example:
stack1over)
I am using a regular expression as below.
^([a-zA-Z0-9]{6,10})$
It satisfies my first 2 conditions.
It fails when I enter only characters or numbers.
Pass it through multiple regexes if you can. It’ll be a lot cleaner than those look-ahead monstrosities 🙂
Though some might consider it clever, it’s not necessary to do everything with a single regex (or even with any regex, sometimes – just witness the people who want a regex to detect numbers between 75 and 4093).
Would you rather see some nice clean code like:
or something like:
I know which one I’d prefer to maintain 🙂