1 | /* NUGET: BEGIN LICENSE TEXT
|
---|
2 | *
|
---|
3 | * Microsoft grants you the right to use these script files for the sole
|
---|
4 | * purpose of either: (i) interacting through your browser with the Microsoft
|
---|
5 | * website or online service, subject to the applicable licensing or use
|
---|
6 | * terms; or (ii) using the files as included with a Microsoft product subject
|
---|
7 | * to that product's license terms. Microsoft reserves all other rights to the
|
---|
8 | * files not expressly granted by Microsoft, whether by implication, estoppel
|
---|
9 | * or otherwise. Insofar as a script file is dual licensed under GPL,
|
---|
10 | * Microsoft neither took the code under GPL nor distributes it thereunder but
|
---|
11 | * under the terms set out in this paragraph. All notices and licenses
|
---|
12 | * below are for informational purposes only.
|
---|
13 | *
|
---|
14 | * NUGET: END LICENSE TEXT */
|
---|
15 | /*!
|
---|
16 | ** Unobtrusive validation support library for jQuery and jQuery Validate
|
---|
17 | ** Copyright (C) Microsoft Corporation. All rights reserved.
|
---|
18 | */
|
---|
19 |
|
---|
20 | /*jslint white: true, browser: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, newcap: true, immed: true, strict: false */
|
---|
21 | /*global document: false, jQuery: false */
|
---|
22 |
|
---|
23 | (function ($) {
|
---|
24 | var $jQval = $.validator,
|
---|
25 | adapters,
|
---|
26 | data_validation = "unobtrusiveValidation";
|
---|
27 |
|
---|
28 | function setValidationValues(options, ruleName, value) {
|
---|
29 | options.rules[ruleName] = value;
|
---|
30 | if (options.message) {
|
---|
31 | options.messages[ruleName] = options.message;
|
---|
32 | }
|
---|
33 | }
|
---|
34 |
|
---|
35 | function splitAndTrim(value) {
|
---|
36 | return value.replace(/^\s+|\s+$/g, "").split(/\s*,\s*/g);
|
---|
37 | }
|
---|
38 |
|
---|
39 | function escapeAttributeValue(value) {
|
---|
40 | // As mentioned on http://api.jquery.com/category/selectors/
|
---|
41 | return value.replace(/([!"#$%&'()*+,./:;<=>?@\[\\\]^`{|}~])/g, "\\$1");
|
---|
42 | }
|
---|
43 |
|
---|
44 | function getModelPrefix(fieldName) {
|
---|
45 | return fieldName.substr(0, fieldName.lastIndexOf(".") + 1);
|
---|
46 | }
|
---|
47 |
|
---|
48 | function appendModelPrefix(value, prefix) {
|
---|
49 | if (value.indexOf("*.") === 0) {
|
---|
50 | value = value.replace("*.", prefix);
|
---|
51 | }
|
---|
52 | return value;
|
---|
53 | }
|
---|
54 |
|
---|
55 | function onError(error, inputElement) { // 'this' is the form element
|
---|
56 | var container = $(this).find("[data-valmsg-for='" + escapeAttributeValue(inputElement[0].name) + "']"),
|
---|
57 | replaceAttrValue = container.attr("data-valmsg-replace"),
|
---|
58 | replace = replaceAttrValue ? $.parseJSON(replaceAttrValue) !== false : null;
|
---|
59 |
|
---|
60 | container.removeClass("field-validation-valid").addClass("field-validation-error");
|
---|
61 | error.data("unobtrusiveContainer", container);
|
---|
62 |
|
---|
63 | if (replace) {
|
---|
64 | container.empty();
|
---|
65 | error.removeClass("input-validation-error").appendTo(container);
|
---|
66 | }
|
---|
67 | else {
|
---|
68 | error.hide();
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | function onErrors(event, validator) { // 'this' is the form element
|
---|
73 | var container = $(this).find("[data-valmsg-summary=true]"),
|
---|
74 | list = container.find("ul");
|
---|
75 |
|
---|
76 | if (list && list.length && validator.errorList.length) {
|
---|
77 | list.empty();
|
---|
78 | container.addClass("validation-summary-errors").removeClass("validation-summary-valid");
|
---|
79 |
|
---|
80 | $.each(validator.errorList, function () {
|
---|
81 | $("<li />").html(this.message).appendTo(list);
|
---|
82 | });
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | function onSuccess(error) { // 'this' is the form element
|
---|
87 | var container = error.data("unobtrusiveContainer"),
|
---|
88 | replaceAttrValue = container.attr("data-valmsg-replace"),
|
---|
89 | replace = replaceAttrValue ? $.parseJSON(replaceAttrValue) : null;
|
---|
90 |
|
---|
91 | if (container) {
|
---|
92 | container.addClass("field-validation-valid").removeClass("field-validation-error");
|
---|
93 | error.removeData("unobtrusiveContainer");
|
---|
94 |
|
---|
95 | if (replace) {
|
---|
96 | container.empty();
|
---|
97 | }
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | function onReset(event) { // 'this' is the form element
|
---|
102 | var $form = $(this);
|
---|
103 | $form.data("validator").resetForm();
|
---|
104 | $form.find(".validation-summary-errors")
|
---|
105 | .addClass("validation-summary-valid")
|
---|
106 | .removeClass("validation-summary-errors");
|
---|
107 | $form.find(".field-validation-error")
|
---|
108 | .addClass("field-validation-valid")
|
---|
109 | .removeClass("field-validation-error")
|
---|
110 | .removeData("unobtrusiveContainer")
|
---|
111 | .find(">*") // If we were using valmsg-replace, get the underlying error
|
---|
112 | .removeData("unobtrusiveContainer");
|
---|
113 | }
|
---|
114 |
|
---|
115 | function validationInfo(form) {
|
---|
116 | var $form = $(form),
|
---|
117 | result = $form.data(data_validation),
|
---|
118 | onResetProxy = $.proxy(onReset, form),
|
---|
119 | defaultOptions = $jQval.unobtrusive.options || {},
|
---|
120 | execInContext = function (name, args) {
|
---|
121 | var func = defaultOptions[name];
|
---|
122 | func && $.isFunction(func) && func.apply(form, args);
|
---|
123 | }
|
---|
124 |
|
---|
125 | if (!result) {
|
---|
126 | result = {
|
---|
127 | options: { // options structure passed to jQuery Validate's validate() method
|
---|
128 | errorClass: defaultOptions.errorClass || "input-validation-error",
|
---|
129 | errorElement: defaultOptions.errorElement || "span",
|
---|
130 | errorPlacement: function () {
|
---|
131 | onError.apply(form, arguments);
|
---|
132 | execInContext("errorPlacement", arguments);
|
---|
133 | },
|
---|
134 | invalidHandler: function () {
|
---|
135 | onErrors.apply(form, arguments);
|
---|
136 | execInContext("invalidHandler", arguments);
|
---|
137 | },
|
---|
138 | messages: {},
|
---|
139 | rules: {},
|
---|
140 | success: function () {
|
---|
141 | onSuccess.apply(form, arguments);
|
---|
142 | execInContext("success", arguments);
|
---|
143 | }
|
---|
144 | },
|
---|
145 | attachValidation: function () {
|
---|
146 | $form
|
---|
147 | .off("reset." + data_validation, onResetProxy)
|
---|
148 | .on("reset." + data_validation, onResetProxy)
|
---|
149 | .validate(this.options);
|
---|
150 | },
|
---|
151 | validate: function () { // a validation function that is called by unobtrusive Ajax
|
---|
152 | $form.validate();
|
---|
153 | return $form.valid();
|
---|
154 | }
|
---|
155 | };
|
---|
156 | $form.data(data_validation, result);
|
---|
157 | }
|
---|
158 |
|
---|
159 | return result;
|
---|
160 | }
|
---|
161 |
|
---|
162 | $jQval.unobtrusive = {
|
---|
163 | adapters: [],
|
---|
164 |
|
---|
165 | parseElement: function (element, skipAttach) {
|
---|
166 | /// <summary>
|
---|
167 | /// Parses a single HTML element for unobtrusive validation attributes.
|
---|
168 | /// </summary>
|
---|
169 | /// <param name="element" domElement="true">The HTML element to be parsed.</param>
|
---|
170 | /// <param name="skipAttach" type="Boolean">[Optional] true to skip attaching the
|
---|
171 | /// validation to the form. If parsing just this single element, you should specify true.
|
---|
172 | /// If parsing several elements, you should specify false, and manually attach the validation
|
---|
173 | /// to the form when you are finished. The default is false.</param>
|
---|
174 | var $element = $(element),
|
---|
175 | form = $element.parents("form")[0],
|
---|
176 | valInfo, rules, messages;
|
---|
177 |
|
---|
178 | if (!form) { // Cannot do client-side validation without a form
|
---|
179 | return;
|
---|
180 | }
|
---|
181 |
|
---|
182 | valInfo = validationInfo(form);
|
---|
183 | valInfo.options.rules[element.name] = rules = {};
|
---|
184 | valInfo.options.messages[element.name] = messages = {};
|
---|
185 |
|
---|
186 | $.each(this.adapters, function () {
|
---|
187 | var prefix = "data-val-" + this.name,
|
---|
188 | message = $element.attr(prefix),
|
---|
189 | paramValues = {};
|
---|
190 |
|
---|
191 | if (message !== undefined) { // Compare against undefined, because an empty message is legal (and falsy)
|
---|
192 | prefix += "-";
|
---|
193 |
|
---|
194 | $.each(this.params, function () {
|
---|
195 | paramValues[this] = $element.attr(prefix + this);
|
---|
196 | });
|
---|
197 |
|
---|
198 | this.adapt({
|
---|
199 | element: element,
|
---|
200 | form: form,
|
---|
201 | message: message,
|
---|
202 | params: paramValues,
|
---|
203 | rules: rules,
|
---|
204 | messages: messages
|
---|
205 | });
|
---|
206 | }
|
---|
207 | });
|
---|
208 |
|
---|
209 | $.extend(rules, { "__dummy__": true });
|
---|
210 |
|
---|
211 | if (!skipAttach) {
|
---|
212 | valInfo.attachValidation();
|
---|
213 | }
|
---|
214 | },
|
---|
215 |
|
---|
216 | parse: function (selector) {
|
---|
217 | /// <summary>
|
---|
218 | /// Parses all the HTML elements in the specified selector. It looks for input elements decorated
|
---|
219 | /// with the [data-val=true] attribute value and enables validation according to the data-val-*
|
---|
220 | /// attribute values.
|
---|
221 | /// </summary>
|
---|
222 | /// <param name="selector" type="String">Any valid jQuery selector.</param>
|
---|
223 |
|
---|
224 | // $forms includes all forms in selector's DOM hierarchy (parent, children and self) that have at least one
|
---|
225 | // element with data-val=true
|
---|
226 | var $selector = $(selector),
|
---|
227 | $forms = $selector.parents()
|
---|
228 | .addBack()
|
---|
229 | .filter("form")
|
---|
230 | .add($selector.find("form"))
|
---|
231 | .has("[data-val=true]");
|
---|
232 |
|
---|
233 | $selector.find("[data-val=true]").each(function () {
|
---|
234 | $jQval.unobtrusive.parseElement(this, true);
|
---|
235 | });
|
---|
236 |
|
---|
237 | $forms.each(function () {
|
---|
238 | var info = validationInfo(this);
|
---|
239 | if (info) {
|
---|
240 | info.attachValidation();
|
---|
241 | }
|
---|
242 | });
|
---|
243 | }
|
---|
244 | };
|
---|
245 |
|
---|
246 | adapters = $jQval.unobtrusive.adapters;
|
---|
247 |
|
---|
248 | adapters.add = function (adapterName, params, fn) {
|
---|
249 | /// <summary>Adds a new adapter to convert unobtrusive HTML into a jQuery Validate validation.</summary>
|
---|
250 | /// <param name="adapterName" type="String">The name of the adapter to be added. This matches the name used
|
---|
251 | /// in the data-val-nnnn HTML attribute (where nnnn is the adapter name).</param>
|
---|
252 | /// <param name="params" type="Array" optional="true">[Optional] An array of parameter names (strings) that will
|
---|
253 | /// be extracted from the data-val-nnnn-mmmm HTML attributes (where nnnn is the adapter name, and
|
---|
254 | /// mmmm is the parameter name).</param>
|
---|
255 | /// <param name="fn" type="Function">The function to call, which adapts the values from the HTML
|
---|
256 | /// attributes into jQuery Validate rules and/or messages.</param>
|
---|
257 | /// <returns type="jQuery.validator.unobtrusive.adapters" />
|
---|
258 | if (!fn) { // Called with no params, just a function
|
---|
259 | fn = params;
|
---|
260 | params = [];
|
---|
261 | }
|
---|
262 | this.push({ name: adapterName, params: params, adapt: fn });
|
---|
263 | return this;
|
---|
264 | };
|
---|
265 |
|
---|
266 | adapters.addBool = function (adapterName, ruleName) {
|
---|
267 | /// <summary>Adds a new adapter to convert unobtrusive HTML into a jQuery Validate validation, where
|
---|
268 | /// the jQuery Validate validation rule has no parameter values.</summary>
|
---|
269 | /// <param name="adapterName" type="String">The name of the adapter to be added. This matches the name used
|
---|
270 | /// in the data-val-nnnn HTML attribute (where nnnn is the adapter name).</param>
|
---|
271 | /// <param name="ruleName" type="String" optional="true">[Optional] The name of the jQuery Validate rule. If not provided, the value
|
---|
272 | /// of adapterName will be used instead.</param>
|
---|
273 | /// <returns type="jQuery.validator.unobtrusive.adapters" />
|
---|
274 | return this.add(adapterName, function (options) {
|
---|
275 | setValidationValues(options, ruleName || adapterName, true);
|
---|
276 | });
|
---|
277 | };
|
---|
278 |
|
---|
279 | adapters.addMinMax = function (adapterName, minRuleName, maxRuleName, minMaxRuleName, minAttribute, maxAttribute) {
|
---|
280 | /// <summary>Adds a new adapter to convert unobtrusive HTML into a jQuery Validate validation, where
|
---|
281 | /// the jQuery Validate validation has three potential rules (one for min-only, one for max-only, and
|
---|
282 | /// one for min-and-max). The HTML parameters are expected to be named -min and -max.</summary>
|
---|
283 | /// <param name="adapterName" type="String">The name of the adapter to be added. This matches the name used
|
---|
284 | /// in the data-val-nnnn HTML attribute (where nnnn is the adapter name).</param>
|
---|
285 | /// <param name="minRuleName" type="String">The name of the jQuery Validate rule to be used when you only
|
---|
286 | /// have a minimum value.</param>
|
---|
287 | /// <param name="maxRuleName" type="String">The name of the jQuery Validate rule to be used when you only
|
---|
288 | /// have a maximum value.</param>
|
---|
289 | /// <param name="minMaxRuleName" type="String">The name of the jQuery Validate rule to be used when you
|
---|
290 | /// have both a minimum and maximum value.</param>
|
---|
291 | /// <param name="minAttribute" type="String" optional="true">[Optional] The name of the HTML attribute that
|
---|
292 | /// contains the minimum value. The default is "min".</param>
|
---|
293 | /// <param name="maxAttribute" type="String" optional="true">[Optional] The name of the HTML attribute that
|
---|
294 | /// contains the maximum value. The default is "max".</param>
|
---|
295 | /// <returns type="jQuery.validator.unobtrusive.adapters" />
|
---|
296 | return this.add(adapterName, [minAttribute || "min", maxAttribute || "max"], function (options) {
|
---|
297 | var min = options.params.min,
|
---|
298 | max = options.params.max;
|
---|
299 |
|
---|
300 | if (min && max) {
|
---|
301 | setValidationValues(options, minMaxRuleName, [min, max]);
|
---|
302 | }
|
---|
303 | else if (min) {
|
---|
304 | setValidationValues(options, minRuleName, min);
|
---|
305 | }
|
---|
306 | else if (max) {
|
---|
307 | setValidationValues(options, maxRuleName, max);
|
---|
308 | }
|
---|
309 | });
|
---|
310 | };
|
---|
311 |
|
---|
312 | adapters.addSingleVal = function (adapterName, attribute, ruleName) {
|
---|
313 | /// <summary>Adds a new adapter to convert unobtrusive HTML into a jQuery Validate validation, where
|
---|
314 | /// the jQuery Validate validation rule has a single value.</summary>
|
---|
315 | /// <param name="adapterName" type="String">The name of the adapter to be added. This matches the name used
|
---|
316 | /// in the data-val-nnnn HTML attribute(where nnnn is the adapter name).</param>
|
---|
317 | /// <param name="attribute" type="String">[Optional] The name of the HTML attribute that contains the value.
|
---|
318 | /// The default is "val".</param>
|
---|
319 | /// <param name="ruleName" type="String" optional="true">[Optional] The name of the jQuery Validate rule. If not provided, the value
|
---|
320 | /// of adapterName will be used instead.</param>
|
---|
321 | /// <returns type="jQuery.validator.unobtrusive.adapters" />
|
---|
322 | return this.add(adapterName, [attribute || "val"], function (options) {
|
---|
323 | setValidationValues(options, ruleName || adapterName, options.params[attribute]);
|
---|
324 | });
|
---|
325 | };
|
---|
326 |
|
---|
327 | $jQval.addMethod("__dummy__", function (value, element, params) {
|
---|
328 | return true;
|
---|
329 | });
|
---|
330 |
|
---|
331 | $jQval.addMethod("regex", function (value, element, params) {
|
---|
332 | var match;
|
---|
333 | if (this.optional(element)) {
|
---|
334 | return true;
|
---|
335 | }
|
---|
336 |
|
---|
337 | match = new RegExp(params).exec(value);
|
---|
338 | return (match && (match.index === 0) && (match[0].length === value.length));
|
---|
339 | });
|
---|
340 |
|
---|
341 | $jQval.addMethod("nonalphamin", function (value, element, nonalphamin) {
|
---|
342 | var match;
|
---|
343 | if (nonalphamin) {
|
---|
344 | match = value.match(/\W/g);
|
---|
345 | match = match && match.length >= nonalphamin;
|
---|
346 | }
|
---|
347 | return match;
|
---|
348 | });
|
---|
349 |
|
---|
350 | if ($jQval.methods.extension) {
|
---|
351 | adapters.addSingleVal("accept", "mimtype");
|
---|
352 | adapters.addSingleVal("extension", "extension");
|
---|
353 | } else {
|
---|
354 | // for backward compatibility, when the 'extension' validation method does not exist, such as with versions
|
---|
355 | // of JQuery Validation plugin prior to 1.10, we should use the 'accept' method for
|
---|
356 | // validating the extension, and ignore mime-type validations as they are not supported.
|
---|
357 | adapters.addSingleVal("extension", "extension", "accept");
|
---|
358 | }
|
---|
359 |
|
---|
360 | adapters.addSingleVal("regex", "pattern");
|
---|
361 | adapters.addBool("creditcard").addBool("date").addBool("digits").addBool("email").addBool("number").addBool("url");
|
---|
362 | adapters.addMinMax("length", "minlength", "maxlength", "rangelength").addMinMax("range", "min", "max", "range");
|
---|
363 | adapters.addMinMax("minlength", "minlength").addMinMax("maxlength", "minlength", "maxlength");
|
---|
364 | adapters.add("equalto", ["other"], function (options) {
|
---|
365 | var prefix = getModelPrefix(options.element.name),
|
---|
366 | other = options.params.other,
|
---|
367 | fullOtherName = appendModelPrefix(other, prefix),
|
---|
368 | element = $(options.form).find(":input").filter("[name='" + escapeAttributeValue(fullOtherName) + "']")[0];
|
---|
369 |
|
---|
370 | setValidationValues(options, "equalTo", element);
|
---|
371 | });
|
---|
372 | adapters.add("required", function (options) {
|
---|
373 | // jQuery Validate equates "required" with "mandatory" for checkbox elements
|
---|
374 | if (options.element.tagName.toUpperCase() !== "INPUT" || options.element.type.toUpperCase() !== "CHECKBOX") {
|
---|
375 | setValidationValues(options, "required", true);
|
---|
376 | }
|
---|
377 | });
|
---|
378 | adapters.add("remote", ["url", "type", "additionalfields"], function (options) {
|
---|
379 | var value = {
|
---|
380 | url: options.params.url,
|
---|
381 | type: options.params.type || "GET",
|
---|
382 | data: {}
|
---|
383 | },
|
---|
384 | prefix = getModelPrefix(options.element.name);
|
---|
385 |
|
---|
386 | $.each(splitAndTrim(options.params.additionalfields || options.element.name), function (i, fieldName) {
|
---|
387 | var paramName = appendModelPrefix(fieldName, prefix);
|
---|
388 | value.data[paramName] = function () {
|
---|
389 | return $(options.form).find(":input").filter("[name='" + escapeAttributeValue(paramName) + "']").val();
|
---|
390 | };
|
---|
391 | });
|
---|
392 |
|
---|
393 | setValidationValues(options, "remote", value);
|
---|
394 | });
|
---|
395 | adapters.add("password", ["min", "nonalphamin", "regex"], function (options) {
|
---|
396 | if (options.params.min) {
|
---|
397 | setValidationValues(options, "minlength", options.params.min);
|
---|
398 | }
|
---|
399 | if (options.params.nonalphamin) {
|
---|
400 | setValidationValues(options, "nonalphamin", options.params.nonalphamin);
|
---|
401 | }
|
---|
402 | if (options.params.regex) {
|
---|
403 | setValidationValues(options, "regex", options.params.regex);
|
---|
404 | }
|
---|
405 | });
|
---|
406 |
|
---|
407 | $(function () {
|
---|
408 | $jQval.unobtrusive.parse(document);
|
---|
409 | });
|
---|
410 | }(jQuery)); |
---|