1

我有这个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"/>
}

我的问题是验证仅在字段为空时有效,但我希望验证发现两个密码不相等以及手机号码不是数字时等等。请问我该怎么办?

4

6 回答 6

1

你应该看看数据注释http://msdn.microsoft.com/en-us/library/dd901590(v=vs.95).aspx

于 2013-10-27T09:35:52.380 回答
1

您可以尝试 jQuery.Validation.Unobtrusive.Native nuget 包。它真的很容易实现,并且会满足您的需求。

安装

只需添加到您的 web.config 文件

 <appSettings>
  <add key="ClientValidationEnabled" value="true" />
  <add key="UnobtrusiveJavaScriptEnabled" value="true" />
 </appSettings>

并在您的 Visual Studio 中转到Tools -> Library Package Manager -> Package Manager Console 现在在控制台类型中

 PM> Install-Package jQuery.Validation.Unobtrusive.Native.MVC4

安装软件包后,您应该查看演示站点或从 github 下载源代码以获取更多信息

jQuery.Validation.Unobtrusive.Native 包

jQuery.Validation.Unobtrusive.Native.Demo 站点

使用 NuGet 包管理器

在您的情况下,请查看以下示例

等于

演示验证

此致

于 2013-10-27T09:40:00.390 回答
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);
    }
}

有关此方法的更多信息:如何强制 MVC 验证 IValidatableObject

于 2013-10-27T10:14:19.663 回答
0

ASP.NET MVC 有一些验证属性,例如RegularExpressionor DataType,但在某些情况下它们还不够。在这种情况下,您的文本框需要一个掩码,最好的掩码是meio。该网站为您提供了许多不同案例的示例。

要检查两个密码是否相等,您需要编写一些 javascript 代码或在 Jquery 验证中添加新规则。用 Jquery 比较两个密码可能会有所帮助。该Compare属性是比较两个值的另一种选择。

于 2013-10-27T09:38:10.827 回答
0

您可以使用 Data Annotations Extensions 库,它可用作 nuget pagkage

这是网站http://dataannotationsextensions.org/

Nuget 包http://www.nuget.org/packages/DataAnnotationsExtensions

演示http://dataannotationsextensions.org/Home/Demos

于 2013-10-27T09:43:13.830 回答
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;}
}
于 2013-10-27T09:46:11.623 回答