0

Need some help with a Regex please, I have accomplished what I need to do but only with a two step process, I have tried multiple rethinks but am unable to make the two regex's into only one. Could someone please point me in the right direction? (have been through lots of tutorials, builders and what not but do not seem clever enough to figure it out!)

This is for password validation, ensuring at least one char, one number, one uppercase letter and ensuring first and last chars are not digits and password is at least 8 chars long. This is the server side version but I will also be placing the regex on client side too.

private bool ValidatePassword(string input)
{
    bool _return;
    var regEx = new Regex(@"^(?=(.*\d))(?=.*[a-z])(?=.*[A-Z])(?!\d).{8,}$");
    _return = regEx.IsMatch(input);

    if (_return)
    {
        regEx = new Regex(@"[^1](\D*)$");
        _return = regEx.IsMatch(input);
    }

    return _return;
}
Abbas
  • 14,186
  • 6
  • 41
  • 72
T1gg3rB0unC3
  • 151
  • 1
  • 10

2 Answers2

0

Would this work?

var regEx = new Regex(@"^(?=(.*\d))(?=.*[a-z])(?=.*[A-Z])(?!\d).{7,}\D$");

Demo here: http://rubular.com/r/xyiSz3RCQr

Matt Burland
  • 44,552
  • 18
  • 99
  • 171
  • This is perfect - thank you Mark, been going round in circles for a couple of hours and you sort my head out in minutes! – T1gg3rB0unC3 Mar 10 '14 at 14:38
0

Try this:

^\D{1}((?=(.*\d))(?=.*[a-z])(?=.*[A-Z])(?!\d).{6,})\D{1}$
amiry jd
  • 27,021
  • 30
  • 116
  • 215