jQuery validate驗(yàn)證插件使用詳解
Validate驗(yàn)證插件,內(nèi)置豐富的驗(yàn)證規(guī)則,還有靈活的自定義規(guī)則接口,HTML、CSS與JS之間的低耦合能讓您自由布局和豐富樣式,支持input,select,textarea的驗(yàn)證。
Description
瀏覽器支持:IE7+ 、Chrome、Firefox、Safari、Mobile Browser
jQuery版本:1.7.0+
Usage
載入jQuery、validate
<script type="text/javascript" src="jquery-1.11.1.js"></script>
<script type="text/javascript" src="jquery-validate.js"></script>
DOM標(biāo)簽驗(yàn)證規(guī)則填寫(xiě)
<div class="form_control"> <input class="required" value="315359131@qq.com" type="text" name="email" data-tip="請(qǐng)輸入您的郵箱" data-valid="isNonEmpty||isEmail" data-error="email不能為空||郵箱格式不正確"> </div> <div class="form_control"> <select class="required" data-valid="isNonEmpty" data-error="省份必填"> <option value="">請(qǐng)選擇省份</option> <option value="001">001</option> <option value="002">002</option> </select> </div>
給需要驗(yàn)證的表單元素的class填入required(不建議在這個(gè)class上做其他樣式)。
建議input用獨(dú)立div包裹,因?yàn)轵?yàn)證的message是從當(dāng)前input的父元素上append生成。
data-tip:在尚未驗(yàn)證而獲取焦點(diǎn)時(shí)出現(xiàn)的提示。
data-valid:驗(yàn)證規(guī)則,若有組合驗(yàn)證,以||符號(hào)分割。
data-error:驗(yàn)證錯(cuò)誤提示,對(duì)應(yīng)data-valid,以||符號(hào)分割。
單選/復(fù)選比較特殊,需要添加元素包裹單選/復(fù)選集合,并在包裹元素上加驗(yàn)證規(guī)則。
<div class="form_control"> <span class="required" data-valid="isChecked" data-error="性別必選" data-type="radio"> <label><input type="radio" name="sex">男</label> <label><input type="radio" name="sex">女</label> <label><input type="radio" name="sex">未知</label> </span> </div> <div class="form_control"> <span class="required" data-valid="isChecked" data-error="標(biāo)簽至少選擇一項(xiàng)" data-type="checkbox"> <label><input type="checkbox" name="label">紅</label> <label><input type="checkbox" name="label">綠</label> <label><input type="checkbox" name="label">藍(lán)</label> </span> </div>
JS調(diào)用
//**注意:必須以表單元素調(diào)用validate** $('form').validate({ type:{ isChecked: function(value, errorMsg, el) { var i = 0; var $collection = $(el).find('input:checked'); if (!$collection.length) { return errorMsg; } } }, onFocus: function() { this.parent().addClass('active'); return false; }, onBlur: function() { var $parent = this.parent(); var _status = parseInt(this.attr('data-status')); $parent.removeClass('active'); if (!_status) { $parent.addClass('error'); } return false; } });
表單提交前的驗(yàn)證
$('form').on('submit', function(event) { event.preventDefault(); $(this).validate('submitValidate'); //return true or false; });
validate內(nèi)置驗(yàn)證規(guī)則
required:true 必輸字段
remote:"check.php" 使用ajax方法調(diào)用check.php驗(yàn)證輸入值
email:true 必須輸入正確格式的電子郵件
url:true 必須輸入正確格式的網(wǎng)址
date:true 必須輸入正確格式的日期
dateISO:true 必須輸入正確格式的日期(ISO),例如:2009-06-23,1998/01/22 只驗(yàn)證格式,不驗(yàn)證有效性
number:true 必須輸入合法的數(shù)字(負(fù)數(shù),小數(shù))
digits:true 必須輸入整數(shù)
creditcard: 必須輸入合法的信用卡號(hào)
equalTo:"#field" 輸入值必須和#field相同
accept: 輸入擁有合法后綴名的字符串(上傳文件的后綴)
maxlength:5 輸入長(zhǎng)度最多是5的字符串(漢字算一個(gè)字符)
minlength:10 輸入長(zhǎng)度最小是10的字符串(漢字算一個(gè)字符)
rangelength:[5,10] 輸入長(zhǎng)度必須介于 5 和 10 之間的字符串")(漢字算一個(gè)字符)
range:[5,10] 輸入值必須介于 5 和 10 之間
max:5 輸入值不能大于5
min:10 輸入值不能小于10
例子:
驗(yàn)證用戶名,密碼,確認(rèn)密碼,主頁(yè),生日,郵箱等
首先引入Jquery、引入jquery.validate.js、引入messages_cn.js并且為表單定義一個(gè)id,為需要驗(yàn)證的控件定義name屬性,并賦值,此插件使用的是控件的name屬性,而非id。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="jquery郵箱驗(yàn)證.aspx.cs" Inherits="練習(xí).jquery郵箱驗(yàn)證" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <style type="text/css"> #aa{ color:Red;} </style> <script src="Jquery1.7.js" type="text/javascript"></script> <script src="jquery.validate.js" type="text/javascript"></script> <script src="messages_cn.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { $('#form1').validate({ rules: { username: { required: true, minlength: 6, maxlength: 12 }, password: { required: true, minlength: 6 }, passwordok:{required: true, equalTo: "#password"}, index: { required: true, url: true }, birthday: { required: true, dateISO: true }, bloodpress:{required: true,digits:true}, email: { required: true, email: true } }, errorshow: function (error, element) { error.appendTo(element.siblings('span')); } }) }) </script> </head> <body> <form id="form1" runat="server"> <div> <table> <tr><td>用戶名:</td><td> <input name="username" type="text" /><span id="aa">*</span></td></tr> <tr><td>密碼:</td><td> <input id="password" name="password" type="text" /><span id="aa">*</span></td></tr> <tr><td>確認(rèn)密碼:</td><td> <input id="repassword" name="passwordok" type="text" /><span id="aa">*</span></td></tr> <tr><td>主頁(yè):</td><td> <input name="index" type="text" /><span id="aa">*</span></td></tr> <tr><td>生日:</td><td> <input name="birthday" type="text" /><span id="aa">*</span></td></tr> <tr><td>血壓:</td><td> <input name="bloodpress" type="text" /><span id="aa">*</span></td></tr> <tr><td>郵箱:</td><td><input name="email" type="text" /><span id="aa">*</span></td></tr> <tr><td></td><td> <input id="Button1" type="button" value="button" /></td></tr> </table> </div> </form> </body> </html>
實(shí)現(xiàn)如下效果:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
相關(guān)文章
jQuery實(shí)現(xiàn)div隨意拖動(dòng)的實(shí)例代碼(通用代碼)
這篇文章主要介紹了jQuery實(shí)現(xiàn)div隨意拖動(dòng)的實(shí)例代碼,涉及到j(luò)query div隨意拖動(dòng)相關(guān)知識(shí),感興趣的朋友一起學(xué)習(xí)吧2016-01-01Jquery判斷$("#id")獲取的對(duì)象是否存在的方法
如何判斷獲取的對(duì)象是否存在,貌似有很多方法實(shí)現(xiàn),下面以$("#id")舉例,為大家詳細(xì)介紹下具體的判斷過(guò)程2013-09-09jquery點(diǎn)贊功能實(shí)現(xiàn)代碼 點(diǎn)個(gè)贊吧!
點(diǎn)贊功能很多地方都會(huì)出現(xiàn),如何實(shí)現(xiàn)愛(ài)心點(diǎn)贊功能,這篇文章主要為大家詳細(xì)介紹了jquery點(diǎn)贊功能實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05jquery判斷iPhone、Android設(shè)備類(lèi)型
這篇文章主要為大家詳細(xì)介紹了jquery實(shí)現(xiàn)iPhone、Android設(shè)備判斷方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09jQuery實(shí)現(xiàn)div拖拽效果實(shí)例分析
這篇文章主要介紹了jQuery實(shí)現(xiàn)div拖拽效果的方法,結(jié)合實(shí)例形式分析了jQuery響應(yīng)鼠標(biāo)事件實(shí)現(xiàn)頁(yè)面元素樣式變換的技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-02-02jquery實(shí)現(xiàn)清新實(shí)用的網(wǎng)頁(yè)菜單效果
這篇文章主要介紹了jquery實(shí)現(xiàn)清新實(shí)用的網(wǎng)頁(yè)菜單效果,涉及jquery通過(guò)鼠標(biāo)事件控制頁(yè)面元素的動(dòng)態(tài)隱藏與顯示實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08jQuery團(tuán)購(gòu)倒計(jì)時(shí)特效實(shí)現(xiàn)方法
這篇文章主要介紹了jQuery團(tuán)購(gòu)倒計(jì)時(shí)特效實(shí)現(xiàn)方法,可實(shí)現(xiàn)相對(duì)固定時(shí)間的倒計(jì)時(shí)效果,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-05-05jquery插件jTimer(jquery定時(shí)器)使用方法
很多時(shí)候我們需要按時(shí)間間隔執(zhí)行一個(gè)任務(wù),當(dāng)滿足一定條件時(shí)停止執(zhí)行.此插件旨在解決這一經(jīng)常遇到的問(wèn)題2013-12-12jQuery實(shí)現(xiàn)鼠標(biāo)滑過(guò)鏈接控制圖片的滑動(dòng)展開(kāi)與隱藏效果
這篇文章主要介紹了jQuery實(shí)現(xiàn)鼠標(biāo)滑過(guò)鏈接控制圖片的滑動(dòng)展開(kāi)與隱藏效果,涉及jQuery鼠標(biāo)事件的響應(yīng)及鏈?zhǔn)讲僮鞯南嚓P(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10Jquery全屏相冊(cè)插件zoomvisualizer具有調(diào)節(jié)放大與縮小功能
本文給大家分享一款經(jīng)常使用的jquery全屏相冊(cè)插件zoomvisualizer具有調(diào)節(jié)放大與縮小的功能,對(duì)jquery全屏相冊(cè)插件zoomvisualizer感興趣的朋友可以通過(guò)本文學(xué)習(xí)一下2015-11-11