我有view
@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"/>
}
我的问题是验证只在字段为空时才起作用,但我希望验证能够发现两个密码何时不相等,以及何时mobilenumber不是数字等等。我该怎么办?
答案 0 :(得分:1)
答案 1 :(得分:1)
您可以尝试使用jQuery.Validation.Unobtrusive.Native nuget包。它非常容易实现,并且能够满足您的需求。
<强>安装强>
只需添加到web.config文件即可
<appSettings>
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
在Visual Studio中转到工具 - &gt;库包管理器 - &gt;包管理器控制台 现在在控制台类型
PM> Install-Package jQuery.Validation.Unobtrusive.Native.MVC4
安装软件包后,您应该查看演示站点或下载 来自github的消息来源了解更多信息
jQuery.Validation.Unobtrusive.Native Package
jQuery.Validation.Unobtrusive.Native.Demo Site
在您的情况下,请查看示例:
祝你好运
答案 2 :(得分:0)
ASP.NET MVC有一些验证属性,如RegularExpression
或DataType
,但在某些情况下它们还不够。在这种情况下,您的文本框需要一个掩码,最好的是meio。该网站为您提供了大量不同案例的例子。
要检查两个密码的相等性,您需要编写一些javascript代码或在Jquery验证中添加新规则。 Comparing two passwords with Jquery可能会有所帮助。 Compare
属性是比较两个值的另一种选择。
答案 3 :(得分:0)
您可以使用Data Annotations Extensions库,它可以作为nuget包这是网站http://dataannotationsextensions.org/
Nuget package http://www.nuget.org/packages/DataAnnotationsExtensions
答案 4 :(得分:0)
对于您创建的模型,您可以使用数据注释,并且根据@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;}
}
答案 5 :(得分:0)
使用可以在您的模型中使用自我验证方法:
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);
}
}