mvc数据注释正则表达式
我刚刚开始摆弄正则表达式,但基本上这个(我在网上找到的)应该要求密码在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能想要这样的东西:
[RegularExpression(@"^.{8,10}$",
ErrorMessage =“必须介于 8 到 10 个字符之间。”)]
You probably want something like this:
[RegularExpression(@"^.{8,10}$",
ErrorMessage = "Must be between 8 and 10 characters.")]
尝试仅
".{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.