jquery.formValidator.js
37.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
/**
* @fileOverview formValidator插件封装
* @author:Hbomb(zhouqq@yoho.cn)
* @date:2012-07-18
*/
define('lib/ui/jquery.formValidator',[],function(require,exports) {
// ====================================================================================================
// [插件名称] jQuery formValidator
// [描 述] jQuery formValidator表单验证插件,它是基于jQuery类库,实现了js脚本于页面的分离。对一个表
// 单对象,你只需要写一行代码就可以轻松实现20种以上的脚本控制。现支持一个表单元素累加很多种
// 校验方式,采用配置信息的思想,而不是把信息写在表单元素上,能比较完美的实现ajax请求。
// ====================================================================================================
var jQuery_formValidator_initConfig;
var regexEnum = {
intege : "^-?[1-9]\\d*$", // 整数
intege1 : "^[1-9]\\d*$", // 正整数
intege2 : "^-[1-9]\\d*$", // 负整数
num : "^([+-]?)\\d*\\.?\\d+$", // 数字
num1 : "^[1-9]\\d*|0$", // 正数(正整数 + 0)
num2 : "^-[1-9]\\d*|0$", // 负数(负整数 + 0)
decmal : "^([+-]?)\\d*\\.\\d+$", // 浮点数
decmal1 : "^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*$", // 正浮点数
decmal2 : "^-([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*)$", // 负浮点数
decmal3 : "^-?([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*|0?.0+|0)$", // 浮点数
decmal4 : "^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*|0?.0+|0$", // 非负浮点数(正浮点数
// + 0)
decmal5 : "^(-([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*))|0?.0+|0$", // 非正浮点数(负浮点数
// + 0)
email : "^[a-z0-9_\-]+(\.[_a-z0-9\-]+)*@([_a-z0-9\-]+\\.)+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel)$", // 邮件
color : "^[a-fA-F0-9]{6}$", // 颜色
url : "^http[s]?:\\/\\/([\\w-]+\\.)+[\\w-]+([\\w-./?%&=]*)?$", // url
chinese : "^[\\u4E00-\\u9FA5\\uF900-\\uFA2D]+$", // 仅中文
ascii : "^[\\x00-\\xFF]+$", // 仅ACSII字符
zipcode : "^\\d{6}$", // 邮编
telphone : "^((0\d{2,3})-)(\d{7,8})",
mobile : "^1[3|4|5|8][0-9]\\d{8}$", // 手机
ip4 : "^(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)$", // ip地址
notempty : "^\\S+$", // 非空
picture : "(.*)\\.(jpg|bmp|gif|ico|pcx|jpeg|tif|png|raw|tga)$", // 图片
rar : "(.*)\\.(rar|zip|7zip|tgz)$", // 压缩文件
date : "^\\d{4}(\\-|\\/|\.)\\d{1,2}\\1\\d{1,2}$", // 日期
qq : "^[1-9]*[1-9][0-9]*$", // QQ号码
username : "^\\w+$", // 用来用户注册。匹配由数字、26个英文字母或者下划线组成的字符串
letter : "^[A-Za-z]+$", // 字母
letter_u : "^[A-Z]+$", // 大写字母
letter_l : "^[a-z]+$", // 小写字母
idcard : "^[1-9]([0-9]{14}|[0-9]{17})$", // 身份证
zhCn : "^[\u4e00-\u9fa5]+$" // 中文
};
var aCity = {
11 : "北京",
12 : "天津",
13 : "河北",
14 : "山西",
15 : "内蒙古",
21 : "辽宁",
22 : "吉林",
23 : "黑龙江",
31 : "上海",
32 : "江苏",
33 : "浙江",
34 : "安徽",
35 : "福建",
36 : "江西",
37 : "山东",
41 : "河南",
42 : "湖北",
43 : "湖南",
44 : "广东",
45 : "广西",
46 : "海南",
50 : "重庆",
51 : "四川",
52 : "贵州",
53 : "云南",
54 : "西藏",
61 : "陕西",
62 : "甘肃",
63 : "青海",
64 : "宁夏",
65 : "新疆",
71 : "台湾",
81 : "香港",
82 : "澳门",
91 : "国外"
};
return (function($) {
function getElementWidth(objectId) {
x = document.getElementById(objectId);
return x.offsetWidth;
}
function getTopLeft(objectId) {
obj = new Object();
o = document.getElementById(objectId);
oLeft = o.offsetLeft;
oTop = o.offsetTop;
while (o.offsetParent != null) {
oParent = o.offsetParent;
oLeft += oParent.offsetLeft;
oTop += oParent.offsetTop;
o = oParent;
}
obj.top = oTop;
obj.left = oLeft;
return obj;
}
function isCardID(sId) {
var iSum = 0;
var info = "";
if (!/^\d{17}(\d|x)$/i.test(sId))
return "你输入的身份证长度或格式错误";
sId = sId.replace(/x$/i, "a");
if (aCity[parseInt(sId.substr(0, 2))] == null)
return "你的身份证地区非法";
sBirthday = sId.substr(6, 4) + "-"
+ Number(sId.substr(10, 2)) + "-"
+ Number(sId.substr(12, 2));
var d = new Date(sBirthday.replace(/-/g, "/"));
if (sBirthday != (d.getFullYear() + "-"
+ (d.getMonth() + 1) + "-" + d.getDate()))
return "身份证上的出生日期非法";
for ( var i = 17; i >= 0; i--)
iSum += (Math.pow(2, i) % 11)
* parseInt(sId.charAt(17 - i), 11);
if (iSum % 11 != 1)
return "你输入的身份证号非法";
return true;// aCity[parseInt(sId.substr(0,2))]+","+sBirthday+","+(sId.substr(16,1)%2?"男":"女")
}
// 短时间,形如 (13:04:06)
function isTime(str) {
var a = str.match(/^(\d{1,2})(:)?(\d{1,2})\2(\d{1,2})$/);
if (a == null) {
return false;
}
if (a[1] > 24 || a[3] > 60 || a[4] > 60) {
return false;
}
return true;
}
// 短日期,形如 (2003-12-05)
function isDate(str) {
var r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
if (r == null)
return false;
var d = new Date(r[1], r[3] - 1, r[4]);
return (d.getFullYear() == r[1]
&& (d.getMonth() + 1) == r[3] && d.getDate() == r[4]);
}
// 长时间,形如 (2003-12-05 13:04:06)
function isDateTime(str) {
var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
var r = str.match(reg);
if (r == null)
return false;
var d = new Date(r[1], r[3] - 1, r[4], r[5], r[6], r[7]);
return (d.getFullYear() == r[1]
&& (d.getMonth() + 1) == r[3]
&& d.getDate() == r[4] && d.getHours() == r[5]
&& d.getMinutes() == r[6] && d.getSeconds() == r[7]);
}
$.formValidator = {
// 各种校验方式支持的控件类型
sustainType : function(id, setting) {
var elem = $("#" + id).get(0);
var srcTag = elem.tagName;
var stype = elem.type;
switch (setting.validatetype) {
case "InitValidator":
return true;
case "InputValidator":
if (srcTag == "INPUT" || srcTag == "TEXTAREA"
|| srcTag == "SELECT") {
return true;
} else {
return false;
}
case "CompareValidator":
if (srcTag == "INPUT" || srcTag == "TEXTAREA") {
if (stype == "checkbox" || stype == "radio") {
return false;
} else {
return true;
}
}
return false;
case "AjaxValidator":
if (stype == "text" || stype == "textarea"
|| stype == "file" || stype == "password"
|| stype == "select-one") {
return true;
} else {
return false;
}
case "RegexValidator":
if (srcTag == "INPUT" || srcTag == "TEXTAREA") {
if (stype == "checkbox" || stype == "radio") {
return false;
} else {
return true;
}
}
return false;
case "FunctionValidator":
return true;
case "FillOneValidator":
return true;
}
},
initConfig : function(controlOptions) {
var settings = {
debug : false,
validatorgroup : "1",
alertmessage : false,
validobjectids : "",
forcevalid : false,
onsuccess : function() {
return true;
},
onerror : function() {
},
submitonce : false,
formid : "",
autotip : false,
tidymode : false,
errorfocus : true,
wideword : true
};
controlOptions = controlOptions || {};
$.extend(settings, controlOptions);
// 如果是精简模式,发生错误的时候,第一个错误的控件就不获得焦点
if (settings.tidymode) {
settings.errorfocus = false
}
;
if (settings.formid != "") {
$("#" + settings.formid).submit(function() {
return $.formValidator.pageIsValid("1");
})
}
;
if (jQuery_formValidator_initConfig == null) {
jQuery_formValidator_initConfig = new Array();
}
jQuery_formValidator_initConfig.push(settings);
},
// 如果validator对象对应的element对象的validator属性追加要进行的校验。
appendValid : function(id, setting) {
// 如果是各种校验不支持的类型,就不追加到。返回-1表示没有追加成功
if (!$.formValidator.sustainType(id, setting))
return -1;
var srcjo = $("#" + id).get(0);
// 重新初始化
if (setting.validatetype == "InitValidator"
|| srcjo.settings == undefined) {
srcjo.settings = new Array();
}
var len = srcjo.settings.push(setting);
srcjo.settings[len - 1].index = len - 1;
return len - 1;
},
// 如果validator对象对应的element对象的validator属性追加要进行的校验。
getInitConfig : function(validatorgroup) {
if (jQuery_formValidator_initConfig != null) {
for (i = 0; i < jQuery_formValidator_initConfig.length; i++) {
if (validatorgroup == jQuery_formValidator_initConfig[i].validatorgroup) {
return jQuery_formValidator_initConfig[i];
}
}
}
return null;
},
// 触发每个控件上的各种校验
triggerValidate : function(returnObj) {
switch (returnObj.setting.validatetype) {
case "InputValidator":
$.formValidator.inputValid(returnObj);
break;
case "CompareValidator":
$.formValidator.compareValid(returnObj);
break;
case "AjaxValidator":
$.formValidator.ajaxValid(returnObj);
break;
case "RegexValidator":
$.formValidator.regexValid(returnObj);
break;
case "FunctionValidator":
$.formValidator.functionValid(returnObj);
break;
case "FillOneValidator":
$.formValidator.fillOneValid(returnObj);
break;
}
},
// 设置显示信息
setTipState : function(elem, showclass, showmsg) {
var setting0 = elem.settings[0];
var initConfig = $.formValidator
.getInitConfig(setting0.validatorgroup);
var tip = $("#" + setting0.tipid);
if (showmsg == null || showmsg == "") {
tip.hide();
} else {
if (initConfig.tidymode) {
// 显示和保存提示信息
$("#fv_content").html(showmsg);
elem.Tooltip = showmsg;
if (showclass != "onError") {
tip.hide();
}
}
tip.removeClass();
tip.addClass(showclass);
tip.html(showmsg);
}
},
resetTipState : function(validatorgroup) {
var initConfig = $.formValidator
.getInitConfig(validatorgroup);
$(initConfig.validobjectids).each(
function() {
$.formValidator.setTipState(this, "onShow",
this.settings[0].onshow);
});
},
// 设置错误的显示信息
setFailState : function(tipid, showmsg) {
var tip = $("#" + tipid);
tip.removeClass();
tip.addClass("onError");
tip.html(showmsg);
},
// 根据单个对象,正确:正确提示,错误:错误提示
showMessage : function(returnObj) {
var id = returnObj.id;
var elem = $("#" + id).get(0);
var isvalid = returnObj.isvalid;
var setting = returnObj.setting;// 正确:setting[0],错误:对应的setting[i]
var showmsg = "", showclass = "";
var settings = $("#" + id).get(0).settings;
var intiConfig = $.formValidator
.getInitConfig(settings[0].validatorgroup);
if (!isvalid) {
showclass = "onError";
if (setting.validatetype == "AjaxValidator") {
if (setting.lastValid == "") {
showclass = "onLoad";
showmsg = setting.onwait;
} else {
showmsg = setting.onerror;
}
} else {
showmsg = (returnObj.errormsg == "" ? setting.onerror
: returnObj.errormsg);
}
if (intiConfig.alertmessage) {
var elem = $("#" + id).get(0);
if (elem.validoldvalue != $(elem).val()) {
alert(showmsg);
}
} else {
$.formValidator.setTipState(elem, showclass,
showmsg);
}
} else {
// 验证成功后,如果没有设置成功提示信息,则给出默认提示,否则给出自定义提示;允许为空,值为空的提示
showmsg = $.formValidator.isEmpty(id) ? setting.onempty
: setting.oncorrect;
$.formValidator.setTipState(elem, "onCorrect",
showmsg);
}
return showmsg;
},
showAjaxMessage : function(returnObj) {
var setting = returnObj.setting;
var elem = $("#" + returnObj.id).get(0);
if (elem.validoldvalue != $(elem).val()) {
$.formValidator.ajaxValid(returnObj);
} else {
if (setting.isvalid != undefined
&& !setting.isvalid) {
elem.lastshowclass = "onError";
elem.lastshowmsg = setting.onerror;
}
$.formValidator.setTipState(elem,
elem.lastshowclass, elem.lastshowmsg);
}
},
// 获取指定字符串的长度
getLength : function(id) {
var srcjo = $("#" + id);
var elem = srcjo.get(0);
sType = elem.type;
var len = 0;
switch (sType) {
case "text":
case "hidden":
case "password":
case "textarea":
case "file":
var val = srcjo.val();
var initConfig = $.formValidator
.getInitConfig(elem.settings[0].validatorgroup);
if (initConfig.wideword) {
for ( var i = 0; i < val.length; i++) {
if (val.charCodeAt(i) >= 0x4e00
&& val.charCodeAt(i) <= 0x9fa5) {
len += 2;
} else {
len++;
}
}
} else {
len = val.length;
}
break;
case "checkbox":
case "radio":
len = $("input[@type='" + sType + "'][@name='"
+ srcjo.attr("name") + "'][@checked]").length;
break;
case "select-one":
len = elem.options ? elem.options.selectedIndex
: -1;
break;
case "select-multiple":
len = $("select[@name=" + elem.name
+ "] option[@selected]").length;
break;
}
return len;
},
// 结合empty这个属性,判断仅仅是否为空的校验情况。
isEmpty : function(id) {
if ($("#" + id).get(0).settings[0].empty
&& $.formValidator.getLength(id) == 0) {
return true;
} else {
return false;
}
},
// 对外调用:判断单个表单元素是否验证通过,不带回调函数
isOneValid : function(id) {
return $.formValidator.oneIsValid(id, 1).isvalid;
},
// 验证单个是否验证通过,正确返回settings[0],错误返回对应的settings[i]
oneIsValid : function(id, index) {
var returnObj = new Object();
returnObj.id = id;
returnObj.ajax = -1;
returnObj.errormsg = ""; // 自定义错误信息
var elem = $("#" + id).get(0);
var settings = elem.settings;
var settingslen = settings.length;
// 只有一个formValidator的时候不检验
if (settingslen == 1) {
settings[0].bind = false;
}
if (!settings[0].bind) {
return null;
}
for ( var i = 0; i < settingslen; i++) {
if (i == 0) {
if ($.formValidator.isEmpty(id)) {
returnObj.isvalid = true;
returnObj.setting = settings[0];
break;
}
continue;
}
returnObj.setting = settings[i];
if (settings[i].validatetype != "AjaxValidator") {
$.formValidator.triggerValidate(returnObj);
} else {
returnObj.ajax = i;
}
if (!settings[i].isvalid) {
returnObj.isvalid = false;
returnObj.setting = settings[i];
break;
} else {
returnObj.isvalid = true;
returnObj.setting = settings[0];
if (settings[i].validatetype == "AjaxValidator")
break;
}
}
return returnObj;
},
// 验证所有需要验证的对象,并返回是否验证成功。
pageIsValid : function(validatorgroup) {
if (validatorgroup == null
|| validatorgroup == undefined) {
validatorgroup = "1"
}
;
var isvalid = true;
var thefirstid = "", thefirsterrmsg;
var returnObj, setting;
var error_tip = "^";
var initConfig = $.formValidator
.getInitConfig(validatorgroup);
var jqObjs = $(initConfig.validobjectids);
jqObjs
.each(function(i, elem) {
if (elem.settings[0].bind) {
returnObj = $.formValidator.oneIsValid(
elem.id, 1);
if (returnObj) {
var tipid = elem.settings[0].tipid;
// 校验失败,获取第一个发生错误的信息和ID
if (!returnObj.isvalid) {
isvalid = false;
if (thefirstid == "") {
thefirstid = returnObj.id;
thefirsterrmsg = (returnObj.errormsg == "" ? returnObj.setting.onerror
: returnObj.errormsg)
}
}
// 为了解决使用同个TIP提示问题:后面的成功或失败都不覆盖前面的失败
if (!initConfig.alertmessage) {
if (error_tip.indexOf("^"
+ tipid + "^") == -1) {
if (!returnObj.isvalid) {
error_tip = error_tip
+ tipid + "^";
}
$.formValidator
.showMessage(returnObj);
}
}
}
}
});
// 成功或失败后,进行回调函数的处理,以及成功后的灰掉提交按钮的功能
if (isvalid) {
isvalid = initConfig.onsuccess();
if (initConfig.submitonce) {
$("input[@type='submit']").attr("disabled",
true);
}
} else {
var obj = $("#" + thefirstid).get(0);
initConfig.onerror(thefirsterrmsg, obj);
if (thefirstid != "" && initConfig.errorfocus) {
$("#" + thefirstid).focus();
}
}
return !initConfig.debug && isvalid;
},
// ajax校验
ajaxValid : function(returnObj) {
var id = returnObj.id;
var srcjo = $("#" + id);
var elem = srcjo.get(0);
var settings = elem.settings;
var setting = settings[returnObj.ajax];
var ls_url = setting.url;
if (srcjo.size() == 0 && settings[0].empty) {
returnObj.setting = settings[0];
returnObj.isvalid = true;
$.formValidator.showMessage(returnObj);
setting.isvalid = true;
return;
}
if (setting.addidvalue) {
var parm = "clientid=" + id + "&" + id + "="
+ encodeURIComponent(srcjo.val());
ls_url = ls_url
+ (ls_url.indexOf("?") > 0 ? ("&" + parm)
: ("?" + parm));
}
$
.ajax({
mode : "abort",
type : setting.type,
url : ls_url,
cache : setting.cache,
data : setting.data,
async : setting.async,
dataType : setting.datatype,
success : function(data) {
if (setting.success(data)) {
$.formValidator.setTipState(elem,
"onCorrect",
settings[0].oncorrect);
setting.isvalid = true;
} else {
$.formValidator.setTipState(elem,
"onError", setting.onerror);
setting.isvalid = false;
}
},
complete : function() {
if (setting.buttons
&& setting.buttons.length > 0) {
setting.buttons.attr({
"disabled" : false
})
}
;
setting.complete;
},
beforeSend : function(xhr) {
// 再服务器没有返回数据之前,先回调提交按钮
if (setting.buttons
&& setting.buttons.length > 0) {
setting.buttons.attr({
"disabled" : true
})
}
;
var isvalid = setting.beforesend(xhr);
if (isvalid) {
setting.isvalid = false; // 如果前面ajax请求成功了,再次请求之前先当作错误处理
$.formValidator
.setTipState(
elem,
"onLoad",
settings[returnObj.ajax].onwait);
}
setting.lastValid = "-1";
return isvalid;
},
error : function() {
$.formValidator.setTipState(elem,
"onError", setting.onerror);
setting.isvalid = false;
setting.error();
},
processData : setting.processdata
});
},
// 对正则表达式进行校验(目前只针对input和textarea)
regexValid : function(returnObj) {
var id = returnObj.id;
var setting = returnObj.setting;
var srcTag = $("#" + id).get(0).tagName;
var elem = $("#" + id).get(0);
// 如果有输入正则表达式,就进行表达式校验
if (elem.settings[0].empty && elem.value == "") {
setting.isvalid = true;
} else {
var regexpress = setting.regexp;
if (setting.datatype == "enum") {
regexpress = eval("regexEnum." + regexpress);
}
if (regexpress == undefined || regexpress == "") {
setting.isvalid = false;
return;
}
setting.isvalid = (new RegExp(regexpress,
setting.param)).test($("#" + id).val());
}
},
// 函数校验。返回true/false表示校验是否成功;返回字符串表示错误信息,校验失败;如果没有返回值表示处理函数,校验成功
functionValid : function(returnObj) {
var id = returnObj.id;
var setting = returnObj.setting;
var srcjo = $("#" + id);
var lb_ret = setting.fun(srcjo.val(), srcjo.get(0));
if (lb_ret != undefined) {
if (typeof lb_ret == "string") {
setting.isvalid = false;
returnObj.errormsg = lb_ret;
} else {
setting.isvalid = lb_ret;
}
}
},
// 对input和select类型控件进行校验
inputValid : function(returnObj) {
var id = returnObj.id;
var setting = returnObj.setting;
var srcjo = $("#" + id);
var elem = srcjo.get(0);
var val = srcjo.val();
var sType = elem.type;
var len = $.formValidator.getLength(id);
var empty = setting.empty, emptyerror = false;
switch (sType) {
case "text":
case "hidden":
case "password":
case "textarea":
case "file":
if (setting.type == "size") {
empty = setting.empty;
if (!empty.leftempty) {
emptyerror = (val.replace(/^[ \s]+/, '').length != val.length);
}
if (!emptyerror && !empty.rightempty) {
emptyerror = (val.replace(/[ \s]+$/, '').length != val.length);
}
if (emptyerror && empty.emptyerror) {
returnObj.errormsg = empty.emptyerror
}
}
case "checkbox":
case "select-one":
case "select-multiple":
case "radio":
var lb_go_on = false;
if (sType == "select-one"
|| sType == "select-multiple") {
setting.type = "size";
}
var type = setting.type;
if (type == "size") { // 获得输入的字符长度,并进行校验
if (!emptyerror) {
lb_go_on = true
}
if (lb_go_on) {
val = len
}
} else if (type == "date" || type == "datetime") {
var isok = false;
if (type == "date") {
lb_go_on = isDate(val)
}
;
if (type == "datetime") {
lb_go_on = isDate(val)
}
;
if (lb_go_on) {
val = new Date(val);
setting.min = new Date(setting.min);
setting.max = new Date(setting.max);
}
;
} else {
stype = (typeof setting.min);
if (stype == "number") {
val = (new Number(val)).valueOf();
if (!isNaN(val)) {
lb_go_on = true;
}
}
if (stype == "string") {
lb_go_on = true;
}
}
setting.isvalid = false;
if (lb_go_on) {
if (val < setting.min || val > setting.max) {
if (val < setting.min && setting.onerrormin) {
returnObj.errormsg = setting.onerrormin;
}
if (val > setting.min && setting.onerrormax) {
returnObj.errormsg = setting.onerrormax;
}
} else {
setting.isvalid = true;
}
}
break;
}
},
compareValid : function(returnObj) {
var id = returnObj.id;
var setting = returnObj.setting;
var srcjo = $("#" + id);
var desjo = $("#" + setting.desid);
var ls_datatype = setting.datatype;
setting.isvalid = false;
curvalue = srcjo.val();
ls_data = desjo.val();
if (ls_datatype == "number") {
if (!isNaN(curvalue) && !isNaN(ls_data)) {
curvalue = parseFloat(curvalue);
ls_data = parseFloat(ls_data);
} else {
return;
}
}
if (ls_datatype == "date" || ls_datatype == "datetime") {
var isok = false;
if (ls_datatype == "date") {
isok = (isDate(curvalue) && isDate(ls_data))
}
;
if (ls_datatype == "datetime") {
isok = (isDateTime(curvalue) && isDateTime(ls_data))
}
;
if (isok) {
curvalue = new Date(curvalue);
ls_data = new Date(ls_data)
} else {
return;
}
}
switch (setting.operateor) {
case "=":
if (curvalue == ls_data) {
setting.isvalid = true;
}
break;
case "!=":
if (curvalue != ls_data) {
setting.isvalid = true;
}
break;
case ">":
if (curvalue > ls_data) {
setting.isvalid = true;
}
break;
case ">=":
if (curvalue >= ls_data) {
setting.isvalid = true;
}
break;
case "<":
if (curvalue < ls_data) {
setting.isvalid = true;
}
break;
case "<=":
if (curvalue <= ls_data) {
setting.isvalid = true;
}
break;
}
},
localTooltip : function(e) {
e = e || window.event;
var mouseX = e.pageX
|| (e.clientX ? e.clientX
+ document.body.scrollLeft : 0);
var mouseY = e.pageY
|| (e.clientY ? e.clientY
+ document.body.scrollTop : 0);
$("#fvtt").css({
"top" : (mouseY + 2) + "px",
"left" : (mouseX - 40) + "px"
});
},
// 用于验证两者必填其一
fillOneValid : function(returnObj) {
var id = returnObj.id;
var setting = returnObj.setting;
var relatejo1 = $("#" + id);
var relatejo2 = $("#" + setting.relateid);
setting.isvalid = true;
if ($.trim(relatejo1.val()) == ''
&& $.trim(relatejo2.val()) == '') {
setting.isvalid = false;
}
return;
}
};
// 每个校验控件必须初始化的
$.fn.formValidator = function(cs) {
var setting = {
validatorgroup : "1",
empty : false,
submitonce : false,
automodify : false,
onshow : "请输入内容",
onfocus : "请输入内容",
oncorrect : "输入正确",
onempty : "输入内容为空",
defaultvalue : null,
bind : true,
validatetype : "InitValidator",
tipcss : {
"left" : "10px",
"top" : "1px",
"height" : "20px",
"width" : "250px"
},
triggerevent : "blur",
forcevalid : false
};
// 获取该校验组的全局配置信息
cs = cs || {};
if (cs.validatorgroup == undefined) {
cs.validatorgroup = "1"
}
;
var initConfig = $.formValidator
.getInitConfig(cs.validatorgroup);
// 如果为精简模式,tipcss要重新设置初始值
if (initConfig.tidymode) {
setting.tipcss = {
"left" : "2px",
"width" : "22px",
"height" : "22px",
"display" : "none"
}
}
;
// 先合并整个配置(深度拷贝)
$.extend(true, setting, cs);
return this
.each(function(e) {
var jqobj = $(this);
var setting_temp = {};
$.extend(true, setting_temp, setting);
var tip = setting_temp.tipid ? setting_temp.tipid
: this.id + "Tip";
// 自动形成TIP
if (initConfig.autotip) {
// 获取层的ID、相对定位控件的ID和坐标
if ($("body [id=" + tip + "]").length == 0) {
aftertip = setting_temp.relativeid ? setting_temp.relativeid
: this.id;
var obj = getTopLeft(aftertip);
var y = obj.top;
var x = getElementWidth(aftertip)
+ obj.left;
$("<div class='formValidateTip'></div>")
.appendTo($("body"))
.css({
left : x + "px",
top : y + "px"
})
.prepend(
$(
'<div id="'
+ tip
+ '"></div>')
.css(
setting_temp.tipcss));
}
if (initConfig.tidymode) {
jqobj.showTooltips()
}
;
}
// 每个控件都要保存这个配置信息
setting.tipid = tip;
$.formValidator.appendValid(this.id, setting);
// 保存控件ID
var validobjectids = initConfig.validobjectids;
if (validobjectids.indexOf("#" + this.id + " ") == -1) {
initConfig.validobjectids = (validobjectids == "" ? "#"
+ this.id
: validobjectids + ",#" + this.id);
}
// 初始化显示信息
if (!initConfig.alertmessage) {
$.formValidator.setTipState(this, "onShow",
setting.onshow);
}
var srcTag = this.tagName.toLowerCase();
var stype = this.type;
var defaultval = setting.defaultvalue;
// 处理默认值
if (defaultval) {
jqobj.val(defaultval);
}
if (srcTag == "input" || srcTag == "textarea") {
// 注册获得焦点的事件。改变提示对象的文字和样式,保存原值
jqobj
.focus(function() {
if (!initConfig.alertmessage) {
if(setting.onfocus=="请输入内容")
{
return;
}
// 保存原来的状态
var tipjq = $("#" + tip);
this.lastshowclass = tipjq
.attr("class");
this.lastshowmsg = tipjq
.html();
$.formValidator
.setTipState(
this,
"onFocus",
setting.onfocus);
}
if (stype == "password"
|| stype == "text"
|| stype == "textarea"
|| stype == "file") {
this.validoldvalue = jqobj
.val();
}
});
// 注册失去焦点的事件。进行校验,改变提示对象的文字和样式;出错就提示处理
jqobj
.bind(
setting.triggerevent,
function() {
var settings = this.settings;
var returnObj = $.formValidator
.oneIsValid(
this.id,
1);
if (returnObj == null) {
return;
}
if (returnObj.ajax >= 0) {
$.formValidator
.showAjaxMessage(returnObj);
} else {
var showmsg = $.formValidator
.showMessage(returnObj);
if (!returnObj.isvalid) {
// 自动修正错误
var auto = setting.automodify
&& (this.type == "text"
|| this.type == "textarea" || this.type == "file");
if (auto
&& !initConfig.alertmessage) {
alert(showmsg);
$.formValidator
.setTipState(
this,
"onShow",
setting.onshow);
} else {
if (initConfig.forcevalid
|| setting.forcevalid) {
alert(showmsg);
this
.focus();
}
}
}
}
});
} else if (srcTag == "select") {
// 获得焦点
jqobj
.bind(
"focus",
function() {
if (!initConfig.alertmessage) {
$.formValidator
.setTipState(
this,
"onFocus",
setting.onfocus);
}
});
// 失去焦点
jqobj.bind("blur", function() {
jqobj.trigger("change")
});
// 选择项目后触发
jqobj
.bind(
"change",
function() {
var returnObj = $.formValidator
.oneIsValid(
this.id,
1);
if (returnObj == null) {
return;
}
if (returnObj.ajax >= 0) {
$.formValidator
.showAjaxMessage(returnObj);
} else {
$.formValidator
.showMessage(returnObj);
}
});
}
});
};
$.fn.inputValidator = function(controlOptions) {
var settings = {
isvalid : false,
min : 0,
max : 99999999999999,
type : "size",
onerror : "输入错误",
validatetype : "InputValidator",
empty : {
leftempty : true,
rightempty : true,
leftemptyerror : null,
rightemptyerror : null
},
wideword : true
};
controlOptions = controlOptions || {};
$.extend(true, settings, controlOptions);
return this.each(function() {
$.formValidator.appendValid(this.id, settings);
});
};
// 两者必须填写其一
$.fn.fullOneValidator = function(controlOptions) {
var settings = {
isvalid : false,
onerror : "两者必须填写其一",
validatetype : "FillOneValidator",
relateid : ''
};
controlOptions = controlOptions || {};
$.extend(true, settings, controlOptions);
return this.each(function() {
$.formValidator.appendValid(this.id, settings);
});
};
$.fn.compareValidator = function(controlOptions) {
var settings = {
isvalid : false,
desid : "",
operateor : "=",
onerror : "输入错误",
validatetype : "CompareValidator"
};
controlOptions = controlOptions || {};
$.extend(true, settings, controlOptions);
return this.each(function() {
$.formValidator.appendValid(this.id, settings);
});
};
$.fn.regexValidator = function(controlOptions) {
var settings = {
isvalid : false,
regexp : "",
param : "i",
datatype : "string",
onerror : "输入的格式不正确",
validatetype : "RegexValidator"
};
controlOptions = controlOptions || {};
$.extend(true, settings, controlOptions);
return this.each(function() {
$.formValidator.appendValid(this.id, settings);
});
};
$.fn.functionValidator = function(controlOptions) {
var settings = {
isvalid : true,
fun : function() {
this.isvalid = true;
},
validatetype : "FunctionValidator",
onerror : "输入错误"
};
controlOptions = controlOptions || {};
$.extend(true, settings, controlOptions);
return this.each(function() {
$.formValidator.appendValid(this.id, settings);
});
};
$.fn.ajaxValidator = function(controlOptions) {
var settings = {
isvalid : false,
lastValid : "",
type : "GET",
url : "",
addidvalue : true,
datatype : "html",
data : "",
async : true,
cache : false,
beforesend : function() {
return true;
},
success : function() {
return true;
},
complete : function() {
},
processdata : false,
error : function() {
},
buttons : null,
onerror : "服务器校验没有通过",
onwait : "正在等待服务器返回数据",
validatetype : "AjaxValidator"
};
controlOptions = controlOptions || {};
$.extend(true, settings, controlOptions);
return this.each(function() {
$.formValidator.appendValid(this.id, settings);
});
};
$.fn.defaultPassed = function(onshow) {
return this
.each(function() {
var settings = this.settings;
for ( var i = 1; i < settings.length; i++) {
settings[i].isvalid = true;
if (!$.formValidator
.getInitConfig(settings[0].validatorgroup).alertmessage) {
var ls_style = onshow ? "onShow"
: "onCorrect";
$.formValidator
.setTipState(this, ls_style,
settings[0].oncorrect);
}
}
});
};
$.fn.unFormValidator = function(unbind) {
return this.each(function() {
this.settings[0].bind = !unbind;
if (unbind) {
$("#" + this.settings[0].tipid).hide();
} else {
$("#" + this.settings[0].tipid).show();
}
});
};
$.fn.showTooltips = function() {
if ($("body [id=fvtt]").length == 0) {
fvtt = $("<div id='fvtt' style='position:absolute;z-index:56002'></div>");
$("body").append(fvtt);
fvtt
.before("<iframe src='about:blank' class='fv_iframe' scrolling='no' frameborder='0'></iframe>");
}
return this
.each(function() {
jqobj = $(this);
s = $("<span class='top' id=fv_content style='display:block'></span>");
b = $("<b class='bottom' style='display:block' />");
this.tooltip = $(
"<span class='fv_tooltip' style='display:block'></span>")
.append(s).append(b).css({
"filter" : "alpha(opacity:95)",
"KHTMLOpacity" : "0.95",
"MozOpacity" : "0.95",
"opacity" : "0.95"
});
// 注册事件
jqobj.mouseover(function(e) {
$("#fvtt").append(this.tooltip);
$("#fv_content").html(this.Tooltip);
$.formValidator.localTooltip(e);
});
jqobj.mouseout(function() {
$("#fvtt").empty();
});
jqobj.mousemove(function(e) {
$("#fv_content").html(this.Tooltip);
$.formValidator.localTooltip(e);
});
});
}
$.regexEnum = regexEnum;
$.aCity = aCity;
});
});