ASP.NET MVC 5 Jquery Validate

來源:互聯網
上載者:User

標籤:highlight   har   asp.net   body   height   示範   str   script   分享   

  1. ClientValidationEnabled

    在asp.net mvc 5中ClientValidationEnabled預設為TRUE,所以也不需要刻意去設定

  2. 應用ValidationAttribute

    這裡使用內建的RequiredAttribute來示範

    public class Book    {        [Required(ErrorMessage = "BookID Is Required")]        public string BookID { get; set; }             public string Title { get; set; }        public string Date { get; set; }    }

     

  3. 引用相關JQuery
    <script src="~/Scripts/jquery-1.10.2.js"></script><script src="~/Scripts/jquery.validate.js"></script><script src="~/Scripts/jquery.validate.unobtrusive.js"></script>

     

  4. 在View中顯示錯誤訊息
       @Html.ValidationSummary()   @Html.ValidationMessageFor(o => o.BookID)

     

  5. DOMReady中設定validate
<script type="text/javascript">       $(function () {           $("form").validate();       });    </script>

 用戶端view總覽

<html><head>    <meta name="viewport" content="width=device-width" />    <title>View</title>    <script src="~/Scripts/jquery-1.10.2.js"></script>    <script src="~/Scripts/jquery.validate.js"></script>    <script src="~/Scripts/jquery.validate.unobtrusive.js"></script>   <script type="text/javascript">       $(function () {           $("form").validate();       });    </script></head><body>    <form  action="/Home/ShowBook" method="POST">     @Html.ValidationSummary()        @Html.ValidationMessageFor(o => o.BookID)             BookID   @Html.EditorFor(o => o.BookID)              Title            @Html.EditorFor(o => o.Title,new { @value= "WCF解密" })        Day     <input id="Day" name="Day" value="12" type="text" />        Month   <input id="Month" name="Month" value="12" type="text" />        Year    <input id="Year" name="Year" value="2016" type="text" />        <input id="Submit" type="Submit" value="Submit" />    </form></body></html>

 在這個例子中當你提交表單的時候,會驗證用戶端的資料是否符合validationAttribute的設定,如果不滿足條件則顯示錯誤提示

 

ASP.NET MVC 5 Jquery Validate

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.