Validate.js
3.64 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
/**
* founder : zzf
* Creation time : 2015/2/25
* note :
* role :
*/
define(function( require, exports, module ) {
var Validate = function(arges, fun){
var config = {
data : [],//数据源
view : null,
alerts : null
};
var self = this;
var regulars = {
"username" : {
tip : "请填写正确用户名格式,4-20位中英文或数字",
regular : /[a-z0-9\u4E00-\u9FFF]{4,20}/
},
"password" : {
tip : "请填写正确密码格式,6-20位字母或数字",
regular : /^[a-z_A-z0-9@#\.-]{6,20}$/
},
phone : {
tip : "请输入正确的手机号",
regular : /^(13|15|18|17|14)[0-9]{9}$/
},
code : {
tip : "请填写验证码",
regular : /\S/
},
zh_cn : {
tip : "名字不超过10个汉字或20个英文字符",
regular : /([\u4E00-\u9FA5]{1,10})|([A-Za-z]{1,20})/
},
identitycard : {
tip : "请填写正确的身份证号码",
regular : /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
}
}
var method = {
serialize : function(){
$.extend(config, arges);
var val = $(config.view).serializeArray();
for(i in val){
var aval = val[i];
var obj = $('[name='+val[i].name+']');
var rel = method.isreg(obj.attr("regular"));
if("object" === typeof(rel)){
rel["value"] = aval.value;
};
config.data.push(rel);
};
this.send();
},
isreg : function(str){
if(! str){return null;}
for(i in regulars){
if(i === str){
return regulars[i];
}
};
return null;
},
parse : function(){
},
send : function(){
for(j in config.data){
var adata = config.data[j];
if(adata.value) {
if ((!adata.regular) || (!adata.regular.test(adata.value))) {
method.alert(adata.tip);
return false;
}
}else{
method.alert("请完善表单信息!");
return false;
};
}
fun();
},
alert : function(txt){
if(config.alerts){
config.alerts.show(txt);
}else{
alert(txt);
}
}
};
this.get = function(key){
if(! key){return false;}
for(i in config){
if(i === key){
return config[i];
}
}
return false;
};
this.set = function(Object){
if("object" !== typeof(Object)){return false;}
var isSucceed = false;
for(i in config){
for(k in Object){
if(i === k){
config[i] = Object[k];
isSucceed = true;
}
}
}
return isSucceed;
};
method.serialize();
return this;
};
return Validate;
});