How can I validate a password using ng-pattern
? The regular expression I'm using in my model works fine, but it only displays an error message if the password is shorter than 7 characters. It doesn't show an error if the password is also in the wrong format. Can someone help me understand why?
Model:
[RegularExpression(@"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{7,}$", ErrorMessage = "Your password is too short (minimum of 7 characters) or not in the correct format.")]
[Required(ErrorMessage = "Please enter your password.", AllowEmptyStrings = false)]
public string Password { get; set; }
Registration.cshtml:
<input type="password" style="width: 600px" class="form-control" name="Password" id="Password" ng-pattern="^(?=.*[a-z])(? =.*[A-Z])(? =.*\d)[a-zA-Z\d]{7,}$" ng-model="User.Password" ng-minlength="7" ng-class="submitted?'ng-dirty':''" required />
<span class="error" ng-show="(form.Password.$dirty || submitted) && form.Password.$error.required">Please enter your password.</span>
<span class="error" ng-show="(form.Password.$dirty || submitted) && (form.Password.$error.minlength || form.Password.$error.pattern)">Your password is too short (minimum of 7 characters) or not in the correct format.</span>
<span class="success" ng-show="(form.Password.$dirty || submitted) && form.Password.$valid">Your password has a sufficient length and is in the correct format.</span>