I am using the following code pattern for password validation. For some reason the regex syntax works on external sites, but not in compiled code. In other words it returns false when it should return true.
This does not work in compiled code (see samples as comments) but works in sites like http://www.regexlib.com:
I am a bit stumped…. any help much apperciated. c#, MVC3,
// Function to check for valid password.
public bool IsPassword(String strToCheck)
{
// Password expresion that requires one lower case letter, one upper case letter, one digit, 6-13 length, and no spaces.
// 1agdA*$# | 1agdA*$# | 1agdA*$#
var objPasswordPattern = new Regex(@"^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).{6,13}$");
return !objPasswordPattern.IsMatch(strToCheck);
}
I’m pretty sure it’s just the little ‘!’ at the return line that’s tripping you up. By doing that you’re saying “is NOT a match”. I ran your code on my local and that’s what happened.