/* nuget: begin license text * * microsoft grants you the right to use these script files for the sole * purpose of either: (i) interacting through your browser with the microsoft * website or online service, subject to the applicable licensing or use * terms; or (ii) using the files as included with a microsoft product subject * to that product's license terms. microsoft reserves all other rights to the * files not expressly granted by microsoft, whether by implication, estoppel * or otherwise. insofar as a script file is dual licensed under gpl, * microsoft neither took the code under gpl nor distributes it thereunder but * under the terms set out in this paragraph. all notices and licenses * below are for informational purposes only. * * nuget: end license text */ /*! ** unobtrusive validation support library for jquery and jquery validate ** copyright (c) microsoft corporation. all rights reserved. */ /*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 */ /*global document: false, jquery: false */ (function ($) { var $jqval = $.validator, adapters, data_validation = "unobtrusivevalidation"; function setvalidationvalues(options, rulename, value) { options.rules[rulename] = value; if (options.message) { options.messages[rulename] = options.message; } } function splitandtrim(value) { return value.replace(/^\s+|\s+$/g, "").split(/\s*,\s*/g); } function escapeattributevalue(value) { // as mentioned on http://api.jquery.com/category/selectors/ return value.replace(/([!"#$%&'()*+,./:;<=>?@\[\\\]^`{|}~])/g, "\\$1"); } function getmodelprefix(fieldname) { return fieldname.substr(0, fieldname.lastindexof(".") + 1); } function appendmodelprefix(value, prefix) { if (value.indexof("*.") === 0) { value = value.replace("*.", prefix); } return value; } function onerror(error, inputelement) { // 'this' is the form element var container = $(this).find("[data-valmsg-for='" + escapeattributevalue(inputelement[0].name) + "']"), replaceattrvalue = container.attr("data-valmsg-replace"), replace = replaceattrvalue ? $.parsejson(replaceattrvalue) !== false : null; container.removeclass("field-validation-valid").addclass("field-validation-error"); error.data("unobtrusivecontainer", container); if (replace) { container.empty(); error.removeclass("input-validation-error").appendto(container); } else { error.hide(); } } function onerrors(event, validator) { // 'this' is the form element var container = $(this).find("[data-valmsg-summary=true]"), list = container.find("ul"); if (list && list.length && validator.errorlist.length) { list.empty(); container.addclass("validation-summary-errors").removeclass("validation-summary-valid"); $.each(validator.errorlist, function () { $("
  • ").html(this.message).appendto(list); }); } } function onsuccess(error) { // 'this' is the form element var container = error.data("unobtrusivecontainer"), replaceattrvalue = container.attr("data-valmsg-replace"), replace = replaceattrvalue ? $.parsejson(replaceattrvalue) : null; if (container) { container.addclass("field-validation-valid").removeclass("field-validation-error"); error.removedata("unobtrusivecontainer"); if (replace) { container.empty(); } } } function onreset(event) { // 'this' is the form element var $form = $(this); $form.data("validator").resetform(); $form.find(".validation-summary-errors") .addclass("validation-summary-valid") .removeclass("validation-summary-errors"); $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 .removedata("unobtrusivecontainer"); } function validationinfo(form) { var $form = $(form), result = $form.data(data_validation), onresetproxy = $.proxy(onreset, form); if (!result) { result = { options: { // options structure passed to jquery validate's validate() method errorclass: "input-validation-error", errorelement: "span", errorplacement: $.proxy(onerror, form), invalidhandler: $.proxy(onerrors, form), messages: {}, rules: {}, success: $.proxy(onsuccess, form) }, attachvalidation: function () { $form .unbind("reset." + data_validation, onresetproxy) .bind("reset." + data_validation, onresetproxy) .validate(this.options); }, validate: function () { // a validation function that is called by unobtrusive ajax $form.validate(); return $form.valid(); } }; $form.data(data_validation, result); } return result; } $jqval.unobtrusive = { adapters: [], parseelement: function (element, skipattach) { /// /// parses a single html element for unobtrusive validation attributes. /// /// the html element to be parsed. /// [optional] true to skip attaching the /// validation to the form. if parsing just this single element, you should specify true. /// if parsing several elements, you should specify false, and manually attach the validation /// to the form when you are finished. the default is false. var $element = $(element), form = $element.parents("form")[0], valinfo, rules, messages; if (!form) { // cannot do client-side validation without a form return; } valinfo = validationinfo(form); valinfo.options.rules[element.name] = rules = {}; valinfo.options.messages[element.name] = messages = {}; $.each(this.adapters, function () { var prefix = "data-val-" + this.name, message = $element.attr(prefix), paramvalues = {}; if (message !== undefined) { // compare against undefined, because an empty message is legal (and falsy) prefix += "-"; $.each(this.params, function () { paramvalues[this] = $element.attr(prefix + this); }); this.adapt({ element: element, form: form, message: message, params: paramvalues, rules: rules, messages: messages }); } }); $.extend(rules, { "__dummy__": true }); if (!skipattach) { valinfo.attachvalidation(); } }, parse: function (selector) { /// /// parses all the html elements in the specified selector. it looks for input elements decorated /// with the [data-val=true] attribute value and enables validation according to the data-val-* /// attribute values. /// /// any valid jquery selector. var $forms = $(selector) .parents("form") .andself() .add($(selector).find("form")) .filter("form"); $(selector).find(":input[data-val=true]").each(function () { $jqval.unobtrusive.parseelement(this, true); }); $forms.each(function () { var info = validationinfo(this); if (info) { info.attachvalidation(); } }); } }; adapters = $jqval.unobtrusive.adapters; adapters.add = function (adaptername, params, fn) { /// adds a new adapter to convert unobtrusive html into a jquery validate validation. /// the name of the adapter to be added. this matches the name used /// in the data-val-nnnn html attribute (where nnnn is the adapter name). /// [optional] an array of parameter names (strings) that will /// be extracted from the data-val-nnnn-mmmm html attributes (where nnnn is the adapter name, and /// mmmm is the parameter name). /// the function to call, which adapts the values from the html /// attributes into jquery validate rules and/or messages. /// if (!fn) { // called with no params, just a function fn = params; params = []; } this.push({ name: adaptername, params: params, adapt: fn }); return this; }; adapters.addbool = function (adaptername, rulename) { /// adds a new adapter to convert unobtrusive html into a jquery validate validation, where /// the jquery validate validation rule has no parameter values. /// the name of the adapter to be added. this matches the name used /// in the data-val-nnnn html attribute (where nnnn is the adapter name). /// [optional] the name of the jquery validate rule. if not provided, the value /// of adaptername will be used instead. /// return this.add(adaptername, function (options) { setvalidationvalues(options, rulename || adaptername, true); }); }; adapters.addminmax = function (adaptername, minrulename, maxrulename, minmaxrulename, minattribute, maxattribute) { /// adds a new adapter to convert unobtrusive html into a jquery validate validation, where /// the jquery validate validation has three potential rules (one for min-only, one for max-only, and /// one for min-and-max). the html parameters are expected to be named -min and -max. /// the name of the adapter to be added. this matches the name used /// in the data-val-nnnn html attribute (where nnnn is the adapter name). /// the name of the jquery validate rule to be used when you only /// have a minimum value. /// the name of the jquery validate rule to be used when you only /// have a maximum value. /// the name of the jquery validate rule to be used when you /// have both a minimum and maximum value. /// [optional] the name of the html attribute that /// contains the minimum value. the default is "min". /// [optional] the name of the html attribute that /// contains the maximum value. the default is "max". /// return this.add(adaptername, [minattribute || "min", maxattribute || "max"], function (options) { var min = options.params.min, max = options.params.max; if (min && max) { setvalidationvalues(options, minmaxrulename, [min, max]); } else if (min) { setvalidationvalues(options, minrulename, min); } else if (max) { setvalidationvalues(options, maxrulename, max); } }); }; adapters.addsingleval = function (adaptername, attribute, rulename) { /// adds a new adapter to convert unobtrusive html into a jquery validate validation, where /// the jquery validate validation rule has a single value. /// the name of the adapter to be added. this matches the name used /// in the data-val-nnnn html attribute(where nnnn is the adapter name). /// [optional] the name of the html attribute that contains the value. /// the default is "val". /// [optional] the name of the jquery validate rule. if not provided, the value /// of adaptername will be used instead. /// return this.add(adaptername, [attribute || "val"], function (options) { setvalidationvalues(options, rulename || adaptername, options.params[attribute]); }); }; $jqval.addmethod("__dummy__", function (value, element, params) { return true; }); $jqval.addmethod("regex", function (value, element, params) { var match; if (this.optional(element)) { return true; } match = new regexp(params).exec(value); return (match && (match.index === 0) && (match[0].length === value.length)); }); $jqval.addmethod("nonalphamin", function (value, element, nonalphamin) { var match; if (nonalphamin) { match = value.match(/\w/g); match = match && match.length >= nonalphamin; } return match; }); adapters.addsingleval("accept", "exts").addsingleval("regex", "pattern"); adapters.addbool("creditcard").addbool("date").addbool("digits").addbool("email").addbool("number").addbool("url"); adapters.addminmax("length", "minlength", "maxlength", "rangelength").addminmax("range", "min", "max", "range"); adapters.add("equalto", ["other"], function (options) { var prefix = getmodelprefix(options.element.name), other = options.params.other, fullothername = appendmodelprefix(other, prefix), element = $(options.form).find(":input[name='" + escapeattributevalue(fullothername) + "']")[0]; setvalidationvalues(options, "equalto", element); }); adapters.add("required", function (options) { // jquery validate equates "required" with "mandatory" for checkbox elements if (options.element.tagname.touppercase() !== "input" || options.element.type.touppercase() !== "checkbox") { setvalidationvalues(options, "required", true); } }); adapters.add("remote", ["url", "type", "additionalfields"], function (options) { var value = { url: options.params.url, type: options.params.type || "get", data: {} }, prefix = getmodelprefix(options.element.name); $.each(splitandtrim(options.params.additionalfields || options.element.name), function (i, fieldname) { var paramname = appendmodelprefix(fieldname, prefix); value.data[paramname] = function () { return $(options.form).find(":input[name='" + escapeattributevalue(paramname) + "']").val(); }; }); setvalidationvalues(options, "remote", value); }); adapters.add("password", ["min", "nonalphamin", "regex"], function (options) { if (options.params.min) { setvalidationvalues(options, "minlength", options.params.min); } if (options.params.nonalphamin) { setvalidationvalues(options, "nonalphamin", options.params.nonalphamin); } if (options.params.regex) { setvalidationvalues(options, "regex", options.params.regex); } }); $(function () { $jqval.unobtrusive.parse(document); }); } (jquery));