PageSourceSearch

https://www.theatertoolkit.com/dist/js/jquery.validate.unobtrusive.js

js theatertoolkit.com collected 2026-10-08 07:25:23 UTC 18,048 bytes, 384 lines download raw bytes

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

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.