-
Notifications
You must be signed in to change notification settings - Fork 1
/
validator.js
194 lines (175 loc) · 5.01 KB
/
validator.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
// Generated by CoffeeScript 1.9.3
(function() {
var Validator;
Validator = {};
Validator.validators = {};
Validator.messages = {};
Validator.messageHandlers = {};
Validator.cfg = {
ignore: ':hidden'
};
Validator.validate = function(id) {
var $elem, $form, elem, elements, i, len, pass, type;
$form = $('#' + id);
if ($form[0].nodeName.toLowerCase() !== 'form') {
throw 'should be form';
}
elements = $form.data("_" + id + "_");
if (!elements) {
elements = $form.find('input, textarea, select').not(':submit, :reset, :image, [disabled]').not(this.cfg.ignore);
$form.data("_" + id + "_", elements);
}
pass = true;
for (i = 0, len = elements.length; i < len; i++) {
elem = elements[i];
type = elem.nodeName.toLowerCase();
$elem = $(elem);
if (!this.validateElement($elem, type)) {
pass = false;
}
}
return pass;
};
Validator.validateElement = function(elem, type) {
var pass;
if (type === 'textarea') {
type = 'input';
}
pass = Validator[type].call(this, elem);
return pass;
};
Validator.input = function(elem) {
var pass, ref, rule, val, validator;
pass = true;
if (this.checkable(elem)) {
pass = this.check(elem);
} else {
val = elem.val();
ref = this.validators;
for (rule in ref) {
validator = ref[rule];
if (elem.is('[' + rule + ']')) {
if (!(pass = validator.call(this, val, elem))) {
this.tip(rule, elem);
break;
}
}
}
}
return pass;
};
Validator.select = function(elem) {
var pass;
pass = !!elem.val();
if (!pass) {
elem.css('color', 'red');
}
elem.one('change', function() {
return $(this).css('color', '');
});
return pass;
};
Validator.check = function(elem) {
var $group, checkedCount, elem0, form, name, pass;
pass = true;
name = elem[0].name;
form = elem[0].form;
$group = $(form).find("[name=" + name + "]");
elem0 = $($group[0]);
if (elem0.is('[required]')) {
checkedCount = $group.filter(':checked').length;
pass = checkedCount > 0;
if (!pass) {
this.messageHandlers.check.call(this, ' ', elem);
}
}
return pass;
};
Validator.tip = function(rule, elem) {
var message, messageHandler;
messageHandler = rule in this.messageHandlers ? rule : 'defaults';
message = rule in this.messages ? rule : 'defaults';
return this.messageHandlers[messageHandler].call(this, this.messages[message].call(this, elem), elem);
};
Validator.isOptional = function(val, elem) {
var it_is_optional;
it_is_optional = elem.is('[required]') ? this.validators.required.call(this, val, elem) : val.length;
return !it_is_optional;
};
Validator.checkable = function(elem) {
return /radio|checkbox/i.test(elem.attr('type'));
};
$.extend(Validator.validators, {
required: function(val, elem) {
return !!val;
},
max: function(val, elem) {
var max;
max = elem.attr('max');
return this.isOptional(val, elem) || val.length <= max;
},
min: function(val, elem) {
var min;
min = elem.attr('min');
return this.isOptional(val, elem) || val.length >= min;
},
regexp: function(val, elem) {
var re, reStr;
reStr = elem.attr('regexp');
re = new RegExp(reStr);
return this.isOptional(val, elem) || re.test(val);
},
hidden: function(val, elem) {
return !!val;
}
});
$.extend(Validator.messageHandlers, {
defaults: function(msg, elem) {
return elem.css('color', 'red').val(msg).one('focus', function() {
return $(this).val('').css('color', '');
});
},
hidden: function(msg, elem) {
return console.log((elem.attr('name')) + " say: i am hidden input but i am required");
},
check: function(msg, elem) {
return console.log((elem.attr('name')) + " say: i am required");
}
});
$.extend(Validator.messages, {
defaults: function(elem) {
var msg;
msg = elem.attr('data-msg');
return msg != null ? msg : msg = 'Wrong';
},
required: function(elem) {
var msg;
msg = elem.attr('data-msg');
return msg != null ? msg : msg = 'Mandatory';
},
min: function(elem) {
var min, msg;
min = elem.attr('min');
msg = elem.attr('data-msg');
return msg != null ? msg : msg = "It may not be less then " + min + " characters";
},
max: function(elem) {
var max, msg;
max = elem.attr('max');
msg = elem.attr('data-msg');
return msg != null ? msg : msg = "It may not be more then " + min + " characters";
},
regexp: function(elem) {
var msg;
msg = elem.attr('data-msg');
return msg != null ? msg : msg = 'Wrong format';
}
});
this.Validator || (this.Validator = Validator);
$.extend($.fn, {
validate: function(id) {
id = this.attr('id');
return Validator.validate(id);
}
});
}).call(this);