您好,登錄后才能下訂單哦!
jQuery Validate
jQuery Validate 插件為表單提供了強大的驗證功能,讓客戶端表單驗證變得更簡單,同時提供了大量的定制選項,滿足應用程序各種需求。
1、導入 js 庫
<script src="http://static.runoob.com/assets/jquery-validation-1.14.0/lib/jquery.js"></script> <script src="http://static.runoob.com/assets/jquery-validation-1.14.0/dist/jquery.validate.min.js"></script>
2、默認校驗規則
序號 | 規則 | 描述 |
---|---|---|
1 | required:true | 必須輸入的字段。 |
2 | remote:"check.php" | 使用 ajax 方法調用 check.php 驗證輸入值。 |
3 | email:true | 必須輸入正確格式的電子郵件。 |
4 | url:true | 必須輸入正確格式的網址。 |
5 | date:true | 必須輸入正確格式的日期。日期校驗 ie6 出錯,慎用。 |
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。 |
例子:
$().ready(function() {// 在鍵盤按下并釋放及提交后驗證提交表單 $("#signupForm").validate({ rules: { firstname: "required", lastname: "required", username: { required: true, minlength: 2 }, password: { required: true, minlength: 5 }, confirm_password: { required: true, minlength: 5, equalTo: "#password" }, email: { required: true, email: true }, topic: { required: "#newsletter:checked", minlength: 2 }, agree: "required" }, messages: { firstname: "請輸入您的名字", lastname: "請輸入您的姓氏", username: { required: "請輸入用戶名", minlength: "用戶名必需由兩個字母組成" }, password: { required: "請輸入密碼", minlength: "密碼長度不能小于 5 個字母" }, confirm_password: { required: "請輸入密碼", minlength: "密碼長度不能小于 5 個字母", equalTo: "兩次密碼輸入不一致" }, email: "請輸入一個正確的郵箱", agree: "請接受我們的聲明", topic: "請選擇兩個主題" } })});
3.特別注意
對于accept的用法 accept:“MIME types”
具體的可能用到的MINIEType如下:
.dotx,application/vnd.openxmlformats-officedocument.wordprocessingml.template
.docx,application/vnd.openxmlformats-officedocument.wordprocessingml.document
.xlsx,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
.pptx,application/vnd.openxmlformats-officedocument.presentationml.presentation
.doc,application/msword
.dot,application/msword
.xls,application/vnd.ms-excel
.ppt - application/mspowerpoint
.ai - application/postscript
.aif - audio/x-aiff
.aifc - audio/x-aiff
.aiff - audio/x-aiff
.asc - text/plain
.au - audio/basic
.avi - video/x-msvideo
.bcpio - application/x-bcpio
.bin - application/octet-stream
.c - text/plain
.cc - text/plain
.ccad - application/clariscad
.cdf - application/x-netcdf
.class - application/octet-stream
.cpio - application/x-cpio
.cpt - application/mac-compactpro
.csh - application/x-csh
.css - text/css
.dcr - application/x-director
.dir - application/x-director
.dms - application/octet-stream
.doc - application/msword
.drw - application/drafting
.dvi - application/x-dvi
.dwg - application/acad
.dxf - application/dxf
.dxr - application/x-director
.eps - application/postscript
.etx - text/x-setext
.exe - application/octet-stream
.ez - application/andrew-inset
.f - text/plain
.f90 - text/plain
.fli - video/x-fli
.gif - image/gif
.gtar - application/x-gtar
.gz - application/x-gzip
.h - text/plain
.hdf - application/x-hdf
.hh - text/plain
.hqx - application/mac-binhex40
.htm - text/html
.html - text/html
.ice - x-conference/x-cooltalk
.ief - image/ief
.iges - model/iges
.igs - model/iges
.ips - application/x-ipscript
.ipx - application/x-ipix
.jpe - image/jpeg
.jpeg - image/jpeg
.jpg - image/jpeg
.js - application/x-javascript
.kar - audio/midi
.latex - application/x-latex
.lha - application/octet-stream
.lsp - application/x-lisp
.lzh - application/octet-stream
.m - text/plain
.man - application/x-troff-man
.me - application/x-troff-me
.mesh - model/mesh
.mid - audio/midi
.midi - audio/midi
.mif - application/vnd.mif
.mime - www/mime
.mov - video/quicktime
.movie - video/x-sgi-movie
.mp2 - audio/mpeg
.mp3 - audio/mpeg
.mpe - video/mpeg
.mpeg - video/mpeg
.mpg - video/mpeg
.mpga - audio/mpeg
.ms - application/x-troff-ms
.msh - model/mesh
.nc - application/x-netcdf
.oda - application/oda
.pbm - image/x-portable-bitmap
.pdb - chemical/x-pdb
.pdf - application/pdf
.pgm - image/x-portable-graymap
.pgn - application/x-chess-pgn
.png - image/png
.pnm - image/x-portable-anymap
.pot - application/mspowerpoint
.ppm - image/x-portable-pixmap
.pps - application/mspowerpoint
.ppt - application/mspowerpoint
.ppz - application/mspowerpoint
.pre - application/x-freelance
.prt - application/pro_eng
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。