MVC驗證12-使用DataAnnotationsExtensions對整型、郵件、最小值、檔案類型、Url地址等驗證

來源:互聯網
上載者:User

標籤:style   blog   http   color   使用   檔案   

原文:MVC驗證12-使用DataAnnotationsExtensions對整型、郵件、最小值、檔案類型、Url地址等驗證

本文體驗來自http://dataannotationsextensions.org/的DataAnnotationsExtensions.MVC3,是DataAnnotation的擴充,可以在用戶端和服務端進行雙步驟驗證,能驗證的類型包括:

 

● 郵件驗證
● 整型驗證
● 日期驗證
● 數字驗證(即從零開始的數字,不包括帶小數點)
● 是否相同驗證
● 檔案類型驗證
● int類型驗證(可以是負的int類型)
● 最大數值驗證(數值可以帶小數點,可以對負數驗證)
● 最小數值驗證(數值可以帶小數點,可以對負數驗證)
● 數值驗證(可以是負數,可以帶小數點,就是不能是字串)
● url地址驗證
● 年份驗證
......

 

MVC驗證兄弟篇:
● MVC驗證01-基礎、遠程驗證  
● MVC驗證02-自訂驗證規則、郵件驗證  
● MVC驗證03-自訂驗證規則、禁止輸入某些值 
● MVC驗證04-自訂驗證規則、日期範圍驗證  
● MVC驗證05-自訂驗證規則、驗證2個屬性值不等  
● MVC驗證06-自訂錯誤資訊  
● MVC驗證07-自訂Model層級驗證  
● MVC驗證08-jQuery非同步驗證  
● MVC驗證09-使用MVC的Ajax.BeginForm方法實現非同步驗證  
● MVC驗證10-到底用哪種方式實現用戶端服務端雙重非同步驗證  
● MVC驗證11-對複雜類型使用jQuery非同步驗證  


□ 安裝

引用→右鍵→選擇"管理NuGet程式包"→輸入"DataAnnotationsExtensions"搜尋→安裝"DataAnnotationsExtensions.MVC3"

 

需要同時引入2個檔案,DataAnnotationsExtensions用於服務端驗證,DataAnnotationsExtensions.MV3使用者用戶端驗證:

 

引入後,引用檔案夾包括:

 

另外,不要被DataAnnotationsExtensions.MV3的名稱疑惑,在MVC4下照樣可以使用。

 

  驗證郵件、整型、最小數、檔案類型

□ View Model

   1:  using System.ComponentModel.DataAnnotations;
   2:  using DataAnnotationsExtensions;
   3:   
   4:  namespace MvcApplication1.Models
   5:  {
   6:      public class Sample
   7:      {
   8:          [Email(ErrorMessage = "請輸入有效郵箱")]
   9:          [Required(ErrorMessage = "必填")]
  10:          [Display(Name = "郵箱")]
  11:          public string Email { get; set; }
  12:   
  13:          [Integer(ErrorMessage = "必須為整數")]
  14:          [Min(1, ErrorMessage = "至少為1")]
  15:          [Display(Name = "年齡")]
  16:          public int Age { get; set; }
  17:   
  18:          [DataAnnotationsExtensions.FileExtensions("png|jpg|jpeg|gif", ErrorMessage = "允許的檔案類型為png|jpg|jpeg|gif")]
  19:          [Display(Name = "檔案類型")]
  20:          public string File { get; set; }
  21:      }
  22:  }
  23:   

 

