-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
246 lines (217 loc) · 5.97 KB
/
index.js
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
(function (root, factory) {
'use strict';
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = factory(root);
} else {
exports = factory(root);
}
} else {
root.validator = factory(root);
}
}(this, function (root) {
'use strict';
var previousValidator = root.validator,
validator = {};
validator.VERSION = '0.1.0';
validator.noConflict = function () {
root.validator = previousValidator;
return this;
};
var regPhone = /^(?:(?:1(?:3[4-9]|5[012789]|8[78])\d{8}|1(?:3[0-2]|5[56]|8[56])\d{8}|18[0-9]\d{8}|1[35]3\d{8})|14[57]\d{8}|170[059]\d{7}|17[67]\d{8})$/,
regMail = /^([_a-zA-Z\d\-\.])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
var method = validator.method = {
required: function(val) {
return val.length !== 0;
},
tel: function(val) {
return val.length == 0 || regPhone.test(val);
},
email: function(val) {
return val.length == 0 || regMail.test(val);
},
number: function(val) {
return val.length == 0 || !isNaN(Number(val));
},
int: function(val) {
return val.length == 0 || !isNaN(Number(val));
},
max: function(val, lim) {
return val.length == 0 || !isNaN(Number(val)) && Number(val) <= lim;
},
min: function(val, lim) {
return val.length == 0 || !isNaN(Number(val)) && Number(val) >= lim;
},
maxlength: function(val, lim) {
return val.length <= lim;
},
minlength: function(val, lim) {
return val.length >= lim;
},
same: (function(){
if(root.$) {
return function(val, tag) {
return val == root.$("[name="+tag+"]").val();
}
} else {
return function(val, tag) {
return val == document.querySelector("[name="+tag+"]").value;
}
}
}())
};
method.required.msg = "不能为空!";
method.tel.msg = "请输入合法手机号码!";
method.email.msg = "请输入合法邮箱!";
method.number.msg = "请输入数字!";
method.int.msg = "请输入整数!";
method.max.msg = "数字太大!";
method.min.msg = "数字太小!";
method.maxlength.msg = "数位太长!";
method.minlength.msg = "数位太短!";
var makeId = validator.uniqueId = function (prefix) {
var idCounter = 0;
return function () {
var id = (idCounter++).toString();
return prefix ? prefix + id : id;
}
};
function extend(obj) {
var length = arguments.length;
for (var index = 1; index < length; index++) {
var source = arguments[index];
for (var key in source) {
if (source.hasOwnProperty(key)) {
obj[key] = source[key];
}
}
}
return obj;
}
var mId = makeId('m');
var Model = validator.Model = function (par) {
this.model = {
value: '',
form: null,
method: [],
status: 'unverify',
prompts: {}
};
par && extend(this.model, par);
this.MID = mId();
};
extend(Model.prototype, {
fashion: function (obj) {
extend(this.model, obj);
},
destroy: function () {
this.model = null;
this.form = null;
},
verify: function (cb, context) {
var model = this.model,
arr;
context = context ? context : root;
model.status = 'verifying';
model.reason = "";
for (var i = 0; i < model.method.length; i++) {
arr = model.method[i].split('_');
if (validator.method[arr[0]] && !validator.method[arr[0]](model.value, arr[1])) {
model.status = 'fail';
model.reason = arr[0];
break;
}
};
model.status = model.status == 'fail' ? 'fail' : 'ok';
if (root.$) {
if(model.status == 'fail'){
$(model.dom).addClass('wrong');
} else {
$(model.dom).removeClass('wrong');
}
} else {
if(model.status == 'fail'){
model.dom.className = model.dom.className.search(/\bwrong\b/) == -1 ? model.dom.className + ' wrong' : model.dom.className;
} else {
model.dom.className = model.dom.className.replace(/\bwrong\b/, "");
}
}
model.status == 'fail' && cb && cb(model.prompts[model.reason] || model.prompts.all || validator.method[model.reason].msg);
}
})
var Form = validator.Form = function (par, cb, context) {
if (par) {
this._root = par.form || document;
this.event = par.event || (par.target ? 'click' : 'blur');
this.target = par.target;
}
var is$;
var that = this;
that.content = {};
if(root.$ && this._root instanceof $){
is$ = true;
var domList = this._root.find("[rg-rule]");
} else {
var domList = this._root.querySelectorAll("[rg-rule]");
}
for (var i = 0; i < domList.length; i++) {
var dom = domList[i],
attr = dom.attributes,
prompts = {};
for (var j = 0;j < attr.length; j++){
if(attr[j].name.match('rg-msg')){
prompts[attr[j].name.substring(7)] = attr[j].value;
}
}
var model = new Model({
value: dom.value || '',
dom: dom,
method: dom.getAttribute('rg-rule').split('|'),
prompts: prompts
});
model.form = that;
that.content[model.MID] = model;
dom.setAttribute('rg-model', model.MID);
if (is$){
if (!that.target) {
$(dom).on(this.event, function (event) {
that.content[event.target.getAttribute('rg-model')].model.value = event.target.value;
that.handle(event.target.getAttribute('rg-model'), cb, context);
});
}
} else {
if (!that.target) {
dom.addEventListener(this.event, function (event) {
that.content[event.target.getAttribute('rg-model')].model.value = event.target.value;
that.handle(event.target.getAttribute('rg-model'), cb, context);
});
}
}
}
if (that.target) {
if(root.$){
$(that.target).on('click', function (event) {
for (var prop in that.content) {
if (that.content.hasOwnProperty(prop)) {
that.handle(prop, cb, context);
}
}
});
}else{
that.target.addEventListener('click', function (event) {
for (var prop in that.content) {
if (that.content.hasOwnProperty(prop)) {
that.handle(prop, cb, context);
}
}
});
}
}
};
extend(Form.prototype, {
handle: function (mid, cb, context) {
this.content[mid].verify(cb, context);
}
});
return validator;
}));