自定义easy-ui validatebox 如maxLength()等等校验规则

  1. $(document).ready(  
  2.         function(){  
  3.               
  4.             $.extend($.fn.validatebox.defaults.rules, {     
  5.                 minLength: {     
  6.                     validator: function(value, param){   //value 为需要校验的输入框的值 , param为使用此规则时存入的参数  
  7.                         return value.length >= param[0];     
  8.                     },     
  9.                     message: '请输入最小{0}位字符.'    
  10.                 }     
  11.             });   
  12.               
  13.             $.extend($.fn.validatebox.defaults.rules, {     
  14.                 maxLength: {     
  15.                     validator: function(value, param){     
  16.                         return param[0] >= value.length;     
  17.                     },     
  18.                     message: '请输入最大{0}位字符.'    
  19.                 }     
  20.             });   
  21.               
  22.             $.extend($.fn.validatebox.defaults.rules, {     
  23.                 length: {     
  24.                     validator: function(value, param){     
  25.                         return value.length >= param[0] && param[1] >= value.length;     
  26.                     },     
  27.                     message: '请输入{0}-{1}位字符.'    
  28.                 }     
  29.             });   
  30.   
  31.         // extend the 'equals' rule     
  32.             $.extend($.fn.validatebox.defaults.rules, {     
  33.                 equals: {     
  34.                     validator: function(value,param){     
  35.                         return value == $(param[0]).val();     
  36.                     },     
  37.                     message: '字段不相同.'    
  38.                 }     
  39.             });    
  40.               
  41.             $.extend($.fn.validatebox.defaults.rules, {     
  42.                 web : {     
  43.                     validator: function(value){     
  44.                         return /^(http[s]{0,1}|ftp):\/\//i.test($.trim(value));     
  45.                     },     
  46.                     message: '网址格式错误.'    
  47.                 }     
  48.             });   
  49.                           
  50.            $.extend($.fn.validatebox.defaults.rules, {     
  51.                mobile : {     
  52.                     validator: function(value){     
  53.                         return /^1[0-9]{10}$/i.test($.trim(value));     
  54.                     },     
  55.                     message: '手机号码格式错误.'    
  56.                 }     
  57.             });  
  58.              
  59.            $.extend($.fn.validatebox.defaults.rules, {     
  60.                date : {     
  61.                     validator: function(value){     
  62.                         return /^[0-9]{4}[-][0-9]{2}[-][0-9]{2}$/i.test($.trim(value));     
  63.                     },     
  64.                     message: '曰期格式错误,如2012-09-11.'    
  65.                 }     
  66.             });   
  67.              
  68.            $.extend($.fn.validatebox.defaults.rules, {     
  69.                email : {     
  70.                     validator: function(value){     
  71.                         return /^[a-zA-Z0-9_+.-]+\@([a-zA-Z0-9-]+\.)+[a-zA-Z0-9]{2,4}$/i.test($.trim(value));     
  72.                     },     
  73.                     message: '电子邮箱格式错误.'    
  74.                 }     
  75.             });   
  76.              
  77.            $.extend($.fn.validatebox.defaults.rules, {     
  78.                captcha : {     
  79.                     validator: function(value){     
  80.                         var data0 = false;  
  81.                         $.ajax({  
  82.                             type: "POST",async:false,  
  83.                             url:contextPath + "/json/valSimulation.action",  
  84.                             dataType:"json",  
  85.                             data:{"simulation":value},  
  86.                             async:false,  
  87.                             success: function(data){  
  88.                                 data0=data;  
  89.                             }  
  90.                         });  
  91.                           
  92.                        return data0;  
  93. //                      return /^[a-zA-Z0-9]{4}$/i.test($.trim(value));  
  94.                     },     
  95.                     message: '验证码错误.'    
  96.                 }     
  97.             });   
  98.              
  99.            $.extend($.fn.validatebox.defaults.rules, {     
  100.                txtName : {     
  101.                     validator: function(value,param){     
  102.                         var data0 = false;  
  103.                         if(value.length >= param[0] && param[1] >= value.length)  
  104.                         {  
  105.                             $.ajax({  
  106.                                 type: "POST",async:false,  
  107.                                 url:contextPath + "/json/valName.action",  
  108.                                 dataType:"json",  
  109.                                 data:{"txtName":value},  
  110.                                 async:false,  
  111.                                 success: function(data){  
  112.                                     data0=!data;  
  113.                                 }  
  114.                             });  
  115.                         }else{  
  116.                             param[2] = "请输入"+param[0]+"-"+param[1]+"位字符.";  
  117.                             return false;  
  118.                         }  
  119.                           
  120.                         param[2] = "用户名称已存在.";  
  121.                        return data0;  
  122.                     },     
  123.                     message: "{2}"   
  124.                 }     
  125.             });   
  126.              
  127.     });    
[javascript]  view plain copy
  1.   
[javascript]  view plain copy
  1.   

使用:
<input type="text" class="easyui-validatebox" required=true missingMessage="必须输入" data-options="validType:'maxLength[100]'" invalidMessage="最大长度不能超过100"/>
小细节:Unicode编码的汉字是3个字节 。
Oracle 数据库字段指定为varchar2(). 还可以指定其单位为char 还是byte 
为char :
varchar2(3) 可以存 3*2/3= 2个汉字
为byte:varchar2(3)可以存3*1/3=1个汉字

[javascript]  view plain copy
  1.   
[javascript]  view plain copy
  1. <pre name="code" class="javascript"><pre></pre>  
  2. <pre></pre>  
  3. <pre name="code" class="javascript"></pre><pre name="code" class="javascript"></pre>  
  4. <pre></pre>  
  5. <pre></pre>  
  6. <pre></pre>  
  7. <pre></pre>  
  8. </pre>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值