zoukankan      html  css  js  c++  java
  • MVC笔记2:mvc+jquery.validate.js 进行前端验证

    1.引用如下js和css 代码

        <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" 
            type="text/css" />
        <link href="@Url.Content("~/Content/themes/base/jquery.ui.theme.css")" rel="stylesheet" 
            type="text/css" />
        <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" 
            type="text/javascript"></script>
    
         <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" 
            type="text/javascript"></script>
    
        
         <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" 
            type="text/javascript"></script>
    

     2.验证代码

     <script type="text/javascript">
        
         $(document).ready(function () {
               $("form").validate({
                   rules   :{
                       titlenew: { required: true },
                  
                  },
      
             messages: {
                 titlenew: { required: "请输入姓名" },
                 
                }           
            });
         });
    </script>

    3. html代码

    @using(Html.BeginForm("form")){
    
        @Html.TextBoxFor(m => m.titlenew); 
        @Html.ValidationMessage("titlenew");
        <input type="submit"  value="提交"/>
        }


    点击提交,将会出现“请输入姓名”的提示

  • 相关阅读:
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
  • 原文地址:https://www.cnblogs.com/tiancai/p/4843511.html
Copyright © 2011-2022 走看看