jQuery.validate 中文API

jQuery.validate 中文API  

2009-12-19 16:12:49|  分類: javascript |  標籤: |字號 訂閱 javascript

HappyCZX css

 

名稱 html

返回類型 java

描述 jquery

validate(options) git

返回:Validator ajax

驗證所選的FORM express

valid() app

返回:Boolean 函數

檢查是否驗證經過

rules()

返回:Options

返回元素的驗證規則

rules("add",rules)

返回:Options

增長驗證規則

rules("remove",rules)

返回:Options

刪除驗證規則

removeAttrs(attributes)

返回:Options

刪除特殊屬性而且返回他們

Custom selectors

:blank

返回:Validator

沒有值的篩選器

:filled

返回:Array <Element >

有值的篩選器

:unchecked

返回:Array <Element >

沒選擇的元素的篩選器

Utilities

jQuery.format

(template,argument ,argumentN...)

返回:String

用參數代替模板中的 {n}

 

 

Validator:

validate方法返回一個Validator對象,它有不少方法, 讓你能使用引起校驗程序或者改變form的內容. validator對象有不少方法,但下面只是列出經常使用的

form()

返回:Boolean

驗證form返回成功仍是失敗

element(element)

返回:Boolean

驗證單個元素是成功仍是失敗

resetForm()

返回:undefined

把前面驗證的FORM恢復到驗證前原來的狀態

showErrors(errors)

返回:undefined

顯示特定的錯誤信息

 

Validator functions:

setDefaults(defaults)

返回:undefined

改變默認的設置

addMethod(name,method,message)

返回:undefined

添加一個新的驗證方法. 必須包括一個獨一無二的名字,一個JAVASCRIPT的方法和一個默認的信息

addClassRules(name,rules)

返回:undefined

增長組合驗證類型 在一個類裏面用多種驗證方法裏比較有用

addClassRules(rules)

返回:undefined

增長組合驗證類型 在一個類裏面用多種驗證方法裏比較有用,這個是一會兒加多個

 

 

內置驗證方式:

required()

返回:Boolean

必填驗證元素

required(dependency-expression)

返回:Boolean

必填元素依賴於表達式的結果

required(dependency-callback)

返回:Boolean

必填元素依賴於回調函數的結果

remote(url)

返回:Boolean

請求遠程校驗。url一般是一個遠程調用方法

minlength(length)

返回:Boolean

設置最小長度

maxlength(length)

返回:Boolean

設置最大長度

rangelength(range)

返回:Boolean

設置一個長度範圍[min,max]

min(value)

返回:Boolean

設置最大值

max(value)

返回:Boolean

設置最小值

email()

返回:Boolean

驗證電子郵箱格式

range(range)

返回:Boolean

設置值的範圍

url()

返回:Boolean

驗證URL格式

date()

返回:Boolean

驗證日期格式(相似30/30/2008的格式,不驗證日期準確性只驗證格式)

dateISO()

返回:Boolean

驗證ISO類型的日期格式

dateDE()

返回:Boolean

