繼承easyui的default驗證,自定義驗證規則以下:java
(function($){jquery
//計算字符串或者漢字的長度 function getByteLength(s){ var len = 0; for(i=0;i<s.length;i++) { var c = s.substr(i,1); var ts = escape(c); if(ts.substring(0,2) == "%u") { len += 2; } else if(ts== "%B7") { len += 2; } else { len += 1; } }//eof:for return len; }; //jquery-easyUI $.extend($.fn.validatebox.defaults.rules, { //校驗字符輸入的限制(只能輸入數字和字母) lettersandnumericonly:{ validator:function(value,param){ return /^[a-zA-Z0-9]+$/i.test(value); }, message:'{0}只能輸入數字和字母' }, //只能輸入數字 numericonly:{ validator:function(value,param){ return /^[0-9]+$/i.test(value); }, message:'{0}只能輸入數字!' }, //只能輸入正整數 positiveInteger:{ validator:function(value,param){ return /^[0-9]+$/i.test(value) && parseInt($.trim(value)) > 0; }, message:'{0}只能輸入正整數!' }, //只能輸入數字(而且有範圍) numericRange:{ validator:function(value,param){ return /^[0-9]+$/i.test(value) && parseInt($.trim(value)) <= param[1]; }, message:'{0}只能輸入數字,且不能大於{1}' }, oneToTwoInteger:{ validator:function(value,param){ return /^[0][[.]\d{1,10}]?$/i.test(value)||/^[1]?$/i.test(value)||/^[0]?$/i.test(value); }, message:'{0}只能輸入數字,且在0到1之間' }, //驗證(-20-130)的整數 integerALLKinds : { validator : function(value) { return /^[-|+]?[0-9]+\d*$/i.test(value) && value >= -20 && value <= 130; }, message : '請輸入-20到130的任意整數' }, //判斷最長 maxLength:{ validator:function(value,param){ return getByteLength($.trim(value)) <= param[1] ; }, message:'{0}長度不能超過{1}個字符或者{2}個漢字' }, //判斷最小長度 minLength : { validator : function(value, param) { var len = $.trim(value).length; return len >= param[0]; }, message :'{0}長度最小{1}個字符' }, //判斷長度範圍 length:{validator:function(value,param){ var len=$.trim(value).length; return len>=param[0]&&len<=param[1]; }, message:"{0}內容長度介於{1}和{2}之間." }, //驗證年齡 age:{ validator : function(value) { return /^[0-9]+$/i.test(value) && parseInt($.trim(value)) >= 0 && value>=0 && value <=150; }, message:'年齡格式不對,請輸出0-150之間的數字.' }, //驗證電話號碼 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 : '手機號碼格式不正確(正確格式如:13/15/18*********)' }, //驗證手機或電話 phoneOrMobile:{ validator : function(value) { return /^(13|14|15|17|18)\d{9}$/i.test(value) || /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/i.test(value) || /^\d{8}$/i.test(value); }, message:'請填入手機或電話號碼,如13/14/15/17/18*********或020-8888888或88888888' }, //驗證身份證 idcard : { validator : function(value) { return /^\d{15}(\d{2}[A-Za-z0-9])?$/i.test(value); }, message : '身份證號碼格式不正確' }, //驗證是否爲小數或整數 floatOrInt : { validator : function(value) { return /^(\d{1,3}(,\d\d\d)*(\.\d{1,3}(,\d\d\d)*)?|\d+(\.\d+))?$/i.test(value); }, message : '請輸入數字,並保證格式正確' }, IntegerOrFloat:{ validator:function(value){ return /^[0-9]+(\.[0-9]+)?$/i.test(value) ; }, message : '請輸入數值型數據' }, //驗證貨幣 currency : { validator : function(value) { return /^d{0,}(\.\d+)?$/i.test(value); }, message : '貨幣格式不正確' }, //驗證整數 integer : { validator : function(value) { return /^[+]?[1-9]+\d*$/i.test(value); }, message : '請輸入整數' }, //驗證中文 chinese : { validator : function(value) { return /^[\u0391-\uFFE5]+$/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 : '輸入值不能爲空和包含其餘非法字符' }, //驗證郵政編碼 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地址格式不正確' }, //email校驗 email:{ validator : function(value){ return /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(value); }, message : '請輸入有效的電子郵件帳號(例:abc@126.com)' }, //校驗值是否存在 param[0]: table, param[1]: colum, param[2]: add和修改區分,param[3]:修改前的value valueExists:{ validator : function(value,param){ var bool = true; var flag = 0; if(param[2]){ if(param[2]=='edit'){ if(value == param[3]){ flag = 1 ; } } } if(flag == 0){ //校驗編號是否存在 $.ajax({ url:"url ?table="+param[0]+"&colum="+param[1]+"&value="+value, type:'post', async:false, dataType:'json', success:function(result){ var result = eval(result); if(result.flag == 0){ bool = true ; }else{ bool = false; } } }); } return bool; }, message : '當前資源已被佔用' } });
})(jQuery)ajax
以上爲例子,根據項目須要修改。json
一、jsp代碼jsp
<form id="gfxwjForm" name="gfxwjForm" action="" method="post" enctype="multipart/form-data"> <div class="win_body_cln_1"> 名 稱:<input class="easyui-textbox" id="txt_mc" name="txt_mc" data-options="required:true" missingMessage="不能爲空!" style="height:30px" value="${zywsptGfxwjb.mc}"> </div>async
<input type="button" class="button" id="btn_save" value="保存"/> </form> 二、js代碼post
$('#btn_save').bind('click', function(){ if(($("#gfxwjForm").form('validate'))) { saveCommonWin(); } });ui
三、java代碼 略編碼