jQuery驗證插件validate使用詳解
一、jQuery.validate簡介
jQuery.validate.js插件用于對表單輸入進行驗證,其使用配置非常簡單。支持多事件觸發(fā),自帶多種驗證規(guī)則,還支持自定義驗證規(guī)則。
1、配置方法
先導入jQuery庫,然后導入Validate插件,如果是中文提示還需要導入messages_zh.js。
注意Validate的導入要在jQuery庫之后。代碼如下:
<script src="jQuery.1.8.3.js" type="text/javascript"></script> <script src="jquery.validate.js" type="text/javascript"></script> <script src="messages_zh.js" type="text/javascript"></script>
然后只要定義驗證規(guī)則和指定錯誤提示位置就可以了。
在$(document).ready()里加入驗證規(guī)則與錯誤提示位置,代碼如下:
JS代碼:
<script type="text/javascript"> $(function () { $("#form1").validate({ /*自定義驗證規(guī)則*/ rules:{ username:{ required:true,minlength:6 }, userpass:{ required:true,minlength:10 } }, /*錯誤提示位置*/ errorPlacement:function(error,element){ error.appendTo(element.siblings("span")); } }); }) </script>
HTML代碼:
<form id="form1" action="#" method="post"> <p>用戶登錄</p> <p>名稱:<input id="txtName" name="username" type="text" class="txt" /><span style="color:Red;font-size:10px;"></span></p> <p>密碼:<input id="txtPass" name="userpass" type="password" class="txt" /><span style="color:Red;font-size:10px;"></span></p> <div> <input id="btnLogin" type="button" value="登錄" class="btn" /> <input id="btnReset" type="button" value="取消" class="btn" /> </div> </form>
這樣就完成了非常簡單的表單驗證功能,當表單填寫不正確時Validate在<input>的兄弟<span>元素里顯示錯誤提示。
2、name屬性
說明:jQuery.validate.js插件與<input>的關聯(lián)使用的是表單的name屬性。只有存在name屬性的<input>才能驗證!
二、自定義錯誤提示位置
當我們想設置錯誤提示的顯示位置怎么設置呢?
答案就是在errorPlacement參數(shù)里,你可以按照自己的需要自定義書寫,用的是jQuery
/*錯誤提示位置*/ errorPlacement:function(error,element){ //第一個參數(shù)是錯誤的提示文字,第二個參數(shù)是當前輸入框 error.appendTo(element.siblings("span")); //用的是jQuery,這里設置的是,錯誤提示文本顯示在當前文本框的兄弟span中 }
三、自定義錯誤提示信息
例如當我們有多個require:true選項,我想根據(jù)不同的選項設置不同的提示怎么辦呢?
答案就是在messages參數(shù)里。用層層嵌套的方式設置自己需要的提示信息。如果某個字段沒有message信息,這時才調(diào)用默認的提示信息。
messages: { UserName: { required: "請輸入用戶名!" //注意,同樣是必填項,但是優(yōu)先顯示在messages里的提示信息 }, Email:{ required:"請輸入郵箱地址!" //不會統(tǒng)一輸出 必填字段 了哦 } }
實際上,jQuery.Validate默認的錯誤提示是生成一個class=error的label,所以,如果想設置樣式,最簡單的方法就是針對這個label設置就OK了,當然默認的label是可以手動更改的。
四、ajax異步驗證
只需要用到remote即可,注意后臺返回的JSON只能夠是true或false。
以下給出一個綜合示例,前臺HTML代碼
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>表單驗證插件</title> <script src="/Scripts/jquery-1.7.1.js" type="text/javascript"></script> <script src="/Scripts/messages_zh.js" type="text/javascript"></script> <script src="/Scripts/validates.js" type="text/javascript"></script> <script src="/Scripts/jquery.validate.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { $("#form1").validate({ rules: { UserName: { required: true, minlength: 3, maxlength: 18, remote: "/Home/CheckUserName" }, Email: { required: true,email:true }, UserPassword: { required: true ,minlength: 6 }, Mobile: { required: true, number:true }, IdCard: { required: true,isIdCardNo: true }, Age: { required: true ,number:true,min:1,max:100 } }, messages:{ UserName: { required: "請輸入用戶名!", minlength: "用戶名長度最少需要3位!", maxlength: "用戶名長度最大不能超過18位!", remote: "此用戶名已存在!" }, Email: { required: "請?zhí)顚戉]箱", email: "請輸入正確的郵箱格式" }, UserPassword: { required: "請?zhí)顚懩愕拿艽a!", minlength: "密碼長度不能小于6位" }, Mobile: { required: "請?zhí)顚懩愕氖謾C號碼", number:"手機號碼只能為數(shù)字" }, IdCard: { required: "請輸入身份證號碼!", isIdCardNo:"請輸入正確的身份證號碼!" }, Age: { required: "請輸入年齡!", number: "請輸入數(shù)字", min: "年齡不能小于1", max: "年齡不能大于100" } }, /*錯誤提示位置*/ errorPlacement: function (error, element) { error.appendTo(element.parent()); } }) }) </script> </head> <body> <form id="form1" method="post" action=""> <div> <p> 用戶名:<input type="text" value="" name="UserName" /> </p> <p> 密碼:<input type="password" value="" name="UserPassword" /> </p> <p> 郵箱:<input type="text" value="" name="Email" /> </p> <p> 手機號碼:<input type="text" value="" name="Mobile" /> </p> <p> 身份證號碼:<input type="text" value="" name="IdCard" /> </p> <p> 年齡:<input type="text" value="" name="Age" /> </p> <p> <input type="submit" id="btn1" value="提交"></p> </div> </form> </body> </html>
后臺控制器代碼:
public class HomeController : Controller { public ActionResult Index() { return View(); } [HttpGet] public ActionResult CheckUserName() { string username = HttpContext.Request.QueryString["username"]; bool succeed = true; if (username == "admin") { succeed = false; } return Json(succeed, JsonRequestBehavior.AllowGet); } }
最終效果如下圖所示:
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助。
相關文章
Jquery實現(xiàn)select multiple左右添加和刪除功能的簡單實例
下面小編就為大家?guī)硪黄狫query實現(xiàn)select multiple左右添加和刪除功能的簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-05-05EasyUI的DataGrid每行數(shù)據(jù)添加操作按鈕的實現(xiàn)代碼
今天做項目的時候,想在easyui的datagrid每一列數(shù)據(jù)后邊都加上一個操作按鈕,怎么實現(xiàn)此功能呢?下面小編給大家?guī)砹薊asyUI的DataGrid每行數(shù)據(jù)添加操作按鈕的實現(xiàn)代碼,需要的朋友參考下吧2017-08-08jquery的flexigrid無法顯示數(shù)據(jù)提示獲取到數(shù)據(jù)
升級了IE10,發(fā)現(xiàn)flexigrid無法顯示數(shù)據(jù),提示獲取到了數(shù)據(jù),但沒任何報錯任何顯示,經(jīng)過試驗和跟蹤,修改如下,有類似問題的朋友可以參考下哈2013-07-07jQuery+ajax實現(xiàn)滾動到頁面底部自動加載圖文列表效果(類似圖片懶加載)
這篇文章主要介紹了jQuery+ajax實現(xiàn)滾動到頁面底部自動加載圖文列表效果,模擬圖片懶加載功能,涉及jQuery的ajax與asp.net交互動態(tài)顯示頁面內(nèi)容的相關技巧,需要的朋友可以參考下2016-06-06jQuery實現(xiàn)form表單元素序列化為json對象的方法
這篇文章主要介紹了jQuery實現(xiàn)form表單元素序列化為json對象的方法,涉及jQuery基于serializeArray方法實現(xiàn)表單序列化的相關技巧,需要的朋友可以參考下2015-12-12