Asp.net mvc MVC4有多种验证类型
我有这个Asp.net mvc MVC4有多种验证类型,asp.net-mvc,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 4,我有这个视图 @using (Html.BeginForm("RegisterApartmentOwner", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) { @Html.AntiForgeryToken() @Html.ValidationSummary(true)
视图
@using (Html.BeginForm("RegisterApartmentOwner", "Home", FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div class="editor-label">
@Html.LabelFor(model => model.FirstName)
</div>
<div class="editor-field">
@Html.TextBoxFor(x => x.FirstName, new {placeholder = "Enter Your First Name" })
@Html.ValidationMessageFor(model => model.FirstName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.LastName)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.LastName, new { placeholder = "Enter Your Last Name"})
@Html.ValidationMessageFor(model => model.LastName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Password)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Password, new { placeholder = "Enter Your Password"})
@Html.ValidationMessageFor(model => model.Password)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Password)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Password, new { placeholder = "Enter Your Password Again"})
@Html.ValidationMessageFor(model => model.Password)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.MobileNumber)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.MobileNumber, new { placeholder = "Enter Your Mobile Number"})
@Html.ValidationMessageFor(model => model.MobileNumber)
</div>
<input type="submit" value="Register" class="submit"/>
}
@使用(Html.BeginForm(“RegisterPartmentOwner”、“Home”、FormMethod.Post、,
新的{enctype=“multipart/form data”})
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@LabelFor(model=>model.FirstName)
@Html.TextBoxFor(x=>x.FirstName,新建{placeholder=“输入您的名字”})
@Html.ValidationMessageFor(model=>model.FirstName)
@LabelFor(model=>model.LastName)
@Html.TextBoxFor(model=>model.LastName,新建{placeholder=“输入您的姓氏”})
@Html.ValidationMessageFor(model=>model.LastName)
@LabelFor(model=>model.Password)
@Html.TextBoxFor(model=>model.Password,新建{placeholder=“输入您的密码”})
@Html.ValidationMessageFor(model=>model.Password)
@LabelFor(model=>model.Password)
@Html.TextBoxFor(model=>model.Password,新建{placeholder=“再次输入密码”})
@Html.ValidationMessageFor(model=>model.Password)
@LabelFor(model=>model.MobileNumber)
@Html.TextBoxFor(model=>model.MobileNumber,new{placeholder=“输入您的手机号码”})
@Html.ValidationMessageFor(model=>model.MobileNumber)
}
我的问题是,验证仅在字段为空时有效,但我希望验证能够发现两个密码不相等的时间,以及mobilenumber不是数字的时间等等。我该怎么办呢?您应该看看数据注释ASP.NET MVC有一些验证属性,如
RegularExpression
或DataType
,但它们在某些情况下是不够的。在这种情况下,你需要为你的文本框蒙版,最好的是。该网站为您提供了许多不同案例的示例
要检查两个密码是否相等,需要编写一些javascript代码或在Jquery验证中添加新规则。可能会有帮助。
Compare
属性是比较两个值的另一种选择。您可以尝试jQuery.Validation.Unobtrusive.Native nuget包。它真的很容易实现,并且会满足您的需求
安装
只需添加到web.config文件
安装软件包后,您应该查看演示站点或下载
来源于github以获取更多信息
在您的案例中,请看以下示例:
致以最诚挚的问候您可以使用数据注释扩展库,该库以nuget pagkage的形式提供 这里是网站 Nuget包
演示对于您创建的模型,您可以使用数据注释,并且按照@kkern,如果您启用了不引人注目的验证,并且包含了所有js文件引用,您可以通过在属性中添加属性来验证它们。样本包括以下内容:
public class MyModel
{
[Required(ErrorMessage="First Name is Required")]
public string FirstName {get; set;}
[Required(ErrorMessage="")]
public string Password {get; set;}
[Compare("Password")]
public string ConfirmPassword {get; set;}
[RegularExpression(@"^[0-9]{7,13}$", ErrorMessage = "Contact No must be digits only and length of 7-13.")]
public string MobileNo {get; set;}
}
可以在模型中使用自验证方法:
public class TestModel : IValidatableObject
{
public string FirstName { get; set; }
public string Password { get; set; }
public string PasswordConfirm { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (FirstName == null)
{
yield return new ValidationResult("FirstName is mandatory.");
}
if (Password != PasswordConfirm)
{
yield return new ValidationResult("Password confirmation does not match.");
}
}
}
有关此方法的更多信息:
public class MyModel
{
[Required(ErrorMessage="First Name is Required")]
public string FirstName {get; set;}
[Required(ErrorMessage="")]
public string Password {get; set;}
[Compare("Password")]
public string ConfirmPassword {get; set;}
[RegularExpression(@"^[0-9]{7,13}$", ErrorMessage = "Contact No must be digits only and length of 7-13.")]
public string MobileNo {get; set;}
}
public class TestModel : IValidatableObject
{
public string FirstName { get; set; }
public string Password { get; set; }
public string PasswordConfirm { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (FirstName == null)
{
yield return new ValidationResult("FirstName is mandatory.");
}
if (Password != PasswordConfirm)
{
yield return new ValidationResult("Password confirmation does not match.");
}
}
}
[HttpPost]
public ActionResult Create(Model model) {
if (!ModelState.IsValid) {
var errors = model.Validate(new ValidationContext(model, null, null));
foreach (var error in errors)
{
foreach (var memberName in error.MemberNames)
{
ModelState.AddModelError(memberName, error.ErrorMessage);
}
}
return View(model);
}
}