Validation-jQuery表單驗證外掛程式使用方法

來源:互聯網
上載者:User

標籤:style   class   blog   java   http   ext   

http://www.cnblogs.com/shuang121/archive/2012/04/23/2466628.html

 

作用

jquery.validate是jquery旗下的一個驗證架構,藉助jquery的優勢,我們可以迅速驗證一些常見的輸入,並且可以自己擴充自己的驗證方法,並且對國際化也有很好的支援。

使用前的布置

說明:需要JQuery版本:1.2.6+

步驟:

要匯入相應的jQuery.js與jquery.validate.js檔案
<script src="jquery.js" type="text/javascript"></script>
<script src="jquery.validate.js" type="text/javascript"></script>

在相應的欄位上指定驗證規則
名稱 *<input type="text" name="loginName" class="required">
其中class="required"代表本欄位必須要輸入資料

指定要對錶單進行驗證
<script type="text/javascript">

$(function(){

$("#testForm").validate();

});

</script>

 

效果如:

 

 

 

 

 

 

 

 

基礎知識

指定驗證規則的方式

把驗證規則寫到欄位元素的class屬性中

例:

名稱 * <input type="text" name="loginName" class="required"><br>

密  *  <input type="password" name="password" class="required"><br>

再次輸入 <input type="password" name="password2"

class="{equalTo: ‘[name=password]‘} required"><br>

生日   <input type="text" name="birthday" class="dateISO"><br>

E-mail *<input type="text" name="email" class="email"><br>

PAR(zip)<input type="file" name="resource" class="{accept: ‘zip‘}">

 

說明:

如果使用class="{key:value,…}"的方式,必須引入:jquery.metadata.js

表單欄位的name不能重複,否則所配置的驗證不起作用。

調用validate()方法時傳遞欄位的驗證規則

$(function() {

$("#testForm").validate({

rules: {

loginName:{

required: true,

minlength: 2

}   ,

password: {

required: true

},

password2: {

equalTo: "input[name=password]"

}

}

});

});

內建的驗證規則

 

 

 

required:true              

必要欄位

remote:"check.php"         

使用ajax方法調用check.php驗證輸入值

email:true                 

必須輸入正確格式的電子郵件

url:true                   

必須輸入正確格式的網址

date:true                  

必須輸入正確格式的日期

dateISO:true                

必須輸入正確格式的日期(ISO),例如:2010-01-01,2010/01/01 只驗證格式,不驗證有效性

number:true                

必須輸入合法的數字(負數,小數)

digits:true                

必須輸入整數

creditcard:                

必須輸入合法的信用卡號

equalTo:"#field"          

輸入值必須和$(“#field”)相同

accept: "gif|png|jpg"

輸入擁有合法尾碼名的字串(上傳檔案的尾碼),多個尾碼之間用’|’隔開

maxlength:5               

輸入長度最多是5的字串(漢字算一個字元)

minlength:3              

輸入長度最小是3的字串(漢字算一個字元)

rangelength:[5,10]        

輸入長度必須介於 5 和 10 之間的字串")(漢字算一個字元)

range:[5,10]              

輸入值必須介於 5 和 10 之間

max:5                      

輸入值不能大於5

min:10                    

輸入值不能小於10

 

 

說明:

remote是遠程驗證:比如註冊驗證使用者名稱是否已被註冊,傳回值只能是true(驗證成功)或false(驗證失敗)。在訪問指定的url時,會自動把當前欄位的值做為參數(以欄位name為key)傳遞過去。

某些屬性值中的引號不能省略,否則出錯。如accept、equalTo等。

 

remote使用時遇到的問題:添加使用者時需要驗證使用者名稱是否存在,當添加上一個使用者後,在不離開該頁面的情況下,再次添加該使用者名稱的使用者,validate不能提示該使用者已存在,因為緩衝的原因,jquery仍認為該使用者名稱可用。解決方案是在頁面中添加:$().ready(function(){

$.ajaxSetup ({

cache: false //關閉AJAX相應的緩衝

}); // 關閉緩衝功能

});

添加

 

修改錯誤資訊提示位置:

修改Jquery validate 的錯誤提示位置,把錯誤提示在input內,當獲得滑鼠焦點的時候清楚提示資訊。 
    具體使用方法: 
var validator = $("#myContainerForm").validate({ 
focusCleanup:true,//clear the error message when the error element get focus again. 
onkeyup:false, 
errorPlacement: function(error, element) {  
showErrorMesssgeDiv(error,element); 
   },   
rules:{ 
       name:{ 
              required: true 
       } 
}, 
messages: { 
       name:{ 
              required:populateErrorMessage($("#errorRequiredMessage").val(),               $("#containerNameTitle").val()) 
       }      
} 
});

自訂驗證規則

除了內建的驗證規則,validation還允許自訂驗證規則。這是通過validation的addMethod()方法實現的,文法 為:

jQuery.validator.addMethod("name",function,message)

其中:

name為驗證規則的名稱

function定義驗證的規則。參數有?。傳回值為?。

message是驗證失敗時的提示資訊。

指定錯誤提示內容

更改預設的提示內容

jQuery.extend(jQuery.validator.messages, {

required: "必要欄位",

remote: "請指定一個不重複的值",

email: "請輸入正確格式的電子郵件",

url: "請輸入合法的網址",

date: "請輸入合法的日期",

dateISO: "請輸入合法的日期 (ISO).",

number: "請輸入合法的數字",

digits: "只能輸入整數",

creditcard: "請輸入合法的信用卡號",

equalTo: "請再次輸入相同的值",

accept: "請輸入擁有合法尾碼名的字串",

maxlength: jQuery.validator.format("允許的最大長度為 {0} 個字元"),

minlength: jQuery.validator.format("允許的最小長度為 {0} 個字元"),

rangelength: jQuery.validator.format("允許的長度為{0}和{1}之間"),

range: jQuery.validator.format("請輸入介於 {0} 和 {1} 之間的值"),

max: jQuery.validator.format("請輸入一個最大為 {0} 的值"),

min: jQuery.validator.format("請輸入一個最小為 {0} 的值")

});

個別表單改變提示內容(只對當前表單有效)

方法一:

<input type="file" name="parResource"

class="{required: true, accept: ‘zip‘, messages: {required: ‘請選擇檔案‘, accept:‘請選擇正確的檔案‘}}">

 

方法二:

$(function() {

$("#testForm").validate({

messages:{

loginName: {

required: "必選欄位2"

},

email: {

required: ‘必選欄位22‘,

email: "請輸入正確格式的電子郵件2"

}

}

});

});

改變錯誤訊息顯示樣式

指定label.error的樣式就可以了,如下:

<style type="text/css">

label.error{

margin-left: 10px;

color: red;

}

</style>

 

說明:label.error指class為error的label元素,如:<label for="resource" class="error">

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.