最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

jQuery validate驗(yàn)證插件使用詳解

 更新時(shí)間:2016年05月11日 15:00:13   投稿:lijiao  
這篇文章主要為大家詳細(xì)介紹了jQuery validate驗(yàn)證插件用法,Validate是基于jQuery的一款輕量級(jí)驗(yàn)證插件,支持input,select,textarea的驗(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ī)則填寫

<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使用ajax跨域獲取數(shù)據(jù)的簡(jiǎn)單實(shí)例

    jQuery使用ajax跨域獲取數(shù)據(jù)的簡(jiǎn)單實(shí)例

    下面小編就為大家?guī)?lái)一篇jQuery使用ajax跨域獲取數(shù)據(jù)的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-05-05
  • jQuery 隱藏和顯示 input 默認(rèn)值示例

    jQuery 隱藏和顯示 input 默認(rèn)值示例

    這篇文章主要介紹了jQuery如何隱藏和顯示 input 默認(rèn)值,需要的朋友可以參考下
    2014-06-06
  • 基于jQuery的message插件實(shí)現(xiàn)右下角彈出消息框

    基于jQuery的message插件實(shí)現(xiàn)右下角彈出消息框

    有時(shí)在頁(yè)面加載的時(shí)候,需要在頁(yè)面的右下角彈出一個(gè)小的提示框,顯示一些提示信息給用戶,通過(guò)使用jQuery的message插件,可以很方便的實(shí)現(xiàn)這個(gè)效果,在使用之前先介紹一下message插件中的方法的使用。
    2011-01-01
  • jquery tab插件精簡(jiǎn)版分享

    jquery tab插件精簡(jiǎn)版分享

    自己平時(shí)用得比較多tab功能,網(wǎng)上有很強(qiáng)大的tab功能,但是很多時(shí)候太過(guò)于復(fù)雜,所以自己寫了一個(gè)最簡(jiǎn)單的jquery插件,代碼如下,就不解釋了。
    2011-09-09
  • jQuery中的each()詳細(xì)介紹(推薦)

    jQuery中的each()詳細(xì)介紹(推薦)

    jquery中的each()非常好用,用起來(lái)非常的爽,下面小編給大家介紹jquery中each()的用法詳細(xì)介紹,感興趣的朋友一起看看吧
    2016-05-05
  • 基于JQuery的實(shí)現(xiàn)圖片輪播效果(焦點(diǎn)圖)

    基于JQuery的實(shí)現(xiàn)圖片輪播效果(焦點(diǎn)圖)

    用JQuery操作DOM確實(shí)很方便,并且JQuery提供了非常人性化的API應(yīng)付我們的各種需求,其中選擇器在此示例-JQuery實(shí)現(xiàn)圖片輪播效果上體現(xiàn)的尤為出色。大大簡(jiǎn)化了js的代碼。
    2010-09-09
  • 動(dòng)態(tài)標(biāo)簽 懸停效果 延遲加載示例代碼

    動(dòng)態(tài)標(biāo)簽 懸停效果 延遲加載示例代碼

    懸停效果、延遲加載想必大家都有見(jiàn)到過(guò)吧,在本文將為大家介紹下是如何實(shí)現(xiàn)的,感興趣的朋友不要錯(cuò)過(guò)
    2013-11-11
  • 完美解決jQuery fancybox ie 無(wú)法顯示關(guān)閉按鈕的問(wèn)題

    完美解決jQuery fancybox ie 無(wú)法顯示關(guān)閉按鈕的問(wèn)題

    下面小編就為大家?guī)?lái)一篇完美解決jQuery fancybox ie 無(wú)法顯示關(guān)閉按鈕的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-11-11
  • 2013年優(yōu)秀jQuery插件整理小結(jié)

    2013年優(yōu)秀jQuery插件整理小結(jié)

    今天為大家推薦的是2013年的第一期,在這期里面十個(gè)jQuery插件涵蓋了響應(yīng)式的網(wǎng)格布局插件、圖片放大插件、表單元素中自定義select插件,google 地圖插件、文件拖放上傳插件、tooltip提示插件、3D旋轉(zhuǎn)菜單等等
    2013-11-11
  • jquery網(wǎng)頁(yè)元素拖拽插件效果及實(shí)現(xiàn)

    jquery網(wǎng)頁(yè)元素拖拽插件效果及實(shí)現(xiàn)

    效果說(shuō)明:配合已有css樣式,載入插件后,網(wǎng)頁(yè)元素可以隨意在窗口內(nèi)拖拽,設(shè)置了原位置半透明和拖拽半透明的效果選項(xiàng),可根據(jù)需要選擇。另外,當(dāng)頁(yè)面上有多個(gè)可拖拽元素時(shí),可以載入另外一個(gè)用于設(shè)置z-index的插件,模擬windows窗口點(diǎn)擊置頂效果。
    2013-08-08

最新評(píng)論

临泉县| 道真| 寿光市| 隆德县| 长泰县| 湘潭县| 班玛县| 安新县| 呼和浩特市| 凤庆县| 沙河市| 垣曲县| 双江| 固安县| 巴楚县| 沙田区| 天祝| 易门县| 兴义市| 玉林市| 南昌县| 兰西县| 礼泉县| 巧家县| 绍兴市| 邯郸县| 玉林市| 长岭县| 乌兰县| 儋州市| 玉田县| 漯河市| 凉山| 永春县| 岐山县| 内黄县| 黎城县| 永德县| 柳林县| 咸丰县| 丘北县|