vml19
vml19

Reputation: 3864

C# Alphanumeric Password validation?

Can any one provide me regx validator which validate the following;

Password must be an alphanumeric password i.e. at least 1 number and at least 1 alphabet.

I tried the following, but it did not work.

(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9])$

Upvotes: 0

Views: 2949

Answers (1)

Narendra Yadala
Narendra Yadala

Reputation: 9664

You are almost there, you just need to add a quantifier for your last expression and it will work fine. So, it should be something like this

(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{2,})$.

Code will look like this

Regex regexObj = new Regex(@"(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{2,})$");
boolean foundMatch = regexObj.IsMatch(passwordString);

You are currently matching a single digit or a letter as [a-zA-Z0-9] matches a single alphanum character. Read about character classes([]) here http://www.regular-expressions.info/charclass.html

Upvotes: 3

Related Questions