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="提交"/> }
点击提交,将会出现“请输入姓名”的提示