-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.validate.unobtrusive.globalize.js
366 lines (331 loc) · 14.5 KB
/
jquery.validate.unobtrusive.globalize.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
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
(function ($) {
var mvcct = window["mvcct"] = window["mvcct"] || {};
var support = null;
var enhancer = mvcct.enhancer;
if (!enhancer) {
support=Html5InputSupport= {
number: 4,
range: 4,
date: 4,
month: 4,
week: 4,
time: 4,
datetime: 4,
email: 4,
search: 4,
tel: 4,
url: 4,
color: 4
};
}
else {
support = null;
}
var numberRegEx = null, rangeRegEx=null;
$.validator.attributeRules = function () { };
$.validator.globalization = {};
var initialized = false;
var neutralTimeParser, neutralDateTimeParser, neutralMonthParser, neutralWeekParser, weekParser;
var neutralNumberParser = parseFloat;
var dateTimeAdder, weekAdder, numberAdder;
var parsers = [null, null, null, null, null, null, null, null, null, null, null, null, null, null];
var neutralParsers = [null, null, null, null, null, null, null, null, null, null, null, null, null, null];
var adders = [null, null, null, null, null, null, null, null, null, null, null, null, null, null];
var formatters = [null, null, null, null, null, null, null, null, null, null, null, null, null, null];
function getType(jElement) {
return parseInt(jElement.attr("data-val-correcttype-type"))+(jElement.attr("data-is-range")
|| jElement.attr("type") == "range" ? 10 : 0);
}
function otherElement(el, ref) {
var name = el.name;
var otherName=null;
var index = name.lastIndexOf(".");
if (index < 0) otherName = ref;
else otherElement = name.substring(0, index + 1) + ref;
if(!otherElement) return null;
var res = $("[name='" + otherName + "']");
return res.length > 0 ? res[0] : null;
};
function otherElements(el, names) {
if (!names) return [];
names = names.split(" ");
var res = [];
for (var i = 0; i < names.length; i++) res.push(otherElement(el, names[i]));
return res;
}
function getDateOfISOWeek(cw) {
var parts=cw.split('-W');
var y=parseInt(parts[0]);
var w = parseInt(parts[1]);
var simple = new Date(y, 0, 1 + (w - 1) * 7);
var dow = simple.getDay();
var ISOweekStart = simple;
if (dow <= 4)
ISOweekStart.setDate(simple.getDate() - simple.getDay() + 1);
else
ISOweekStart.setDate(simple.getDate() + 8 - simple.getDay());
return ISOweekStart;
}
function initialize(toptions) {
if (initialized) return;
var Globalize = enhancer.Globalize();
if (!support) support = mvcct.enhancer.getSupport().Html5InputSupport;
var locale = Globalize.locale().attributes.language;
var nInfos = Globalize.cldr.get('main/' + locale).numbers["symbols-numberSystem-latn"];
if (support.number > 2) {
numberRegEx = new RegExp("^[\+\-\.0-9]*$");
}
else {
numberRegEx = new RegExp("^[\\" + nInfos.plusSign + "\\" + nInfos.minusSign + "\\" + nInfos.decimal + "0-9]*$");
};
if (support.range > 2) {
rangeRegEx = new RegExp("^[\+\-\.0-9]*$");
}
else {
rangeRegEx = new RegExp("^[\\" + nInfos.plusSign + "\\" + nInfos.minusSign + "\\" + nInfos.decimal + "0-9]*$");
};
neutralTimeParser = Globalize.dateParser({ raw: "HH:mm:ss" });
neutralDateTimeParser = Globalize.dateParser({ raw: "yyyy-MM-ddTHH:mm:ss" })
neutralMonthParser = Globalize.dateParser({ raw: "yyyy-MM" });
neutralWeekParser = support.week > 2 ?
function (s) {
return s;
}
:
getDateOfISOWeek;
weekParser = support.week > 2 ? function (x) { return x } : function(x){return enhancer.parse("week", x)};
weekFormatter = support.week > 2 ? function (x) { return x } : function(x){return enhancer.format("week", x)};
numberAdder = function (base, value) {
if (!base ) return null;
if (!value) return base;
return base + parseFloat(value);
}
dateTimeAdder = function (base, value) {
if (!base) return null;
if (!value) return base;
return new Date(base.getTime() + parseInt(value));
}
var weekHelp = document.createElement("INPUT");
weekHelp.setAttribute("type", "week");
weekAdder = support.week > 2 ?
function (base, value) {
if (!base) return null;
if (!value) return base;
var toAdd = parseInt(value) / (1000 * 3600 * 24 * 7);
if (toAdd == 0) return base;
weekHelp.value = base;
if (toAdd > 0) weekHelp.stepUp(toAdd);
else weekHelp.stepDown(-toAdd);
return weekHelp.value;
} :
dateTimeAdder;
parsers[0] = function (x) { return x };
neutralParsers[0] = parsers[0];
adders[0] = function (x) { return x };
formatters[0] = parsers[0];
parsers[1] = parsers[2] = parsers[3] = function(x){return enhancer.parse("number", x);};
neutralParsers[1] = neutralParsers[2] = neutralParsers[3] = neutralNumberParser;
adders[1] = adders[2] = adders[3] = numberAdder;
formatters[1] = formatters[2] = formatters[3] = function(x){return enhancer.format("number", x);};
parsers[11] = parsers[12] = parsers[13] = function(x){return enhancer.parse("range", x);};
neutralParsers[11] = neutralParsers[12] = neutralParsers[13] = neutralNumberParser;
adders[11] = adders[12] = adders[13] = numberAdder;
formatters[11] = formatters[12] = formatters[13] = function(x){return enhancer.format("range", x);};
parsers[4] = function(x){return enhancer.parse("time", x);};
neutralParsers[4] = neutralTimeParser;
formatters[4] = function(x){return enhancer.format("time", x);};
parsers[5] = function(x){return enhancer.parse("date", x);};
neutralParsers[5] = neutralDateTimeParser;
formatters[5] = function(x){return enhancer.format("date", x);};
parsers[6] = function(x){return enhancer.parse("datetime", x);};
neutralParsers[6] = neutralDateTimeParser;
adders[4] = adders[5] = adders[6] = dateTimeAdder;
formatters[6] = function(x){return enhancer.format("datetime", x);};
parsers[7] = weekParser;
neutralParsers[7] = neutralWeekParser;
adders[7] = weekAdder;
formatters[7] = weekFormatter;
parsers[8] = function(x){return enhancer.parse("month", x);};
neutralParsers[8] = neutralMonthParser;
adders[8] = dateTimeAdder;
formatters[8] = function(x){return enhancer.format("month", x);};
initialized = true;
}
var $jQval = $.validator;
$.validator.methods.minE = function (value, element, param) {
if (!enhancer) initialize();
var type = getType($(element));
if (!value) return true;
var parser = parsers[type];
var go = param[1];
if (go && $(element).is(":focus")) return true;
var neutralParser = neutralParsers[type];
if (go) {
var val = parser(value);
var otherVal = neutralParser(param[0]);
if (val < otherVal) $(element).val(formatters[type](otherVal));
return true;
}
else return parser(value) >= neutralParser(param[0]);
}
$.validator.methods.maxE = function (value, element, param) {
if(!enhancer) initialize();
var type = getType($(element));
if (!value) return true;
var parser = parsers[type];
var go = param[1];
if (go && $(element).is(":focus")) return true;
var neutralParser = neutralParsers[type];
if (go) {
var val = parser(value);
var otherVal = neutralParser(param[0]);
if (val > otherVal) $(element).val(formatters[type](otherVal));
return true;
}
else return parser(value) <= neutralParser(param[0]);
}
$.validator.methods.rangeE = function (value, element, param) {
if(!enhancer) initialize();
var type = getType($(element));
if (!value) return true;
var parser = parsers[type];
var go = param[2];
if (go && $(element).is(":focus")) return true;
var neutralParser = neutralParsers[type];
if (go) {
var val = parser(value);
var otherVal = neutralParser(param[0]);
if (val < otherVal) $(element).val(formatters[type](otherVal));
else {
otherVal = neutralParser(param[1]);
if (val > otherVal) $(element).val(formatters[type](otherVal));
}
return true;
}
else return parser(value) <= neutralParser(param[1]) && parser(value) >= neutralParser(param[0]);
}
$.validator.methods.correcttype = function (value, element, param) {
if(!enhancer) initialize();
if (!value) return true;
var type = getType($(element));
var parser = parsers[type];
var val = parser(value);
return (val || val === 0)
&& (!(typeof val === "number") || (type < 10 ? numberRegEx.test(value) : rangeRegEx.test(value)))
&& ((type != 1 && type != 2 && type != 11 && type != 12) || (val % 1) === 0 )
&& ((type != 1 && type != 11) || val >= 0);
}
$.validator.methods.drange = function (value, element, param) {
if (!enhancer) initialize();
if (!value) return true;
var type = getType($(element));
var mins = param[0];
var minDelays = param[1];
var maxs = param[2];
var maxDelays = param[3];
var go = param[4];
if (go && $(element).is(":focus")) return true;
var parser = parsers[type];
var adder = adders[type];
var tvalue = parser(value);
var min, max, violated, vMAx, vMin;
for (var i = 0; i < mins.length; i++) {
var otherVal = mins[i].value;
if (!otherVal) continue;
otherVal = parser(otherVal);
if (!otherVal) continue;
otherVal = adder(otherVal, minDelays[i]);
if (go && !(otherVal <= min)) min = otherVal;
if (tvalue < otherVal) {
if (go) {
violated = true;
vMin = true;
}
else return false;
}
}
for (var i = 0; i < maxs.length; i++) {
var otherVal = maxs[i].value;
if (!otherVal) continue;
otherVal = parser(otherVal);
if (!otherVal) continue;
otherVal=adder(otherVal, maxDelays[i]);
if (go && !(otherVal >= max)) max = otherVal;
if (tvalue > otherVal) {
if (go) {
violated = true;
vMAx = true;
}
else return false;
}
}
if (go && violated) {
if (max < min ) return false;
if (vMAx) {
$(element).val(formatters[type](max))
}
else if (vMin) $(element).val(formatters[type](min))
}
return true;
}
var adapters = $jQval.unobtrusive.adapters;
var index = 0;
for (index = 0; index < adapters.length; index++) if (adapters[index].name == "range") break;
delete adapters[index];
function setValidationValues(options, ruleName, value) {
options.rules[ruleName] = value;
if (options.message) {
options.messages[ruleName] = options.message;
}
}
adapters.addMinMax = function (adapterName, minRuleName, maxRuleName, minMaxRuleName, minAttribute, maxAttribute) {
return this.add(adapterName, [minAttribute || "min", maxAttribute || "max", "go"], function (options) {
var min = options.params.min,
max = options.params.max,
go = options.params.go == "true";
if ((min || min === 0) && (max || max === 0)) {
setValidationValues(options, minMaxRuleName, [min, max, go]);
}
else if (min || min === 0) {
setValidationValues(options, minRuleName, [min, go]);
}
else if (max || max === 0) {
setValidationValues(options, maxRuleName, [max, go]);
}
});
};
adapters.addCorrecttype = function (adapterName, ruleName, paramName) {
return this.add(adapterName, [paramName || "type"], function (options) {
var type = options.params.type;
setValidationValues(options, ruleName, type);
});
};
adapters.addCorrecttype("correcttype", "correcttype");
adapters.addMinMax("range", "minE", "maxE", "rangeE");
if (enhancer) {
function addHandler(el, limits, preventKeyUp) {
if (!limits) return;
for (var i = 0; i < limits.length; i++) {
enhancer.dependency("main", limits[i], el, preventKeyUp? ["blur"] : ["blur", "keyup"], function (t, s) {
$(t).closest('form').validate().element(t);
});
}
}
adapters.addDRange = function (adapterName, ruleName, minsName, minDelaysName, maxsName, maxDelaysName) {
return this.add(adapterName, [minsName, minDelaysName, maxsName, maxDelaysName, "go"], function (options) {
var el = options.element;
var mins = otherElements(el, options.params[minsName]);
var maxs = otherElements(el, options.params[maxsName]);
addHandler(el, mins, options.params.go);
addHandler(el, maxs, options.params.go);
var mdelays = options.params[minDelaysName].split(" ");
var maxdelays = options.params[maxDelaysName].split(" ");
var go = options.params.go == "true";
setValidationValues(options, ruleName, [mins, mdelays, maxs, maxdelays, go]);
});
};
adapters.addDRange("drange", "drange", "dmins", "dminds", "dmaxs", "dmaxds");
enhancer["register"](null, false, initialize, "html5 globalized fallback", undefined, 'unobtrusive-validation');
}
})(jQuery);