jquery validate 驗證

來源:互聯網
上載者:User

JSP中引入

<script src="jquery-1.8.0.min.js" type="text/javascript"></script><script src="jquery.easyui.min.js" type="text/javascript"></script><script src="jquery.validate.js" type="text/javascript"></script>

jquery.validate.js

$.extend($.fn.validatebox.defaults.rules,{minLength : {validator : function(value, param) {return value.length >= param[0];},message : '長度需要大於{0}個字元.'},maxLength : {validator : function(value, param) {return value.length <= param[0];},message : '長度需要小於{0}個字元.'},maxCNLen : {validator : function(value, param) {var cArr = value.match(/[^\x00-\xff]/ig);     var len= value.length + (cArr == null ? 0 : cArr.length);  return len <= param[0];},message : '長度需要小於{0}個字元,中文算2個字元.'},intOrFloat : {// 驗證整數或小數validator : function(value) {return /^\d+(\.\d+)?$/i.test(value);},message : '請輸入數字,並確保格式正確'},idcard : {// 驗證身份證validator : function(value) {return /^\d{15}(\d{2}[A-Za-z0-9])?$/i.test(value);},message : '社會安全號碼碼格式不正確'},length : {validator : function(value, param) {var len = $.trim(value).length;return len >= param[0] && len <= param[1];},message : "輸入內容長度必須介於{0}和{1}之間."},phone : {// 驗證電話號碼validator : function(value) {return /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/i.test(value);},message : '格式不正確,請使用下面格式:020-88888888'},mobile : {// 驗證手機號碼validator : function(value) {return /^(13|15|18)\d{9}$/i.test(value);},message : '手機號碼格式不正確'},currency : {// 驗證貨幣validator : function(value) {return /^\d+(\.\d+)?$/i.test(value);},message : '貨幣格式不正確'},qq : {// 驗證QQ,從10000開始validator : function(value) {return /^[1-9]\d{4,9}$/i.test(value);},message : 'QQ號碼格式不正確'},integer : {// 驗證整數validator : function(value) {return /^[+]?[1-9]+\d*$/i.test(value);},message : '請輸入整數'},integerAndMaxLength : {validator : function(value, param) {return (value.length <= param[0])&& (/^[+]?[1-9]+\d*$/i.test(value));},message : '請輸入長度小於{0}個字元的整數.'},integer : {// 驗證固定長度的整數validator : function(value) {return /^[+]?[1-9]+\d*$/i.test(value);},message : '請輸入整數'},age : {// 驗證年齡validator : function(value) {return /^(?:[1-9][0-9]?|1[01][0-9]|120)$/i.test(value);},message : '年齡必須是0到120之間的整數'},chinese : {// 驗證中文validator : function(value) {return /^[\Α-\¥]+$/i.test(value);},message : '請輸入中文'},english : {// 驗證英語validator : function(value) {return /^[A-Za-z]+$/i.test(value);},message : '請輸入英文'},unnormal : {// 驗證是否包含空格和非法字元validator : function(value) {return /.+/i.test(value);},message : '輸入值不可為空和包含其他非法字元'},username : {// 驗證使用者名稱validator : function(value) {return /^[a-zA-Z][a-zA-Z0-9_]{5,15}$/i.test(value);},message : '使用者名稱不合法(字母開頭,允許6-16位元組,允許字母數字底線)'},faxno : {// 驗證傳真validator : function(value) {// return /^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[// ]){1,12})+$/i.test(value);return /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/i.test(value);},message : '傳真號碼不正確'},zip : {// 驗證郵遞區號validator : function(value) {return /^[1-9]\d{5}$/i.test(value);},message : '郵遞區號格式不正確'},ip : {// 驗證IP地址validator : function(value) {return /d+.d+.d+.d+/i.test(value);},message : 'IP地址格式不正確'},name : {// 驗證姓名,可以是中文或英文validator : function(value) {return /^[\Α-\¥]+$/i.test(value)| /^\w+[\w\s]+\w+$/i.test(value);},message : '請輸入姓名'},date : {// 驗證姓名,可以是中文或英文validator : function(value) {// 格式yyyy-MM-dd或yyyy-M-dreturn /^(?:(?!0000)[0-9]{4}([-]?)(?:(?:0?[1-9]|1[0-2])\1(?:0?[1-9]|1[0-9]|2[0-8])|(?:0?[13-9]|1[0-2])\1(?:29|30)|(?:0?[13578]|1[02])\1(?:31))|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)([-]?)0?2\2(?:29))$/i.test(value);},message : '清輸入合適的日期格式'},msn : {validator : function(value) {return /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(value);},message : '請輸入有效msn帳號(例:abc@hotnail(msn/live).com)'},same : {validator : function(value, param) {if ($("#" + param[0]).val() != "" && value != "") {return $("#" + param[0]).val() == value;} else {return true;}},message : '兩次輸入的密碼不一致!'}});

Example




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.