In my demo MVC Application I have the following validation rules for validating my domain model classes.
RuleFor(m => m.Password)
.Matches(@"^(?=(\d){2,})(?=([a-z])+)(?=(\.\$\~\&)*)").WithMessage("Password should contain at least 2 digits");
But the password validation fails . Basically I want to validate that a password input value should at least contain 2 digits, at least either one of the special characters (.$~&) and at least one alphabet in any order.
They can appear in any order.
Basically I should match the strings like
'a2ss1~A33',
'678.&aA88'
but not
'aaa2sfhdjkf^',
'aass'.
Also I just came across lookahead s in regex. I still have a doubt why cant just we have the rule for validating the password field ?
.Matches(@"^((\d){2,})(.*[a-zA-Z])([\.\$\~\&]*)").WithMessage("Password should contain at least 2 digits");
When to use lookaheads in regex and when not to ?