问题描述
在我的 Password 属性上的模型 Registerviewmodel 中,我创建了两个验证属性。在这种情况下,我使用通常的 MVC 和 jQuery 验证。问题是验证消息显示了唯一的
[RegularExpression(@"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[#$^+=!*()@%&]).{0,}$",ErrorMessage = CommonConstants.CREATE_USER_PASSWORD_ERROR)]
如果密码不在 6-15 个字符之间,我希望显示来自此属性的消息
[StringLength(15,ErrorMessage = "The password must be at least {2} and at max {1} characters long.",MinimumLength = 6)]
密码属性:
[required(ErrorMessage = "Password field is required")]
[StringLength(15,MinimumLength = 6)]
[RegularExpression(@"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[#$^+=!*()@%&]).{6,15}$",ErrorMessage = CommonConstants.CREATE_USER_PASSWORD_ERROR)]
[DataType(DataType.Password)]
[display(Name = "Password")]
public string Password { get; set; }
这是视图:
@using (Html.BeginForm("Create","Users",FormMethod.Post))
{
@Html.AntiForgeryToken();
<div class="form-horizontal">
<h4>Contacts</h4>
<hr />
@Html.HiddenFor(x => Model.Registerviewmodel.OrganizationId)
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<div class="col-md-2 control-label">
@Html.LabelFor(x => Model.Registerviewmodel.Email,htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-10">
@Html.TextBoxFor(x => Model.Registerviewmodel.Email,htmlAttributes: new { @class = "form-control",@readonly="readonly" })
@Html.ValidationMessageFor(x => x.Registerviewmodel.Email,"",new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-2 control-label">
@Html.LabelFor(x => Model.Registerviewmodel.FirstName,htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-10">
@Html.TextBoxFor(x => Model.Registerviewmodel.FirstName,htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.Registerviewmodel.FirstName,new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-2 control-label">
@Html.LabelFor(x => Model.Registerviewmodel.LastName,htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-10">
@Html.TextBoxFor(x => Model.Registerviewmodel.LastName,htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.Registerviewmodel.LastName,new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-2 control-label">
@Html.LabelFor(x => Model.Registerviewmodel.Mobile,htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-10">
@Html.TextBoxFor(x => Model.Registerviewmodel.Mobile,htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.Registerviewmodel.Mobile,new { @class = "text-danger" })
</div>
</div>
@*<div class="form-group">
<div class="col-md-2 control-label">
@Html.LabelFor(x => Model.Registerviewmodel.Role,htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-10">
@Html.DropDownListFor(x => Model.Registerviewmodel.Role,new SelectList(Model.Registerviewmodel.Roles,"Value","Text"),id = "Role" })
@Html.ValidationMessageFor(x => x.Registerviewmodel.Role,new { @class = "text-danger" })
</div>
</div>*@
<div class="form-group">
<div class="col-md-2 control-label">
@Html.LabelFor(x => Model.Registerviewmodel.Department,htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-10">
@Html.TextBoxFor(x => Model.Registerviewmodel.Department,htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.Registerviewmodel.Department,new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-2 control-label">
@Html.LabelFor(x => Model.Registerviewmodel.Password,htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-10">
@Html.TextBoxFor(x => Model.Registerviewmodel.Password,placeholder="Password",@type = "password" })
@Html.ValidationMessageFor(x => x.Registerviewmodel.Password,new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-2 control-label">
@Html.LabelFor(x => Model.Registerviewmodel.ConfirmPassword,htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-10">
@Html.TextBoxFor(x => Model.Registerviewmodel.ConfirmPassword,placeholder="Confirm Password",@type = "password" })
@Html.ValidationMessageFor(x => x.Registerviewmodel.ConfirmPassword,new { @class = "text-danger" })
</div>
</div>
</div>
<br />
}
解决方法
可能会使用 RegEx Library 网站来研究和帮助识别一些示例; https://regexlib.com/Search.aspx?k=6-15%20characters
有很多变化,您可以根据需要进行调整。