Jquery 匹配 'password' 和 'admin' 的验证无效

Jquery validation on matching 'password' and 'admin' not working

我试过为密码写一个正则表达式:

public class ApplicationUser : IdentityUser, ITimeStamps
{
    public const string PasswordRegularExpression = @"admin|password";
    // public const string PasswordRegularExpression = @"/admin|password/i"; // Tried this too
    // public const string PasswordRegularExpression = @"/(admin|password)/i"; // Tried this too

这超出了正常的 Microsoft 标识内容:

manager.PasswordValidator = new PasswordValidator
{
     RequiredLength = 6,
     RequireNonLetterOrDigit = false,
     RequireDigit = true,
     RequireLowercase = false,
     RequireUppercase = false,
};

这是我的注册视图模型:

public class RegisterViewModel
{
    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    [RegularExpression( ApplicationUser.PasswordRegularExpression, ErrorMessage = "The {0} must contain atleast 1 number and must not contain the word 'admin' or 'password'" )] 
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }
}

当有人在密码中使用 password 或 admin 时,我想在 jquery 验证中抛出错误。但是,jquery 验证似乎没有按预期工作。我可能缺少什么?

从 asp.net 生成的 html 如下所示:

<input class="form-control input-validation-error" data-val="true" data-val-length="The Password must be at least 6 characters long." data-val-length-max="100" data-val-length-min="6" data-val-regex="The Password must contain atleast 1 number and must not contain the word 'admin' or 'password'" data-val-regex-pattern="admin|password" data-val-required="The Password field is required." id="Password" name="Password" placeholder="Enter a password" type="password">

测试用例:

旁注:

根据 Regular expression to match a line that doesn't contain a word? 的回答,以下正则表达式应该匹配任何密码 除了 如果它包含 "admin" 或 "password":

^((?!(admin|password)).)*$

我已经在 Regex101 上为此创建了一个 fiddle,用于在将其添加到您的应用程序之前进行测试。