gpt4 book ai didi

匹配 'password' 和 'admin' 的 Jquery 验证不起作用

转载 作者:行者123 更新时间:2023-12-01 03:37:23 26 4
gpt4 key购买 nike

我尝试编写密码的正则表达式:

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,
};

这是我的注册 View 模型:

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; }
}

当有人在密码中使用密码或管理员时,我想在 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">

测试用例:

  • 我已经添加了括号,但它仍然无法按预期工作。我输入密码,实际上并没有失败!然后我再输入 1 个字符(不管它是什么),然后它失败了......现在我删除了单词“password”的一部分,例如“pasrd21234”,错误仍然存​​在!

旁注:

最佳答案

基于 Regular expression to match a line that doesn't contain a word? 的答案,以下正则表达式应匹配任何密码除了(如果它包含“admin”或“password”:

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

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

关于匹配 'password' 和 'admin' 的 Jquery 验证不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30307246/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com