easyui 之表单验证

  1 /** 
  2  * 扩展easyui的validator插件rules,支持更多类型验证 
  3  */  
  4 $.extend($.fn.validatebox.defaults.rules, {  
  5             minLength : { // 判断最小长度  
  6                 validator : function(value, param) {  
  7                     return value.length >= param[0];  
  8                 },  
  9                 message : '最少输入 {0} 个字符'  
 10             },  
 11             length : { // 长度  
 12                 validator : function(value, param) {  
 13                     var len = $.trim(value).length;  
 14                     return len >= param[0] && len <= param[1];  
 15                 },  
 16                 message : "输入内容长度必须介于{0}和{1}之间"  
 17             },  
 18             phone : {// 验证电话号码  
 19                 validator : function(value) {  
 20                     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);  
 21                 },  
 22                 message : '格式不正确,请使用下面格式:020-88888888'  
 23             },  
 24             mobile : {// 验证手机号码  
 25                 validator : function(value) {  
 26                     return /^(13|15|18)\d{9}$/i.test(value);  
 27                 },  
 28                 message : '手机号码格式不正确'  
 29             },  
 30             phoneAndMobile : {// 电话号码或手机号码  
 31                 validator : function(value) {  
 32                     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) || /^(13|15|18)\d{9}$/i.test(value);  
 33                 },  
 34                 message : '电话号码或手机号码格式不正确'  
 35             },  
 36             idcard : {// 验证身份证  
 37                 validator : function(value) {  
 38                     return /^\d{15}(\d{2}[A-Za-z0-9])?$/i.test(value) || /^\d{18}(\d{2}[A-Za-z0-9])?$/i.test(value);  
 39                 },  
 40                 message : '身份证号码格式不正确'  
 41             },  
 42             intOrFloat : {// 验证整数或小数  
 43                 validator : function(value) {  
 44                     return /^\d+(\.\d+)?$/i.test(value);  
 45                 },  
 46                 message : '请输入数字,并确保格式正确'  
 47             },  
 48             currency : {// 验证货币  
 49                 validator : function(value) {  
 50                     return /^\d+(\.\d+)?$/i.test(value);  
 51                 },  
 52                 message : '货币格式不正确'  
 53             },  
 54             qq : {// 验证QQ,从10000开始  
 55                 validator : function(value) {  
 56                     return /^[1-9]\d{4,9}$/i.test(value);  
 57                 },  
 58                 message : 'QQ号码格式不正确'  
 59             },  
 60             integer : {// 验证整数  
 61                 validator : function(value) {  
 62                     return /^[+]?[1-9]+\d*$/i.test(value);  
 63                 },  
 64                 message : '请输入整数'  
 65             },  
 66             chinese : {// 验证中文  
 67                 validator : function(value) {  
 68                     return /^[\u0391-\uFFE5]+$/i.test(value);  
 69                 },  
 70                 message : '请输入中文'  
 71             },  
 72             chineseAndLength : {// 验证中文及长度  
 73                 validator : function(value) {  
 74                     var len = $.trim(value).length;  
 75                     if (len >= param[0] && len <= param[1]) {  
 76                         return /^[\u0391-\uFFE5]+$/i.test(value);  
 77                     }  
 78                 },  
 79                 message : '请输入中文'  
 80             },  
 81             english : {// 验证英语  
 82                 validator : function(value) {  
 83                     return /^[A-Za-z]+$/i.test(value);  
 84                 },  
 85                 message : '请输入英文'  
 86             },  
 87             englishAndLength : {// 验证英语及长度  
 88                 validator : function(value, param) {  
 89                     var len = $.trim(value).length;  
 90                     if (len >= param[0] && len <= param[1]) {  
 91                         return /^[A-Za-z]+$/i.test(value);  
 92                     }  
 93                 },  
 94                 message : '请输入英文'  
 95             },  
 96             englishUpperCase : {// 验证英语大写  
 97                 validator : function(value) {  
 98                     return /^[A-Z]+$/.test(value);  
 99                 },  
100                 message : '请输入大写英文'  
101             },  
102             unnormal : {// 验证是否包含空格和非法字符  
103                 validator : function(value) {  
104                     return /.+/i.test(value);  
105                 },  
106                 message : '输入值不能为空和包含其他非法字符'  
107             },  
108             username : {// 验证用户名  
109                 validator : function(value) {  
110                     return /^[a-zA-Z][a-zA-Z0-9_]{5,15}$/i.test(value);  
111                 },  
112                 message : '用户名不合法(字母开头,允许6-16字节,允许字母数字下划线)'  
113             },  
114             faxno : {// 验证传真  
115                 validator : function(value) {  
116                     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);  
117                 },  
118                 message : '传真号码不正确'  
119             },  
120             zip : {// 验证邮政编码  
121                 validator : function(value) {  
122                     return /^[1-9]\d{5}$/i.test(value);  
123                 },  
124                 message : '邮政编码格式不正确'  
125             },  
126             ip : {// 验证IP地址  
127                 validator : function(value) {  
128                     return /d+.d+.d+.d+/i.test(value);  
129                 },  
130                 message : 'IP地址格式不正确'  
131             },  
132             name : {// 验证姓名,可以是中文或英文  
133                 validator : function(value) {  
134                     return /^[\u0391-\uFFE5]+$/i.test(value) | /^\w+[\w\s]+\w+$/i.test(value);  
135                 },  
136                 message : '请输入姓名'  
137             },  
138             engOrChinese : {// 可以是中文或英文  
139                 validator : function(value) {  
140                     return /^[\u0391-\uFFE5]+$/i.test(value) | /^\w+[\w\s]+\w+$/i.test(value);  
141                 },  
142                 message : '请输入中文'  
143             },  
144             engOrChineseAndLength : {// 可以是中文或英文  
145                 validator : function(value) {  
146                     var len = $.trim(value).length;  
147                     if (len >= param[0] && len <= param[1]) {  
148                         return /^[\u0391-\uFFE5]+$/i.test(value) | /^\w+[\w\s]+\w+$/i.test(value);  
149                     }  
150                 },  
151                 message : '请输入中文或英文'  
152             },  
153             carNo : {  
154                 validator : function(value) {  
155                     return /^[\u4E00-\u9FA5][\da-zA-Z]{6}$/.test(value);  
156                 },  
157                 message : '车牌号码无效(例:粤B12350)'  
158             },  
159             carenergin : {  
160                 validator : function(value) {  
161                     return /^[a-zA-Z0-9]{16}$/.test(value);  
162                 },  
163                 message : '发动机型号无效(例:FG6H012345654584)'  
164             },  
165             same : {  
166                 validator : function(value, param) {  
167                     if ($("#" + param[0]).val() != "" && value != "") {  
168                         return $("#" + param[0]).val() == value;  
169                     } else {  
170                         return true;  
171                     }  
172                 },  
173                 message : '两次输入的密码不一致!'  
174             }  
175         });  
176 /** 
177  * 扩展easyui validatebox的两个方法.移除验证和还原验证 
178  */  
179 $.extend($.fn.validatebox.methods, {  
180             remove : function(jq, newposition) {  
181                 return jq.each(function() {  
182                     $(this).removeClass("validatebox-text validatebox-invalid").unbind('focus.validatebox').unbind('blur.validatebox');  
183                         // remove tip  
184                         // $(this).validatebox('hideTip', this);  
185                     });  
186             },  
187             reduce : function(jq, newposition) {  
188                 return jq.each(function() {  
189                     var opt = $(this).data().validatebox.options;  
190                     $(this).addClass("validatebox-text").validatebox(opt);  
191                         // $(this).validatebox('validateTip', this);  
192                     });  
193             },  
194             validateTip : function(jq) {  
195                 jq = jq[0];  
196                 var opts = $.data(jq, "validatebox").options;  
197                 var tip = $.data(jq, "validatebox").tip;  
198                 var box = $(jq);  
199                 var value = box.val();  
200                 function setTipMessage(msg) {  
201                     $.data(jq, "validatebox").message = msg;  
202                 };  
203                 var disabled = box.attr("disabled");  
204                 if (disabled == true || disabled == "true") {  
205                     return true;  
206                 }  
207                 if (opts.required) {  
208                     if (value == "") {  
209                         box.addClass("validatebox-invalid");  
210                         setTipMessage(opts.missingMessage);  
211                         $(jq).validatebox('showTip', jq);  
212                         return false;  
213                     }  
214                 }  
215                 if (opts.validType) {  
216                     var result = /([a-zA-Z_]+)(.*)/.exec(opts.validType);  
217                     var rule = opts.rules[result[1]];  
218                     if (value && rule) {  
219                         var param = eval(result[2]);  
220                         if (!rule["validator"](value, param)) {  
221                             box.addClass("validatebox-invalid");  
222                             var message = rule["message"];  
223                             if (param) {  
224                                 for (var i = 0; i < param.length; i++) {  
225                                     message = message.replace(new RegExp("\\{" + i + "\\}", "g"), param[i]);  
226                                 }  
227                             }  
228                             setTipMessage(opts.invalidMessage || message);  
229                             $(jq).validatebox('showTip', jq);  
230                             return false;  
231                         }  
232                     }  
233                 }  
234                 box.removeClass("validatebox-invalid");  
235                 $(jq).validatebox('hideTip', jq);  
236                 return true;  
237             },  
238             showTip : function(jq) {  
239                 jq = jq[0];  
240                 var box = $(jq);  
241                 var msg = $.data(jq, "validatebox").message  
242                 var tip = $.data(jq, "validatebox").tip;  
243                 if (!tip) {  
244                     tip = $("<div class=\"validatebox-tip\">" + "<span class=\"validatebox-tip-content\">" + "</span>" + "<span class=\"validatebox-tip-pointer\">" + "</span>" + "</div>").appendTo("body");  
245                     $.data(jq, "validatebox").tip = tip;  
246                 }  
247                 tip.find(".validatebox-tip-content").html(msg);  
248                 tip.css({  
249                             display : "block",  
250                             left : box.offset().left + box.outerWidth(),  
251                             top : box.offset().top  
252                         });  
253             },  
254             hideTip : function(jq) {  
255                 jq = jq[0];  
256                 var tip = $.data(jq, "validatebox").tip;  
257                 if (tip) {  
258                     tip.remove;  
259                     $.data(jq, "validatebox").tip = null;  
260                 }  
261             }  
262         });  

 

远程校验

 1 $.extend($.fn.validatebox.defaults.rules, {
 2    checkItem: {
 3         validator: function (value) {
 4             var flag = false;
 5             $.ajax({
 6                 async: false,
 7                 cache: false,                
 8                 type: 'post',
 9                 url: '/CarSecurityCheckItem/CheckName',
10                 data: {
11                     name: value
12                 },
13                 success: function (date) {
14                     if (date === 'True') {
15                         flag = true;
16                     }
17                 }
18             })
19             return flag;
20         },
21         message: '名称已存在'
22     } 
23 
24 })

前台界面:

1 <div class="state_content">
2             @Html.TextBoxFor(m => m.ItemName, new { Class = "easyui-validatebox", style = "width: 50%; height: 24px;", required = true, delay = 600, validType = "checkItem" })
3         </div>

 

http://uule.iteye.com/blog/1849690

posted @ 2017-07-11 17:33  查克拉的觉醒  阅读(509)  评论(0编辑  收藏  举报