驗證德式的日期格式(29.04.1994 or 1.1.2006

number()

返回:Boolean

驗證十進制數字(包括小數的)

digits()

返回:Boolean

驗證整數

creditcard()

返回:Boolean

驗證信用卡號

accept(extension)

返回:Boolean

驗證相同後綴名的字符串

equalTo(other)

返回:Boolean

驗證兩個輸入框的內容是否相同

phoneUS()

返回:Boolean

驗證美式的電話號碼

 

 

validate ()的可選項:

debug:進行調試模式(表單不提交):

$(".selector").validate

({

   debug:true

})

把調試設置爲默認:

$.validator.setDefaults({

   debug:true

})

submitHandler:

經過驗證後運行的函數,裏面要加上表單提交的函數,不然表單不會提交

$(".selector").validate({

   submitHandler:function(form) {

$(form).ajaxSubmit();

   }

})

ignore:

對某些元素不進行驗證

$("#myform").validate({

   ignore:".ignore"

})

rules:

自定義規則,key:value的形式,key是要驗證的元素,value能夠是字符串或對象

$(".selector").validate({

   rules:{

     name:"required",

     email:{

       required:true,

       email:true

     }

   }

})

messages:

自定義的提示信息key:value的形式key是要驗證的元素,值是字符串或函數

$(".selector").validate({

   rules:{

     name:"required",

     email:{

       required:true,

       email:true

     }

   },

   messages:{

     name:"Name不能爲空",

     email:{

       required:"E-mail不能爲空",

       email:"E-mail地址不正確"

     }

   }

})

groups:

對一組元素的驗證,用一個錯誤提示,error Placement控制把出錯信息放在哪裏

$("#myform").validate({

  groups:{

    username:"fname lname"

  },

  errorPlacement:function(error,element) {

     if (element.attr("name") == "fname" || element.attr("name") == "lname")

       error.insertAfter("#lastname");

     else

       error.insertAfter(element);

   },

   debug:true

})

Onubmit Boolean 默認:true

是否提交時驗證

$(".selector").validate({

   onsubmit:false

})

onfocusout Boolean 默認:true 

是否在獲取焦點時驗證

$(".selector").validate({

   onfocusout:false

})

onkeyup Boolean 默認:true 

是否在敲擊鍵盤時驗證

$(".selector").validate({

   onkeyup:false

})

onclick Boolean 默認:true

是否在鼠標點擊時驗證(通常驗證checkbox,radiobox

$(".selector").validate({

   onclick:false

})

focusInvalid Boolean 默認:true

提交表單後,未經過驗證的表單(第一個或提交以前得到焦點的未經過驗證的表單)會得到焦點

$(".selector").validate({

   focusInvalid:false

})

focusCleanup Boolean 默認:false

當未經過驗證的元素得到焦點時,並移除錯誤提示(避免和 focusInvalid.一塊兒使用)

$(".selector").validate({

   focusCleanup:true

})

errorClass String 默認:"error"

指定錯誤提示的css類名,能夠自定義錯誤提示的樣式

$(".selector").validate({

   errorClass:"invalid"

})

errorElement String 默認:"label"

使用什麼標籤標記錯誤

$(".selector").validate

   errorElement:"em"

})

wrapper String

使用什麼標籤再把上邊的errorELement包起來

$(".selector").validate({

   wrapper:"li"

})

errorLabelContainer Selector

把錯誤信息統一放在一個容器裏面

$("#myform").validate({

   errorLabelContainer:"#messageBox",

   wrapper:"li",

   submitHandler:function() { alert("Submitted!") }

})

 

showErrors:

跟一個函數,能夠顯示總共有多少個未經過驗證的元素

$(".selector").validate({

   showErrors:function(errorMap,errorList) {

        $("#summary").html("Your form contains " + this.numberOfInvalids() + " errors,see details below.");

        this.defaultShowErrors();

   }

})

errorPlacement:

跟一個函數,能夠自定義錯誤放到哪裏

$("#myform").validate({

  rrorPlacement:function(error,element) {  error.appendTo(element.parent("td").next("td"));

   },

   debug:true

 

})

success:

要驗證的元素經過驗證後的動做,若是跟一個字符串,會當作一個css,也可跟一個函數

$("#myform").validate({

        success:"valid",

        submitHandler:function() { alert("Submitted!") }

})

highlight:

能夠給未經過驗證的元素加效果,閃爍等

 

 

 

addMethod(name,method,message)方法:

參數name是添加的方法的名字

參數method是一個函數,接收三個參數(value,element,param) value是元素的值,element是元素自己 param是參數,咱們能夠用addMethod來添加除built-in Validation methods以外的驗證方法 好比有一個字段,只能輸一個字母,範圍是a-f,寫法以下:

 

$.validator.addMethod("af",function(value,element,params){

   if(value.length>1){

    return false;

   }

   if(value>=params[0] && value<=params[1]){

    return true;

   }else{

    return false;

   }

},"必須是一個字母,a-f");

用的時候,好比有個表單字段的id="username",則在rules中寫

username:{

   af:["a","f"]

}

 

addMethod的第一個參數,就是添加的驗證方法的名子,這時是af

addMethod的第三個參數,就是自定義的錯誤提示,這裏的提示爲:"必須是一個字母,a-f"

addMethod的第二個參數,是一個函數,這個比較重要,決定了用這個驗證方法時的寫法

若是隻有一個參數,直接寫,若是af:"a",那麼a就是這個惟一的參數,若是多個參數,用在[],用逗號分開

 

 

meta String方式:

$("#myform").validate({

   meta:"validate",

   submitHandler:function() { alert("Submitted!") }

})

<script type="text/javascript" src="js/jquery.metadata.js"></script>

<script type="text/javascript" src="js/jquery.validate.js"></script>

<form id="myform">

  <input type="text" name="email" class="{validate:{ required:true,email:true }}" />

  <input type="submit" value="Submit" />

</form>

相關文章
相關標籤/搜索