一點一滴,編程人生

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            69 隨筆 :: 0 文章 :: 25 評論 :: 0 Trackbacks

          主要分幾部分
          jquery.validate 基本用法
          jquery.validate API說明
          jquery.validate 自定義
          jquery.validate 常見類型的驗證代碼

          下載地址

          jquery.validate插件的文檔地址
          http://docs.jquery.com/Plugins/Validation

          jquery.validate插件的主頁
          http://bassistance.de/jquery-plugins/jquery-plugin-validation/

          jquery.validate插件主頁上提供的demo
          http://jquery.bassistance.de/validate/demo/

          驗證規則下面是默認校驗規則,也可以自定義規則

          (1)required:true 必輸字段
          (2)remote:"check.php" 使用ajax方法調用check.php驗證輸入值
          (3)email:true 必須輸入正確格式的電子郵件
          (4)url:true 必須輸入正確格式的網址
          (5)date:true 必須輸入正確格式的日期
          (6)dateISO:true 必須輸入正確格式的日期(ISO),例如:2009-06-23,1998/01/22 只驗證格式,不驗證有效性
          (7)number:true 必須輸入合法的數字(負數,小數)
          (8)digits:true 必須輸入整數
          (9)creditcard: 必須輸入合法的信用卡號
          (10)equalTo:"#field" 輸入值必須和#field相同
          (11)accept: 輸入擁有合法后綴名的字符串(上傳文件的后綴)
          (12)maxlength:5 輸入長度最多是5的字符串(漢字算一個字符)
          (13)minlength:10 輸入長度最小是10的字符串(漢字算一個字符)
          (14)rangelength:[5,10] 輸入長度必須介于 5 和 10 之間的字符串")(漢字算一個字符)
          (15)range:[5,10] 輸入值必須介于 5 和 10 之間
          (16)max:5 輸入值不能大于5
          (17)min:10 輸入值不能小于10

          驗證提示

          下面是默認的驗證提示,官網有簡體中文版的驗證提示下載,或者通過jQuery.extend(jQuery.validator.messages自定義錯誤提示信息,可以將網站的驗證提示文本統一到一個文件里。
          代碼如下:

           1
           2required: "This field is required."
           3remote: "Please fix this field."
           4email: "Please enter a valid email address."
           5url: "Please enter a valid URL."
           6date: "Please enter a valid date."
           7dateISO: "Please enter a valid date (ISO)."
           8number: "Please enter a valid number."
           9digits: "Please enter only digits"
          10creditcard: "Please enter a valid credit card number."
          11equalTo: "Please enter the same value again."
          12accept: "Please enter a value with a valid extension."
          13maxlength: $.validator.format("Please enter no more than {0} characters."), 
          14minlength: $.validator.format("Please enter at least {0} characters."), 
          15rangelength: $.validator.format("Please enter a value between {0} and {1} characters long."), 
          16range: $.validator.format("Please enter a value between {0} and {1}."), 
          17max: $.validator.format("Please enter a value less than or equal to {0}."), 
          18min: $.validator.format("Please enter a value greater than or equal to {0}."



          使用方式

          1:
          在控件中使用默認驗證規則,例子:
          電子郵件(必填) <input id="email" class="required email" value="email@" />2:
          可以在控件中自定義驗證規則,例子:
          自定義(必填,[3,5])
          <input id="complex" value="hi" class="{required:true,minlength:3, maxlength:5,
          messages:{required:'為什么不輸入一點文字呢',minlength:'輸入的太少了',maxlength:'輸入那么多干嘛'}}" />

          3: 通過javascript自定義驗證規則,下面的JS自定義了兩個規則,password和confirm_password 
          代碼如下:

           1
           2$().ready(function() 
           3$("#form2").validate(
           4rules: 
           5password: 
           6required: true
           7minlength: 5 
           8}

           9confirm_password: 
          10required: true
          11minlength: 5
          12equalTo: "#password" 
          13}
           
          14}

          15messages: 
          16password: 
          17required: "沒有填寫密碼"
          18minlength: jQuery.format("密碼不能小于{0}個字符"
          19}

          20confirm_password: 
          21required: "沒有確認密碼"
          22minlength: "確認密碼不能小于{0}個字符"
          23equalTo: "兩次輸入密碼不一致嘛" 
          24}
           
          25}
           
          26}
          ); 
          27}
          ); 



          Html
          密碼<input id="password" name="password" type="password" />
          確認密碼<input id="confirm_password" name="confirm_password" type="password" />
          條件驗證<input id="funcvalidate" name="funcvalidate" value="" />

          4: 使用meta自定義驗證信息

          首先用JS設置meta
          $("#form3").validate({ meta: "validate" });

          Html

          email<input class="{validate:{required:true, email:true,
          messages:{required:'輸入email地址', email:'你輸入的不是有效的郵件地址'}}}"/>

          5: 使用meta可以將驗證規則寫在自定義的標簽內,比如validate

          JS設置meta
          $().ready(function() {
          $.metadata.setType("attr", "validate");
          $("#form1").validate();
          });

          Html

          Email
          <input id="email" name="email"
          validate="{required:true, email:true, messages:{required:'輸入email地址', email:'你輸入的不是有效的郵件地址'}}" />

          6: 自定義驗證規則

          對于復雜的驗證,可以通過jQuery.validator.addMethod添加自定義的驗證規則

          官網提供的additional-methods.js里包含一些常用的驗證方式,比如lettersonly,ziprange,nowhitespace等

          例子
          代碼如下:

           1
           2$("#form2").validate(
           3rules: 
           4funcvalidate: 
           5required: function() {return $("#password").val()!=""; } 
           6}
           
           7}

           8messages: 
           9funcvalidate: 
          10required: "有密碼的情況下必填" 
          11}
           
          12}
           
          13}
          ); 




          7: radio、checkbox、select的驗證方式類似

          radio的驗證

          性別
          <span>
          男<input type="radio" id="gender_male" value="m" name="gender" class="{required:true}"/><br />
          女<input type="radio" id="gender_female" value="f" name="gender" />
          </span>

          checkbox的驗證

          最少選擇兩項
          <span>
          選項1<input type="checkbox" id="check_1" value="1" name="checkGroup"
          class="{required:true,minlength:2, messages:{required:'必須選擇',minlength:'至少選擇2項'}}" /><br />
          選項2<input type="checkbox" id="check_2" value="2" name="checkGroup" /><br />
          選項3<input type="checkbox" id="check_3" value="3" name="checkGroup" /><br />
          </span>

          select的驗證

          下拉框
          <span>
          <select id="selectbox" name="selectbox" class="{required:true}">
          <option value=""></option>
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
          </select>
          </span>
          8: Ajax驗證

          用remote可以進行Ajax驗證
          remote: {
          url: "url", //url地址
          type: "post", //發送方式
          dataType: "json", //數據格式 data: { //要傳遞的數據
          username: function() {
          return $("#username").val();
          }}
          }

          補充: jQuery Validation插件remote驗證方式的Bug
          http://www.jb51.net/article/24079.htm

          下一章是API的具體說明
          然后整理怎么進一步自定義jQuery.validate以及網上一些常用的驗證代碼
          詳細出處參考:http://www.jb51.net/article/24078.htm

          posted on 2011-04-27 23:37 writegull 閱讀(626) 評論(0)  編輯  收藏 所屬分類: javascript
          主站蜘蛛池模板: 兰考县| 湄潭县| 奎屯市| 太湖县| 古田县| 达拉特旗| 乃东县| 阳曲县| 专栏| 定襄县| 冀州市| 阳西县| 江北区| 富顺县| 德昌县| 西城区| 枣阳市| 孟村| 温宿县| 昌图县| 巴楚县| 和平县| 元谋县| 汝州市| 西青区| 泰来县| 巴楚县| 上林县| 台东县| 周口市| 二手房| 靖安县| 白水县| 铁力市| 景德镇市| 万年县| 米泉市| 永昌县| 肥西县| 永丰县| 乐昌市|