2013-10-27 56 views
1

的,我有這個viewMVC4許多類型的驗證

@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"/> 
} 

我的問題是驗證工作只是當字段爲空,但我想驗證發現,當兩個密碼是不相等的,而當移動號碼不是號碼等。我該怎麼做?

回答

0

ASP.NET MVC有一些驗證屬性,如RegularExpressionDataType但他們沒有足夠的一些情況。在這種情況下,您需要爲您的文本框添加一個蒙版,最好的蒙版是meio。該網站爲您提供了很多不同案例的例子。

要檢查兩個密碼的相等性,您需要編寫一些javascript代碼或在Jquery驗證中添加新規則。 Comparing two passwords with Jquery可能會有所幫助。 Compare屬性是比較兩個值的另一種選擇。

1

你可以試試jQuery.Validation.Unobtrusive.Native nuget包。這很容易實現,並將滿足您的需求。

安裝

只需添加到您的網絡。配置文件

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

,並在Visual Studio中去工具 - >庫包管理器 - >程序包管理器控制檯 現在在控制檯類型

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

你安裝包後,您應該看看演示網站或從github下載 源碼獲取更多信息

jQuery.Validation.Unobtrusive.Native Package

jQuery.Validation.Unobtrusive.Native.Demo Site

Using NuGet Package Manager

而在你的情況看這例子

EqualTo

Demo Validation

最好的問候

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;} 
} 
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); 
    } 
} 

更多有關此方法的信息:How to force MVC to Validate IValidatableObject