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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With