Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weak password of 6 characters or more with at least 1 digit

Tags:

c#

regex

I want to create regular expression for password that has length of atleast 6 characters and contains at least 1 digit in it. This is the expression I came up with:

Regex regEx = new Regex(@"^?=\d.{6,}$");

But this doesn't seem to work. Can anybody tell me why?

I guess it says between beginning and ending, it performs look ahead to see if any digit appears in password. It appears, then says anything can repeat {6,} says minimum 6 characters. But this doesn't seem to work. Can anybody correct me on this?

Update: On request of Albin Sunnanbo I have changed title from strong password to weak.

like image 613
TCM Avatar asked Jan 25 '26 06:01

TCM


1 Answers

Regular expressions are not very good at requirements like "contains at least x at any position".

Try this:

bool result = (password.Length >= 6) && password.Any(char.IsDigit);
like image 102
dtb Avatar answered Jan 27 '26 20:01

dtb