□ 視圖:必須包含與用戶端驗證相關的jquery,即@Scripts.Render("~/bundles/jqueryval")

   1:  @model MvcApplication1.Models.Sample
   2:   
   3:  @{
   4:      ViewBag.Title = "Index";
   5:      Layout = "~/Views/Shared/_Layout.cshtml";
   6:  }
   7:   
   8:  <h2>Index</h2>
   9:   
  10:  @using (Html.BeginForm()) {
  11:      @Html.ValidationSummary(true)
  12:   
  13:      <fieldset>
  14:          <legend>Sample</legend>
  15:   
  16:          <div class="editor-label">
  17:              @Html.LabelFor(model => model.Email)
  18:          </div>
  19:          <div class="editor-field">
  20:              @Html.EditorFor(model => model.Email)
  21:              @Html.ValidationMessageFor(model => model.Email)
  22:          </div>
  23:   
  24:          <div class="editor-label">
  25:              @Html.LabelFor(model => model.Age)
  26:          </div>
  27:          <div class="editor-field">
  28:              @Html.EditorFor(model => model.Age)
  29:              @Html.ValidationMessageFor(model => model.Age)
  30:          </div>
  31:   
  32:          <div class="editor-label">
  33:              @Html.LabelFor(model => model.File)
  34:          </div>
  35:          <div class="editor-field">
  36:              @Html.EditorFor(model => model.File)
  37:              @Html.ValidationMessageFor(model => model.File)
  38:          </div>
  39:   
  40:          <p>
  41:              <input type="submit" value="提交" />
  42:          </p>
  43:      </fieldset>
  44:  }
  45:   
  46:  <div>
  47:      @Html.ActionLink("Back to List", "Index")
  48:  </div>
  49:   
  50:  @section Scripts {
  51:      @Scripts.Render("~/bundles/jqueryval")
  52:  }
  53:   


□ 結果

郵箱不符合要求報錯:

年齡小於1報錯:

年齡不為整型報錯:

檔案類型不符合要求報錯:

 

  驗證Url地址,比較驗證

□ View Model

   1:      public class Sample1
   2:      {
   3:          [Display(Name = "密碼")]
   4:          public string Password { get; set; }
   5:   
   6:          [Display(Name = "確認密碼")]
   7:          [EqualTo("Password",ErrorMessage = "密碼不匹配")]
   8:          public string PasswordConfirm { get; set; }
   9:   
  10:          [Display(Name = "Url地址")]
  11:          [DataAnnotationsExtensions.Url(UrlOptions.RequireProtocol,ErrorMessage = "Url地址不符合要求")] 
  12:          public string HomePage { get; set; }
  13:      }

 

□ 視圖

   1:  @model MvcApplication1.Models.Sample1
   2:   
   3:  @{
   4:      ViewBag.Title = "Hello";
   5:      Layout = "~/Views/Shared/_Layout.cshtml";
   6:  }
   7:   
   8:  <h2>Hello</h2>
   9:   
  10:  @using (Html.BeginForm()) {
  11:      @Html.ValidationSummary(true)
  12:   
  13:      <fieldset>
  14:          <legend>Sample1</legend>
  15:   
  16:          <div class="editor-label">
  17:              @Html.LabelFor(model => model.Password)
  18:          </div>
  19:          <div class="editor-field">
  20:              @Html.EditorFor(model => model.Password)
  21:              @Html.ValidationMessageFor(model => model.Password)
  22:          </div>
  23:   
  24:          <div class="editor-label">
  25:              @Html.LabelFor(model => model.PasswordConfirm)
  26:          </div>
  27:          <div class="editor-field">
  28:              @Html.EditorFor(model => model.PasswordConfirm)
  29:              @Html.ValidationMessageFor(model => model.PasswordConfirm)
  30:          </div>
  31:   
  32:          <div class="editor-label">
  33:              @Html.LabelFor(model => model.HomePage)
  34:          </div>
  35:          <div class="editor-field">
  36:              @Html.EditorFor(model => model.HomePage)
  37:              @Html.ValidationMessageFor(model => model.HomePage)
  38:          </div>
  39:   
  40:          <p>
  41:              <input type="submit" value="提交" />
  42:          </p>
  43:      </fieldset>
  44:  }
  45:   
  46:  @section Scripts {
  47:      @Scripts.Render("~/bundles/jqueryval")
  48:  }
  49:   


□ 結果

密碼不一致報錯:

 

Url地址不符合要求:


參考資料:
※ INTRODUCING DATA ANNOTATIONS EXTENSIONS

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.