mvc数据注释正则表达式

发布于 2024-11-16 13:08:51 字数 652 浏览 4 评论 0原文

我刚刚开始摆弄正则表达式,但基本上这个(我在网上找到的)应该要求密码在 8 - 10 之间,但每当我尝试并输入正确的长度时,它仍然显示错误信息。有什么想法吗? 我还需要在验证中包含 1 个小写字母、1 个大写字母和 1 个数字。

public class password : IValidatableObject
{
    [Required]
    [RegularExpression("(?=^.{8,10}$)", ErrorMessage = "Password is invalid.")]
    public string ConfirmPWD { get; set; }
    [Required(ErrorMessage="Confirm Password field is required.")]
    public string ConfirmPWD { get; set; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (ConfirmPWD != ConfirmPWD )
            yield return new ValidationResult("Not identical.");
    }
}

I just started messing around with regularexpressions, but basically with this one (which i found online btw) is supposed to require for the password to be between 8 - 10, but whenever i try it out and put in the correct length it's still showing the error message. any ideas?
i also need to include a 1 lowercase, 1 uppercase and 1 number on the validations..

public class password : IValidatableObject
{
    [Required]
    [RegularExpression("(?=^.{8,10}$)", ErrorMessage = "Password is invalid.")]
    public string ConfirmPWD { get; set; }
    [Required(ErrorMessage="Confirm Password field is required.")]
    public string ConfirmPWD { get; set; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (ConfirmPWD != ConfirmPWD )
            yield return new ValidationResult("Not identical.");
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若相惜即相离 2024-11-23 13:08:51

您可能想要这样的东西:

[RegularExpression(@"^.{8,10}$",
ErrorMessage =“必须介于 8 到 10 个字符之间。”)]

You probably want something like this:

[RegularExpression(@"^.{8,10}$",
ErrorMessage = "Must be between 8 and 10 characters.")]

葬花如无物 2024-11-23 13:08:51

尝试仅 ".{8,10}"。此外,更具体的错误消息可能对您的用户有帮助。

编辑:想想为什么不直接使用 StringLengthAttribute 来实现这个...

[StringLength(10, MinLength = 8, ErrorMessage = "密码必须在 8 到 10 个字符之间") ]

那么您可以使用正则表达式来实现更具体的规则,例如所需的复杂性。

try just ".{8,10}". also, a more specific error message might be helpful to your users.

Edit: come to think of it why not just use the StringLengthAttribute for this...

[StringLength(10, MinLength = 8, ErrorMessage = "Password must be between 8 and 10 characters")]

then you can use regex for your more specific rules such as required complexity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文