PageSourceSearch

https://lonestarmilk.net/assets/js/jquery.validate.unobtrusive.js

js lonestarmilk.net collected 2026-10-05 17:42:32 UTC 14,745 bytes, 344 lines download raw bytes

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/*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 */
20/*global document: false, jQuery: false */
21(function ($) {
22var $jQval = $.validator,
vendor: 13,539 bytes, lines 23-344
23adapters,
24data_validation = "unobtrusiveValidation";
25function setValidationValues(options, ruleName, value) {
26options.rules[ruleName] = value;
27if (options.message) {
28options.messages[ruleName] = options.message;
29}
30}
31function splitAndTrim(value) {
32return value.replace(/^\s+|\s+$/g, "").split(/\s*,\s*/g);
33}
34function escapeAttributeValue(value) {
35// As mentioned on http://api.jquery.com/category/selectors/
36return value.replace(/([!"#$%&'()*+,./:;<=>?@\[\\\]^`{|}~])/g, "\\$1");
37}
38function getModelPrefix(fieldName) {
39return fieldName.substr(0, fieldName.lastIndexOf(".") + 1);
40}
41function appendModelPrefix(value, prefix) {
42if (value.indexOf("*.") === 0) {
43value = value.replace("*.", prefix);
44}
45return value;
46}
47function onError(error, inputElement) {  // 'this' is the form element
48var container = $(this).find("[data-valmsg-for='" + escapeAttributeValue(inputElement[0].name) + "']"),
49replaceAttrValue = container.attr("data-valmsg-replace"),
50replace = replaceAttrValue ? $.parseJSON(replaceAttrValue) !== false : null;
51container.removeClass("field-validation-valid").addClass("field-validation-error");
52error.data("unobtrusiveContainer", container);
53if (replace) {
54container.empty();
55error.removeClass("input-validation-error").appendTo(container);
56}
57else {
58error.hide();
59}
60}
61function onErrors(event, validator) {  // 'this' is the form element
62var container = $(this).find("[data-valmsg-summary=true]"),
63list = container.find("ul");
64if (list && list.length && validator.errorList.length) {
65list.empty();
66container.addClass("validation-summary-errors").removeClass("validation-summary-valid");
67$.each(validator.errorList, function () {
68$("<li />").html(this.message).appendTo(list);
69});
70}
71}
72function onSuccess(error) {  // 'this' is the form element
73var container = error.data("unobtrusiveContainer"),
74replaceAttrValue = container.attr("data-valmsg-replace"),
75replace = replaceAttrValue ? $.parseJSON(replaceAttrValue) : null;
76if (container) {
77container.addClass("field-validation-valid").removeClass("field-validation-error");
78error.removeData("unobtrusiveContainer");
79if (replace) {
80container.empty();
81}
82}
83}
84function onReset(event) {  // 'this' is the form element
85var $form = $(this);
86$form.data("validator").resetForm();
87$form.find(".validation-summary-errors")
88.addClass("validation-summary-valid")
89.removeClass("validation-summary-errors");
90$form.find(".field-validation-error")
91.addClass("field-validation-valid")
92.removeClass("field-validation-error")
93.removeData("unobtrusiveContainer")
94.find(">*")  // If we were using valmsg-replace, get the underlying error
95.removeData("unobtrusiveContainer");
96}
97function validationInfo(form) {
98var $form = $(form),
99result = $form.data(data_validation),
100onResetProxy = $.proxy(onReset, form);
101if (!result) {
102result = {
103options: {  // options structure passed to jQuery Validate's validate() method
104errorClass: "input-validation-error",
105errorElement: "span",
106errorPlacement: $.proxy(onError, form),
107invalidHandler: $.proxy(onErrors, form),
108messages: {},
109rules: {},
110success: $.proxy(onSuccess, form)
111},
112attachValidation: function () {
113$form
114.unbind("reset." + data_validation, onResetProxy)
115.bind("reset." + data_validation, onResetProxy)
116.validate(this.options);
117},
118validate: function () {  // a validation function that is called by unobtrusive Ajax
119$form.validate();
120return $form.valid();
121}
122};
123$form.data(data_validation, result);
124}
125return result;
126}
127$jQval.unobtrusive = {
128adapters: [],
129parseElement: function (element, skipAttach) {
130/// <summary>
131/// Parses a single HTML element for unobtrusive validation attributes.
132/// </summary>
133/// <param name="element" domElement="true">The HTML element to be parsed.</param>
134/// <param name="skipAttach" type="Boolean">[Optional] true to skip attaching the
135/// validation to the form. If parsing just this single element, you should specify true.
136/// If parsing several elements, you should specify false, and manually attach the validation
137/// to the form when you are finished. The default is false.</param>
138var $element = $(element),
139form = $element.parents("form")[0],
140valInfo, rules, messages;
141if (!form) {  // Cannot do client-side validation without a form
142return;
143}
144valInfo = validationInfo(form);
145valInfo.options.rules[element.name] = rules = {};
146valInfo.options.messages[element.name] = messages = {};
147$.each(this.adapters, function () {
148var prefix = "data-val-" + this.name,
149message = $element.attr(prefix),
150paramValues = {};
151if (message !== undefined) {  // Compare against undefined, because an empty message is legal (and falsy)
152prefix += "-";
153$.each(this.params, function () {
154paramValues[this] = $element.attr(prefix + this);
155});
156this.adapt({
157element: element,
158form: form,
159message: message,
160params: paramValues,
161rules: rules,
162messages: messages
163});
164}
165});
166$.extend(rules, { "__dummy__": true });
167if (!skipAttach) {
168valInfo.attachValidation();
169}
170},
171parse: function (selector) {
172/// <summary>
173/// Parses all the HTML elements in the specified selector. It looks for input elements decorated
174/// with the [data-val=true] attribute value and enables validation according to the data-val-*
175/// attribute values.
176/// </summary>
177/// <param name="selector" type="String">Any valid jQuery selector.</param>
178var $forms = $(selector)
179.parents("form")
180.andSelf()
181.add($(selector).find("form"))
182.filter("form");
183// :input is a psuedoselector provided by jQuery which selects input and input-like elements
184// combining :input with other selectors significantly decreases performance.
185$(selector).find(":input").filter("[data-val=true]").each(function () {
186$jQval.unobtrusive.parseElement(this, true);
187});
188$forms.each(function () {
189var info = validationInfo(this);
190if (info) {
191info.attachValidation();
192}
193});
194}
195};
196adapters = $jQval.unobtrusive.adapters;
197adapters.add = function (adapterName, params, fn) {
198/// <summary>Adds a new adapter to convert unobtrusive HTML into a jQuery Validate validation.</summary>
199/// <param name="adapterName" type="String">The name of the adapter to be added. This matches the name used
200/// in the data-val-nnnn HTML attribute (where nnnn is the adapter name).</param>
201/// <param name="params" type="Array" optional="true">[Optional] An array of parameter names (strings) that will
202/// be extracted from the data-val-nnnn-mmmm HTML attributes (where nnnn is the adapter name, and
203/// mmmm is the parameter name).</param>
204/// <param name="fn" type="Function">The function to call, which adapts the values from the HTML
205/// attributes into jQuery Validate rules and/or messages.</param>
206/// <returns type="jQuery.validator.unobtrusive.adapters" />
207if (!fn) {  // Called with no params, just a function
208fn = params;
209params = [];
210}
211this.push({ name: adapterName, params: params, adapt: fn });
212return this;
213};
214adapters.addBool = function (adapterName, ruleName) {
215/// <summary>Adds a new adapter to convert unobtrusive HTML into a jQuery Validate validation, where
216/// the jQuery Validate validation rule has no parameter values.</summary>
217/// <param name="adapterName" type="String">The name of the adapter to be added. This matches the name used
218/// in the data-val-nnnn HTML attribute (where nnnn is the adapter name).</param>
219/// <param name="ruleName" type="String" optional="true">[Optional] The name of the jQuery Validate rule. If not provided, the value
220/// of adapterName will be used instead.</param>
221/// <returns type="jQuery.validator.unobtrusive.adapters" />
222return this.add(adapterName, function (options) {
223setValidationValues(options, ruleName || adapterName, true);
224});
225};
226adapters.addMinMax = function (adapterName, minRuleName, maxRuleName, minMaxRuleName, minAttribute, maxAttribute) {
227/// <summary>Adds a new adapter to convert unobtrusive HTML into a jQuery Validate validation, where
228/// the jQuery Validate validation has three potential rules (one for min-only, one for max-only, and
229/// one for min-and-max). The HTML parameters are expected to be named -min and -max.</summary>
230/// <param name="adapterName" type="String">The name of the adapter to be added. This matches the name used
231/// in the data-val-nnnn HTML attribute (where nnnn is the adapter name).</param>
232/// <param name="minRuleName" type="String">The name of the jQuery Validate rule to be used when you only
233/// have a minimum value.</param>
234/// <param name="maxRuleName" type="String">The name of the jQuery Validate rule to be used when you only
235/// have a maximum value.</param>
236/// <param name="minMaxRuleName" type="String">The name of the jQuery Validate rule to be used when you
237/// have both a minimum and maximum value.</param>
238/// <param name="minAttribute" type="String" optional="true">[Optional] The name of the HTML attribute that
239/// contains the minimum value. The default is "min".</param>
240/// <param name="maxAttribute" type="String" optional="true">[Optional] The name of the HTML attribute that
241/// contains the maximum value. The default is "max".</param>
242/// <returns type="jQuery.validator.unobtrusive.adapters" />
243return this.add(adapterName, [minAttribute || "min", maxAttribute || "max"], function (options) {
244var min = options.params.min,
245max = options.params.max;
246if (min && max) {
247setValidationValues(options, minMaxRuleName, [min, max]);
248}
249else if (min) {
250setValidationValues(options, minRuleName, min);
251}
252else if (max) {
253setValidationValues(options, maxRuleName, max);
254}
255});
256};
257adapters.addSingleVal = function (adapterName, attribute, ruleName) {
258/// <summary>Adds a new adapter to convert unobtrusive HTML into a jQuery Validate validation, where
259/// the jQuery Validate validation rule has a single value.</summary>
260/// <param name="adapterName" type="String">The name of the adapter to be added. This matches the name used
261/// in the data-val-nnnn HTML attribute(where nnnn is the adapter name).</param>
262/// <param name="attribute" type="String">[Optional] The name of the HTML attribute that contains the value.
263/// The default is "val".</param>
264/// <param name="ruleName" type="String" optional="true">[Optional] The name of the jQuery Validate rule. If not provided, the value
265/// of adapterName will be used instead.</param>
266/// <returns type="jQuery.validator.unobtrusive.adapters" />
267return this.add(adapterName, [attribute || "val"], function (options) {
268setValidationValues(options, ruleName || adapterName, options.params[attribute]);
269});
270};
271$jQval.addMethod("__dummy__", function (value, element, params) {
272return true;
273});
274$jQval.addMethod("regex", function (value, element, params) {
275var match;
276if (this.optional(element)) {
277return true;
278}
279match = new RegExp(params).exec(value);
280return (match && (match.index === 0) && (match[0].length === value.length));
281});
282$jQval.addMethod("nonalphamin", function (value, element, nonalphamin) {
283var match;
284if (nonalphamin) {
285match = value.match(/\W/g);
286match = match && match.length >= nonalphamin;
287}
288return match;
289});
290if ($jQval.methods.extension) {
291adapters.addSingleVal("accept", "mimtype");
292adapters.addSingleVal("extension", "extension");
293} else {
294// for backward compatibility, when the 'extension' validation method does not exist, such as with versions
295// of JQuery Validation plugin prior to 1.10, we should use the 'accept' method for
296// validating the extension, and ignore mime-type validations as they are not supported.
297adapters.addSingleVal("extension", "extension", "accept");
298}
299adapters.addSingleVal("regex", "pattern");
300adapters.addBool("creditcard").addBool("date").addBool("digits").addBool("email").addBool("number").addBool("url");
301adapters.addMinMax("length", "minlength", "maxlength", "rangelength").addMinMax("range", "min", "max", "range");
302adapters.add("equalto", ["other"], function (options) {
303var prefix = getModelPrefix(options.element.name),
304other = options.params.other,
305fullOtherName = appendModelPrefix(other, prefix),
306element = $(options.form).find(":input").filter("[name='" + escapeAttributeValue(fullOtherName) + "']")[0];
307setValidationValues(options, "equalTo", element);
308});
309adapters.add("required", function (options) {
310// jQuery Validate equates "required" with "mandatory" for checkbox elements
311if (options.element.tagName.toUpperCase() !== "INPUT" || options.element.type.toUpperCase() !== "CHECKBOX") {
312setValidationValues(options, "required", true);
313}
314});
315adapters.add("remote", ["url", "type", "additionalfields"], function (options) {
316var value = {
317url: options.params.url,
318type: options.params.type || "GET",
319data: {}
320},
321prefix = getModelPrefix(options.element.name);
322$.each(splitAndTrim(options.params.additionalfields || options.element.name), function (i, fieldName) {
323var paramName = appendModelPrefix(fieldName, prefix);
324value.data[paramName] = function () {
325return $(options.form).find(":input").filter("[name='" + escapeAttributeValue(paramName) + "']").val();
326};
327});
328setValidationValues(options, "remote", value);
329});
330adapters.add("password", ["min", "nonalphamin", "regex"], function (options) {
331if (options.params.min) {
332setValidationValues(options, "minlength", options.params.min);
333}
334if (options.params.nonalphamin) {
335setValidationValues(options, "nonalphamin", options.params.nonalphamin);
336}
337if (options.params.regex) {
338setValidationValues(options, "regex", options.params.regex);
339}
340});
341$(function () {
342$jQval.unobtrusive.parse(document);
343});
344}(jQuery));

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.