forked from hestiacp/hestiacp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidators.js
More file actions
208 lines (193 loc) · 7.14 KB
/
Copy pathvalidators.js
File metadata and controls
208 lines (193 loc) · 7.14 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
/*App.Validate.form = function(values){
if(values.IP_ADDRESS == '') {
return alert('Not correct ip');
}
return true;
}*/
App.Validate.Is = {
ip: function(object) {
var ip_regexp = new RegExp(/\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/);
return ip_regexp.test() ? false : App.i18n.getMessage('incorrect_ip');
}
};
App.Validate.getFieldName = function(elm)
{
fb.log(elm);
var txt_label = $(elm).prev('label').text();
if (txt_label.trim() == '') {
txt_label = $(elm).parents('.field-box').select('label:first').text();
}
return ['<strong>', txt_label, '</strong>'].join('');
}
App.Validate.Rule = {
'username' : function(elm) {
if ($(elm).val().trim() != '' && $(elm).val().search(/[^a-zA-Z_]+/) != -1) {
return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' is required'};
}
return {VALID: true};
},
'required' : function(elm) {
if ($(elm).val().trim() == '') {
return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' is required'};
}
return {VALID: true};
},
'numeric': function(elm) {
if ($(elm).val().trim() != '' && isNaN(parseInt($(elm).val(), 10))) {
return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' is incorrect'};
}
return {VALID: true};
},
'no-spaces': function(elm) {
if ($(elm).val().trim() != '' && $(elm).val().search(/\s/) != -1) {
return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' cannot contain spaces'};
}
return {VALID: true};
},
'abc': function(elm) {
if ($(elm).val().trim() != '' && $(elm).val().search(/[^a-zA-Z]+/) != -1) {
return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' must contain only letters without spaces or other symbols'};
}
return {VALID: true};
},
'email': function(elm) {
if ($(elm).val().search(/^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/) == -1) {
return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' not a valid email'};
}
return {VALID: true};
},
'ip': function(elm) {
if ($(elm).val().trim() != '' && (/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/).test($(elm).val()) == false) {
return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' not a valid IP value'};
}
return {VALID: true};
},
'domain': function(elm) {
if ($(elm).val().trim() != '' && (/^([a-z0-9\.])*[a-z0-9][a-z0-9\-]+[a-z0-9](\.[a-z]{2,4})+$/).test($(elm).val()) == false) {
return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' not a valid domain name'};
}
return {VALID: true};
},
'ns': function(elm) {
if ($(elm).val().trim() != '' && (/^([a-z0-9\.])*[a-z0-9][a-z0-9\-]+[a-z0-9](\.[a-z]{2,4})+$/).test($(elm).val()) == false) {
return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' not a valid NS name'};
}
return {VALID: true};
},
'minute': function(elm) {
if ($(elm).val() == '*') {
return {VALID: true};
}
var minute = parseInt($(elm).val(), 10);
if (minute > 60 || minute < 0) {
return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' wrong minute value'};
}
return {VALID: true};
},
'hour': function(elm) {
if ($(elm).val() == '*') {
return {VALID: true};
}
var hour = parseInt($(elm).val(), 10);
if (hour > 60 || hour < 0) {
return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' wrong hour value'};
}
return {VALID: true};
},
'wday': function(elm) {
if ($(elm).val() == '*') {
return {VALID: true};
}
var wday = parseInt($(elm).val(), 10);
if (wday > 7 || wday < 1) {
return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' wrong week day value'};
}
return {VALID: true};
},
'month': function(elm) {
if ($(elm).val() == '*') {
return {VALID: true};
}
var month = parseInt($(elm).val(), 10);
if (month > 1 || month < 12) {
return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' wrong month value'};
}
return {VALID: true};
},
'day': function(elm) {
if ($(elm).val() == '*') {
return {VALID: true};
}
var day = parseInt($(elm).val(), 10);
if (day > 31 || day < 1) {
return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' wrong day value'};
}
return {VALID: true};
}
}
App.Validate.form = function(world, elm)
{
var form_valid = true;
App.Env.FormError = [];
$(elm).find('select, input, textarea').each(function(i, field)
{
if ($(field).attr('type') == 'checkbox') {
var value = $(field).attr('checked') ? 'on' : 'off';
$(field).val(value);
}
if ($.inArray($(field).attr('name'), ['target', 'source', 'save']) != -1) {
//return; // pass
}
else {
var rules = App.Validate.getRules(field);
$(rules).each(function(i, rule)
{
fb.log('Validate with %o %o', rule, field);
if (App.Validate.Rule[rule]) {
var result = App.Validate.Rule[rule](field);
fb.log(result);
if (result.VALID == false) {
App.Env.FormError.push(result.ERROR); //$(field).attr('name') + ' is required');
form_valid = false;
}
}
})
/*if ($(field).val().trim() == '' || $(field).val().trim() == '-') {
App.Env.FormError.push($(field).attr('name') + ' is required');
form_valid = false;
}*/
}
});
return form_valid;
}
App.Validate.displayFormErrors = function(world, elm)
{
var errors_tpl = '';
$(App.Env.FormError).each(function(i, error)
{
var tpl = App.Templates.get('error_elm', 'general');
tpl.set(':ERROR', error);
errors_tpl += tpl.finalize();
});
var ref = $('.form-error', elm);
ref.removeClass('hidden');
ref.html(errors_tpl);
}
App.Validate.getRules = function(elm)
{
try {
var rules_string = $(elm).attr('class');
var rules = [];
$(rules_string.split(/\s/)).each(function(i, str)
{
var rule = str.split('rule-');
if (rule.length > 1) {
rules[rules.length++] = rule[1];
}
});
return rules;
}
catch(e) {
return [];
}
}