Files
Aaru.Documentation/jquery-validation/dist/jquery.validate.js

1605 lines
64 KiB
JavaScript
Raw Normal View History

/*!
* jQuery Validation Plugin v1.17.0
*
* https://jqueryvalidation.org/
*
* Copyright (c) 2017 Jörn Zaefferer
* Released under the MIT license
*/
2023-10-04 00:09:17 +01:00
(function (factory) {
if (typeof define === "function" && define.amd) {
2021-08-18 03:06:03 +01:00
define(["jquery"], factory);
2023-10-04 00:09:17 +01:00
} else if (typeof module === "object" && module.exports) {
2021-08-18 03:06:03 +01:00
module.exports = factory(require("jquery"));
2023-10-04 00:09:17 +01:00
} else {
2021-08-18 03:06:03 +01:00
factory(jQuery);
}
2023-10-04 00:09:17 +01:00
}(function ($) {
2021-08-18 03:06:03 +01:00
$.extend($.fn, {
// https://jqueryvalidation.org/validate/
2023-10-04 00:09:17 +01:00
validate: function (options) {
2021-08-18 03:06:03 +01:00
// If nothing is selected, return nothing; can't chain anyway
2023-10-04 00:09:17 +01:00
if (!this.length) {
if (options && options.debug && window.console) {
2021-08-18 03:06:03 +01:00
console.warn("Nothing selected, can't validate, returning nothing.");
}
return;
}
// Check if a validator for this form was already created
var validator = $.data(this[0], "validator");
2023-10-04 00:09:17 +01:00
if (validator) {
2021-08-18 03:06:03 +01:00
return validator;
}
// Add novalidate tag if HTML5.
this.attr("novalidate", "novalidate");
validator = new $.validator(options, this[0]);
$.data(this[0], "validator", validator);
2023-10-04 00:09:17 +01:00
if (validator.settings.onsubmit) {
2021-08-18 03:06:03 +01:00
2023-10-04 00:09:17 +01:00
this.on("click.validate", ":submit", function (event) {
2021-08-18 03:06:03 +01:00
// Track the used submit button to properly handle scripted
// submits later.
validator.submitButton = event.currentTarget;
// Allow suppressing validation by adding a cancel class to the submit button
2023-10-04 00:09:17 +01:00
if ($(this).hasClass("cancel")) {
2021-08-18 03:06:03 +01:00
validator.cancelSubmit = true;
}
// Allow suppressing validation by adding the html5 formnovalidate attribute to the submit button
2023-10-04 00:09:17 +01:00
if ($(this).attr("formnovalidate") !== undefined) {
2021-08-18 03:06:03 +01:00
validator.cancelSubmit = true;
}
});
// Validate the form on submit
2023-10-04 00:09:17 +01:00
this.on("submit.validate", function (event) {
if (validator.settings.debug) {
2021-08-18 03:06:03 +01:00
// Prevent form submit to be able to see console output
event.preventDefault();
}
2023-10-04 00:09:17 +01:00
function handle() {
2021-08-18 03:06:03 +01:00
var hidden, result;
// Insert a hidden input as a replacement for the missing submit button
// The hidden input is inserted in two cases:
// - A user defined a `submitHandler`
// - There was a pending request due to `remote` method and `stopRequest()`
// was called to submit the form in case it's valid
2023-10-04 00:09:17 +01:00
if (validator.submitButton && (validator.settings.submitHandler || validator.formSubmitted)) {
2021-08-18 03:06:03 +01:00
hidden = $("<input type='hidden'/>")
.attr("name", validator.submitButton.name)
.val($(validator.submitButton).val())
.appendTo(validator.currentForm);
}
2023-10-04 00:09:17 +01:00
if (validator.settings.submitHandler) {
2021-08-18 03:06:03 +01:00
result = validator.settings.submitHandler.call(validator, validator.currentForm, event);
2023-10-04 00:09:17 +01:00
if (hidden) {
2021-08-18 03:06:03 +01:00
// And clean up afterwards; thanks to no-block-scope, hidden can be referenced
hidden.remove();
}
2023-10-04 00:09:17 +01:00
if (result !== undefined) {
2021-08-18 03:06:03 +01:00
return result;
}
return false;
}
return true;
}
// Prevent submit for invalid forms or custom submit handlers
2023-10-04 00:09:17 +01:00
if (validator.cancelSubmit) {
2021-08-18 03:06:03 +01:00
validator.cancelSubmit = false;
return handle();
}
2023-10-04 00:09:17 +01:00
if (validator.form()) {
if (validator.pendingRequest) {
2021-08-18 03:06:03 +01:00
validator.formSubmitted = true;
return false;
}
return handle();
2023-10-04 00:09:17 +01:00
} else {
2021-08-18 03:06:03 +01:00
validator.focusInvalid();
return false;
}
});
}
return validator;
},
// https://jqueryvalidation.org/valid/
2023-10-04 00:09:17 +01:00
valid: function () {
2021-08-18 03:06:03 +01:00
var valid, validator, errorList;
2023-10-04 00:09:17 +01:00
if ($(this[0]).is("form")) {
2021-08-18 03:06:03 +01:00
valid = this.validate().form();
2023-10-04 00:09:17 +01:00
} else {
2021-08-18 03:06:03 +01:00
errorList = [];
valid = true;
validator = $(this[0].form).validate();
2023-10-04 00:09:17 +01:00
this.each(function () {
2021-08-18 03:06:03 +01:00
valid = validator.element(this) && valid;
2023-10-04 00:09:17 +01:00
if (!valid) {
2021-08-18 03:06:03 +01:00
errorList = errorList.concat(validator.errorList);
}
});
validator.errorList = errorList;
}
return valid;
},
// https://jqueryvalidation.org/rules/
2023-10-04 00:09:17 +01:00
rules: function (command, argument) {
2021-08-18 03:06:03 +01:00
var element = this[0],
settings, staticRules, existingRules, data, param, filtered;
// If nothing is selected, return empty object; can't chain anyway
2023-10-04 00:09:17 +01:00
if (element == null) {
2021-08-18 03:06:03 +01:00
return;
}
2023-10-04 00:09:17 +01:00
if (!element.form && element.hasAttribute("contenteditable")) {
2021-08-18 03:06:03 +01:00
element.form = this.closest("form")[0];
element.name = this.attr("name");
}
2023-10-04 00:09:17 +01:00
if (element.form == null) {
2021-08-18 03:06:03 +01:00
return;
}
2023-10-04 00:09:17 +01:00
if (command) {
2021-08-18 03:06:03 +01:00
settings = $.data(element.form, "validator").settings;
staticRules = settings.rules;
existingRules = $.validator.staticRules(element);
2023-10-04 00:09:17 +01:00
switch (command) {
2021-08-18 03:06:03 +01:00
case "add":
$.extend(existingRules, $.validator.normalizeRule(argument));
// Remove messages from rules, but allow them to be set separately
delete existingRules.messages;
staticRules[element.name] = existingRules;
2023-10-04 00:09:17 +01:00
if (argument.messages) {
2021-08-18 03:06:03 +01:00
settings.messages[element.name] = $.extend(settings.messages[element.name], argument.messages);
}
break;
case "remove":
2023-10-04 00:09:17 +01:00
if (!argument) {
2021-08-18 03:06:03 +01:00
delete staticRules[element.name];
return existingRules;
}
filtered = {};
2023-10-04 00:09:17 +01:00
$.each(argument.split(/\s/), function (index, method) {
2021-08-18 03:06:03 +01:00
filtered[method] = existingRules[method];
delete existingRules[method];
});
return filtered;
}
}
data = $.validator.normalizeRules(
$.extend(
{},
$.validator.classRules(element),
$.validator.attributeRules(element),
$.validator.dataRules(element),
$.validator.staticRules(element)
), element);
// Make sure required is at front
2023-10-04 00:09:17 +01:00
if (data.required) {
2021-08-18 03:06:03 +01:00
param = data.required;
delete data.required;
data = $.extend({required: param}, data);
}
// Make sure remote is at back
2023-10-04 00:09:17 +01:00
if (data.remote) {
2021-08-18 03:06:03 +01:00
param = data.remote;
delete data.remote;
data = $.extend(data, {remote: param});
}
return data;
}
});
// Custom selectors
2021-08-18 03:06:03 +01:00
$.extend($.expr.pseudos || $.expr[":"], { // '|| $.expr[ ":" ]' here enables backwards compatibility to jQuery 1.7. Can be removed when dropping jQ 1.7.x support
// https://jqueryvalidation.org/blank-selector/
2023-10-04 00:09:17 +01:00
blank: function (a) {
2021-08-18 03:06:03 +01:00
return !$.trim("" + $(a).val());
},
// https://jqueryvalidation.org/filled-selector/
2023-10-04 00:09:17 +01:00
filled: function (a) {
2021-08-18 03:06:03 +01:00
var val = $(a).val();
return val !== null && !!$.trim("" + val);
},
// https://jqueryvalidation.org/unchecked-selector/
2023-10-04 00:09:17 +01:00
unchecked: function (a) {
2021-08-18 03:06:03 +01:00
return !$(a).prop("checked");
}
});
// Constructor for validator
2023-10-04 00:09:17 +01:00
$.validator = function (options, form) {
2021-08-18 03:06:03 +01:00
this.settings = $.extend(true, {}, $.validator.defaults, options);
this.currentForm = form;
this.init();
};
// https://jqueryvalidation.org/jQuery.validator.format/
2023-10-04 00:09:17 +01:00
$.validator.format = function (source, params) {
if (arguments.length === 1) {
return function () {
2021-08-18 03:06:03 +01:00
var args = $.makeArray(arguments);
args.unshift(source);
return $.validator.format.apply(this, args);
};
}
2023-10-04 00:09:17 +01:00
if (params === undefined) {
2021-08-18 03:06:03 +01:00
return source;
}
2023-10-04 00:09:17 +01:00
if (arguments.length > 2 && params.constructor !== Array) {
2021-08-18 03:06:03 +01:00
params = $.makeArray(arguments).slice(1);
}
2023-10-04 00:09:17 +01:00
if (params.constructor !== Array) {
2021-08-18 03:06:03 +01:00
params = [params];
}
2023-10-04 00:09:17 +01:00
$.each(params, function (i, n) {
source = source.replace(new RegExp("\\{" + i + "\\}", "g"), function () {
2021-08-18 03:06:03 +01:00
return n;
});
});
return source;
};
$.extend($.validator, {
defaults: {
messages: {},
groups: {},
rules: {},
errorClass: "error",
pendingClass: "pending",
validClass: "valid",
errorElement: "label",
focusCleanup: false,
focusInvalid: true,
errorContainer: $([]),
errorLabelContainer: $([]),
onsubmit: true,
ignore: ":hidden",
ignoreTitle: false,
2023-10-04 00:09:17 +01:00
onfocusin: function (element) {
2021-08-18 03:06:03 +01:00
this.lastActive = element;
// Hide error label and remove error class on focus if enabled
2023-10-04 00:09:17 +01:00
if (this.settings.focusCleanup) {
if (this.settings.unhighlight) {
2021-08-18 03:06:03 +01:00
this.settings.unhighlight.call(this, element, this.settings.errorClass, this.settings.validClass);
}
this.hideThese(this.errorsFor(element));
}
},
2023-10-04 00:09:17 +01:00
onfocusout: function (element) {
if (!this.checkable(element) && (element.name in this.submitted || !this.optional(element))) {
2021-08-18 03:06:03 +01:00
this.element(element);
}
},
2023-10-04 00:09:17 +01:00
onkeyup: function (element, event) {
2021-08-18 03:06:03 +01:00
// Avoid revalidate the field when pressing one of the following keys
// Shift => 16
// Ctrl => 17
// Alt => 18
// Caps lock => 20
// End => 35
// Home => 36
// Left arrow => 37
// Up arrow => 38
// Right arrow => 39
// Down arrow => 40
// Insert => 45
// Num lock => 144
// AltGr key => 225
var excludedKeys = [
16, 17, 18, 20, 35, 36, 37,
38, 39, 40, 45, 144, 225
];
2023-10-04 00:09:17 +01:00
if (event.which === 9 && this.elementValue(element) === "" || $.inArray(event.keyCode, excludedKeys) !== -1) {
2021-08-18 03:06:03 +01:00
2023-10-04 00:09:17 +01:00
} else if (element.name in this.submitted || element.name in this.invalid) {
2021-08-18 03:06:03 +01:00
this.element(element);
}
},
2023-10-04 00:09:17 +01:00
onclick: function (element) {
2021-08-18 03:06:03 +01:00
// Click on selects, radiobuttons and checkboxes
2023-10-04 00:09:17 +01:00
if (element.name in this.submitted) {
2021-08-18 03:06:03 +01:00
this.element(element);
// Or option elements, check parent select in that case
2023-10-04 00:09:17 +01:00
} else if (element.parentNode.name in this.submitted) {
2021-08-18 03:06:03 +01:00
this.element(element.parentNode);
}
},
2023-10-04 00:09:17 +01:00
highlight: function (element, errorClass, validClass) {
if (element.type === "radio") {
2021-08-18 03:06:03 +01:00
this.findByName(element.name).addClass(errorClass).removeClass(validClass);
2023-10-04 00:09:17 +01:00
} else {
2021-08-18 03:06:03 +01:00
$(element).addClass(errorClass).removeClass(validClass);
}
},
2023-10-04 00:09:17 +01:00
unhighlight: function (element, errorClass, validClass) {
if (element.type === "radio") {
2021-08-18 03:06:03 +01:00
this.findByName(element.name).removeClass(errorClass).addClass(validClass);
2023-10-04 00:09:17 +01:00
} else {
2021-08-18 03:06:03 +01:00
$(element).removeClass(errorClass).addClass(validClass);
}
}
},
// https://jqueryvalidation.org/jQuery.validator.setDefaults/
2023-10-04 00:09:17 +01:00
setDefaults: function (settings) {
2021-08-18 03:06:03 +01:00
$.extend($.validator.defaults, settings);
},
messages: {
required: "This field is required.",
remote: "Please fix this field.",
email: "Please enter a valid email address.",
url: "Please enter a valid URL.",
date: "Please enter a valid date.",
dateISO: "Please enter a valid date (ISO).",
number: "Please enter a valid number.",
digits: "Please enter only digits.",
equalTo: "Please enter the same value again.",
maxlength: $.validator.format("Please enter no more than {0} characters."),
minlength: $.validator.format("Please enter at least {0} characters."),
rangelength: $.validator.format("Please enter a value between {0} and {1} characters long."),
range: $.validator.format("Please enter a value between {0} and {1}."),
max: $.validator.format("Please enter a value less than or equal to {0}."),
min: $.validator.format("Please enter a value greater than or equal to {0}."),
step: $.validator.format("Please enter a multiple of {0}.")
},
autoCreateRanges: false,
prototype: {
2023-10-04 00:09:17 +01:00
init: function () {
2021-08-18 03:06:03 +01:00
this.labelContainer = $(this.settings.errorLabelContainer);
this.errorContext = this.labelContainer.length && this.labelContainer || $(this.currentForm);
this.containers = $(this.settings.errorContainer).add(this.settings.errorLabelContainer);
this.submitted = {};
this.valueCache = {};
this.pendingRequest = 0;
this.pending = {};
this.invalid = {};
this.reset();
var groups = (this.groups = {}),
rules;
2023-10-04 00:09:17 +01:00
$.each(this.settings.groups, function (key, value) {
if (typeof value === "string") {
2021-08-18 03:06:03 +01:00
value = value.split(/\s/);
}
2023-10-04 00:09:17 +01:00
$.each(value, function (index, name) {
2021-08-18 03:06:03 +01:00
groups[name] = key;
});
});
rules = this.settings.rules;
2023-10-04 00:09:17 +01:00
$.each(rules, function (key, value) {
2021-08-18 03:06:03 +01:00
rules[key] = $.validator.normalizeRule(value);
});
2023-10-04 00:09:17 +01:00
function delegate(event) {
2021-08-18 03:06:03 +01:00
// Set form expando on contenteditable
2023-10-04 00:09:17 +01:00
if (!this.form && this.hasAttribute("contenteditable")) {
2021-08-18 03:06:03 +01:00
this.form = $(this).closest("form")[0];
this.name = $(this).attr("name");
}
var validator = $.data(this.form, "validator"),
eventType = "on" + event.type.replace(/^validate/, ""),
settings = validator.settings;
2023-10-04 00:09:17 +01:00
if (settings[eventType] && !$(this).is(settings.ignore)) {
2021-08-18 03:06:03 +01:00
settings[eventType].call(validator, this, event);
}
}
$(this.currentForm)
.on("focusin.validate focusout.validate keyup.validate",
":text, [type='password'], [type='file'], select, textarea, [type='number'], [type='search'], " +
"[type='tel'], [type='url'], [type='email'], [type='datetime'], [type='date'], [type='month'], " +
"[type='week'], [type='time'], [type='datetime-local'], [type='range'], [type='color'], " +
"[type='radio'], [type='checkbox'], [contenteditable], [type='button']", delegate)
// Support: Chrome, oldIE
// "select" is provided as event.target when clicking a option
.on("click.validate", "select, option, [type='radio'], [type='checkbox']", delegate);
2023-10-04 00:09:17 +01:00
if (this.settings.invalidHandler) {
2021-08-18 03:06:03 +01:00
$(this.currentForm).on("invalid-form.validate", this.settings.invalidHandler);
}
},
// https://jqueryvalidation.org/Validator.form/
2023-10-04 00:09:17 +01:00
form: function () {
2021-08-18 03:06:03 +01:00
this.checkForm();
$.extend(this.submitted, this.errorMap);
this.invalid = $.extend({}, this.errorMap);
2023-10-04 00:09:17 +01:00
if (!this.valid()) {
2021-08-18 03:06:03 +01:00
$(this.currentForm).triggerHandler("invalid-form", [this]);
}
this.showErrors();
return this.valid();
},
2023-10-04 00:09:17 +01:00
checkForm: function () {
2021-08-18 03:06:03 +01:00
this.prepareForm();
2023-10-04 00:09:17 +01:00
for (var i = 0, elements = (this.currentElements = this.elements()); elements[i]; i++) {
2021-08-18 03:06:03 +01:00
this.check(elements[i]);
}
return this.valid();
},
// https://jqueryvalidation.org/Validator.element/
2023-10-04 00:09:17 +01:00
element: function (element) {
2021-08-18 03:06:03 +01:00
var cleanElement = this.clean(element),
checkElement = this.validationTargetFor(cleanElement),
v = this,
result = true,
rs, group;
2023-10-04 00:09:17 +01:00
if (checkElement === undefined) {
2021-08-18 03:06:03 +01:00
delete this.invalid[cleanElement.name];
2023-10-04 00:09:17 +01:00
} else {
2021-08-18 03:06:03 +01:00
this.prepareElement(checkElement);
this.currentElements = $(checkElement);
// If this element is grouped, then validate all group elements already
// containing a value
group = this.groups[checkElement.name];
2023-10-04 00:09:17 +01:00
if (group) {
$.each(this.groups, function (name, testgroup) {
if (testgroup === group && name !== checkElement.name) {
2021-08-18 03:06:03 +01:00
cleanElement = v.validationTargetFor(v.clean(v.findByName(name)));
2023-10-04 00:09:17 +01:00
if (cleanElement && cleanElement.name in v.invalid) {
2021-08-18 03:06:03 +01:00
v.currentElements.push(cleanElement);
result = v.check(cleanElement) && result;
}
}
});
}
rs = this.check(checkElement) !== false;
result = result && rs;
2023-10-04 00:09:17 +01:00
if (rs) {
2021-08-18 03:06:03 +01:00
this.invalid[checkElement.name] = false;
2023-10-04 00:09:17 +01:00
} else {
2021-08-18 03:06:03 +01:00
this.invalid[checkElement.name] = true;
}
2023-10-04 00:09:17 +01:00
if (!this.numberOfInvalids()) {
2021-08-18 03:06:03 +01:00
// Hide error containers on last error
this.toHide = this.toHide.add(this.containers);
}
this.showErrors();
// Add aria-invalid status for screen readers
$(element).attr("aria-invalid", !rs);
}
return result;
},
// https://jqueryvalidation.org/Validator.showErrors/
2023-10-04 00:09:17 +01:00
showErrors: function (errors) {
if (errors) {
2021-08-18 03:06:03 +01:00
var validator = this;
// Add items to error list and map
$.extend(this.errorMap, errors);
2023-10-04 00:09:17 +01:00
this.errorList = $.map(this.errorMap, function (message, name) {
2021-08-18 03:06:03 +01:00
return {
message: message,
element: validator.findByName(name)[0]
};
});
// Remove items from success list
2023-10-04 00:09:17 +01:00
this.successList = $.grep(this.successList, function (element) {
2021-08-18 03:06:03 +01:00
return !(element.name in errors);
});
}
2023-10-04 00:09:17 +01:00
if (this.settings.showErrors) {
2021-08-18 03:06:03 +01:00
this.settings.showErrors.call(this, this.errorMap, this.errorList);
2023-10-04 00:09:17 +01:00
} else {
2021-08-18 03:06:03 +01:00
this.defaultShowErrors();
}
},
// https://jqueryvalidation.org/Validator.resetForm/
2023-10-04 00:09:17 +01:00
resetForm: function () {
if ($.fn.resetForm) {
2021-08-18 03:06:03 +01:00
$(this.currentForm).resetForm();
}
this.invalid = {};
this.submitted = {};
this.prepareForm();
this.hideErrors();
var elements = this.elements()
.removeData("previousValue")
.removeAttr("aria-invalid");
this.resetElements(elements);
},
2023-10-04 00:09:17 +01:00
resetElements: function (elements) {
2021-08-18 03:06:03 +01:00
var i;
2023-10-04 00:09:17 +01:00
if (this.settings.unhighlight) {
for (i = 0; elements[i]; i++) {
2021-08-18 03:06:03 +01:00
this.settings.unhighlight.call(this, elements[i],
this.settings.errorClass, "");
this.findByName(elements[i].name).removeClass(this.settings.validClass);
}
2023-10-04 00:09:17 +01:00
} else {
2021-08-18 03:06:03 +01:00
elements
.removeClass(this.settings.errorClass)
.removeClass(this.settings.validClass);
}
},
2023-10-04 00:09:17 +01:00
numberOfInvalids: function () {
2021-08-18 03:06:03 +01:00
return this.objectLength(this.invalid);
},
2023-10-04 00:09:17 +01:00
objectLength: function (obj) {
2021-08-18 03:06:03 +01:00
/* jshint unused: false */
var count = 0,
i;
2023-10-04 00:09:17 +01:00
for (i in obj) {
2021-08-18 03:06:03 +01:00
// This check allows counting elements with empty error
// message as invalid elements
2023-10-04 00:09:17 +01:00
if (obj[i] !== undefined && obj[i] !== null && obj[i] !== false) {
2021-08-18 03:06:03 +01:00
count++;
}
}
return count;
},
2023-10-04 00:09:17 +01:00
hideErrors: function () {
2021-08-18 03:06:03 +01:00
this.hideThese(this.toHide);
},
2023-10-04 00:09:17 +01:00
hideThese: function (errors) {
2021-08-18 03:06:03 +01:00
errors.not(this.containers).text("");
this.addWrapper(errors).hide();
},
2023-10-04 00:09:17 +01:00
valid: function () {
2021-08-18 03:06:03 +01:00
return this.size() === 0;
},
2023-10-04 00:09:17 +01:00
size: function () {
2021-08-18 03:06:03 +01:00
return this.errorList.length;
},
2023-10-04 00:09:17 +01:00
focusInvalid: function () {
if (this.settings.focusInvalid) {
try {
2021-08-18 03:06:03 +01:00
$(this.findLastActive() || this.errorList.length && this.errorList[0].element || [])
.filter(":visible")
.focus()
// Manually trigger focusin event; without it, focusin handler isn't called, findLastActive won't have anything to find
.trigger("focusin");
2023-10-04 00:09:17 +01:00
} catch (e) {
2021-08-18 03:06:03 +01:00
// Ignore IE throwing errors when focusing hidden elements
}
}
},
2023-10-04 00:09:17 +01:00
findLastActive: function () {
2021-08-18 03:06:03 +01:00
var lastActive = this.lastActive;
2023-10-04 00:09:17 +01:00
return lastActive && $.grep(this.errorList, function (n) {
2021-08-18 03:06:03 +01:00
return n.element.name === lastActive.name;
}).length === 1 && lastActive;
},
2023-10-04 00:09:17 +01:00
elements: function () {
2021-08-18 03:06:03 +01:00
var validator = this,
rulesCache = {};
// Select all valid inputs inside the form (no submit or reset buttons)
return $(this.currentForm)
.find("input, select, textarea, [contenteditable]")
.not(":submit, :reset, :image, :disabled")
.not(this.settings.ignore)
2023-10-04 00:09:17 +01:00
.filter(function () {
2021-08-18 03:06:03 +01:00
var name = this.name || $(this).attr("name"); // For contenteditable
2023-10-04 00:09:17 +01:00
if (!name && validator.settings.debug && window.console) {
2021-08-18 03:06:03 +01:00
console.error("%o has no name assigned", this);
}
// Set form expando on contenteditable
2023-10-04 00:09:17 +01:00
if (this.hasAttribute("contenteditable")) {
2021-08-18 03:06:03 +01:00
this.form = $(this).closest("form")[0];
this.name = name;
}
// Select only the first element for each name, and only those with rules specified
2023-10-04 00:09:17 +01:00
if (name in rulesCache || !validator.objectLength($(this).rules())) {
2021-08-18 03:06:03 +01:00
return false;
}
rulesCache[name] = true;
return true;
});
},
2023-10-04 00:09:17 +01:00
clean: function (selector) {
2021-08-18 03:06:03 +01:00
return $(selector)[0];
},
2023-10-04 00:09:17 +01:00
errors: function () {
2021-08-18 03:06:03 +01:00
var errorClass = this.settings.errorClass.split(" ").join(".");
return $(this.settings.errorElement + "." + errorClass, this.errorContext);
},
2023-10-04 00:09:17 +01:00
resetInternals: function () {
2021-08-18 03:06:03 +01:00
this.successList = [];
this.errorList = [];
this.errorMap = {};
this.toShow = $([]);
this.toHide = $([]);
},
2023-10-04 00:09:17 +01:00
reset: function () {
2021-08-18 03:06:03 +01:00
this.resetInternals();
this.currentElements = $([]);
},
2023-10-04 00:09:17 +01:00
prepareForm: function () {
2021-08-18 03:06:03 +01:00
this.reset();
this.toHide = this.errors().add(this.containers);
},
2023-10-04 00:09:17 +01:00
prepareElement: function (element) {
2021-08-18 03:06:03 +01:00
this.reset();
this.toHide = this.errorsFor(element);
},
2023-10-04 00:09:17 +01:00
elementValue: function (element) {
2021-08-18 03:06:03 +01:00
var $element = $(element),
type = element.type,
val, idx;
2023-10-04 00:09:17 +01:00
if (type === "radio" || type === "checkbox") {
2021-08-18 03:06:03 +01:00
return this.findByName(element.name).filter(":checked").val();
2023-10-04 00:09:17 +01:00
} else if (type === "number" && typeof element.validity !== "undefined") {
2021-08-18 03:06:03 +01:00
return element.validity.badInput ? "NaN" : $element.val();
}
2023-10-04 00:09:17 +01:00
if (element.hasAttribute("contenteditable")) {
2021-08-18 03:06:03 +01:00
val = $element.text();
2023-10-04 00:09:17 +01:00
} else {
2021-08-18 03:06:03 +01:00
val = $element.val();
}
2023-10-04 00:09:17 +01:00
if (type === "file") {
2021-08-18 03:06:03 +01:00
// Modern browser (chrome & safari)
2023-10-04 00:09:17 +01:00
if (val.substr(0, 12) === "C:\\fakepath\\") {
2021-08-18 03:06:03 +01:00
return val.substr(12);
}
// Legacy browsers
// Unix-based path
idx = val.lastIndexOf("/");
2023-10-04 00:09:17 +01:00
if (idx >= 0) {
2021-08-18 03:06:03 +01:00
return val.substr(idx + 1);
}
// Windows-based path
idx = val.lastIndexOf("\\");
2023-10-04 00:09:17 +01:00
if (idx >= 0) {
2021-08-18 03:06:03 +01:00
return val.substr(idx + 1);
}
// Just the file name
return val;
}
2023-10-04 00:09:17 +01:00
if (typeof val === "string") {
2021-08-18 03:06:03 +01:00
return val.replace(/\r/g, "");
}
return val;
},
2023-10-04 00:09:17 +01:00
check: function (element) {
2021-08-18 03:06:03 +01:00
element = this.validationTargetFor(this.clean(element));
var rules = $(element).rules(),
2023-10-04 00:09:17 +01:00
rulesCount = $.map(rules, function (n, i) {
2021-08-18 03:06:03 +01:00
return i;
}).length,
dependencyMismatch = false,
val = this.elementValue(element),
result, method, rule, normalizer;
// Prioritize the local normalizer defined for this element over the global one
// if the former exists, otherwise user the global one in case it exists.
2023-10-04 00:09:17 +01:00
if (typeof rules.normalizer === "function") {
2021-08-18 03:06:03 +01:00
normalizer = rules.normalizer;
2023-10-04 00:09:17 +01:00
} else if (typeof this.settings.normalizer === "function") {
2021-08-18 03:06:03 +01:00
normalizer = this.settings.normalizer;
}
// If normalizer is defined, then call it to retreive the changed value instead
// of using the real one.
// Note that `this` in the normalizer is `element`.
2023-10-04 00:09:17 +01:00
if (normalizer) {
2021-08-18 03:06:03 +01:00
val = normalizer.call(element, val);
2023-10-04 00:09:17 +01:00
if (typeof val !== "string") {
2021-08-18 03:06:03 +01:00
throw new TypeError("The normalizer should return a string value.");
}
// Delete the normalizer from rules to avoid treating it as a pre-defined method.
delete rules.normalizer;
}
2023-10-04 00:09:17 +01:00
for (method in rules) {
2021-08-18 03:06:03 +01:00
rule = {method: method, parameters: rules[method]};
2023-10-04 00:09:17 +01:00
try {
2021-08-18 03:06:03 +01:00
result = $.validator.methods[method].call(this, val, element, rule.parameters);
// If a method indicates that the field is optional and therefore valid,
// don't mark it as valid when there are no other rules
2023-10-04 00:09:17 +01:00
if (result === "dependency-mismatch" && rulesCount === 1) {
2021-08-18 03:06:03 +01:00
dependencyMismatch = true;
continue;
}
dependencyMismatch = false;
2023-10-04 00:09:17 +01:00
if (result === "pending") {
2021-08-18 03:06:03 +01:00
this.toHide = this.toHide.not(this.errorsFor(element));
return;
}
2023-10-04 00:09:17 +01:00
if (!result) {
2021-08-18 03:06:03 +01:00
this.formatAndAdd(element, rule);
return false;
}
2023-10-04 00:09:17 +01:00
} catch (e) {
if (this.settings.debug && window.console) {
2021-08-18 03:06:03 +01:00
console.log("Exception occurred when checking element " + element.id + ", check the '" + rule.method + "' method.", e);
}
2023-10-04 00:09:17 +01:00
if (e instanceof TypeError) {
2021-08-18 03:06:03 +01:00
e.message += ". Exception occurred when checking element " + element.id + ", check the '" + rule.method + "' method.";
}
throw e;
}
}
2023-10-04 00:09:17 +01:00
if (dependencyMismatch) {
2021-08-18 03:06:03 +01:00
return;
}
2023-10-04 00:09:17 +01:00
if (this.objectLength(rules)) {
2021-08-18 03:06:03 +01:00
this.successList.push(element);
}
return true;
},
// Return the custom message for the given element and validation method
// specified in the element's HTML5 data attribute
// return the generic message if present and no method specific message is present
2023-10-04 00:09:17 +01:00
customDataMessage: function (element, method) {
2021-08-18 03:06:03 +01:00
return $(element).data("msg" + method.charAt(0).toUpperCase() +
method.substring(1).toLowerCase()) || $(element).data("msg");
},
// Return the custom message for the given element name and validation method
2023-10-04 00:09:17 +01:00
customMessage: function (name, method) {
2021-08-18 03:06:03 +01:00
var m = this.settings.messages[name];
return m && (m.constructor === String ? m : m[method]);
},
// Return the first defined argument, allowing empty strings
2023-10-04 00:09:17 +01:00
findDefined: function () {
for (var i = 0; i < arguments.length; i++) {
if (arguments[i] !== undefined) {
2021-08-18 03:06:03 +01:00
return arguments[i];
}
}
return undefined;
},
// The second parameter 'rule' used to be a string, and extended to an object literal
// of the following form:
// rule = {
// method: "method name",
// parameters: "the given method parameters"
// }
//
// The old behavior still supported, kept to maintain backward compatibility with
// old code, and will be removed in the next major release.
2023-10-04 00:09:17 +01:00
defaultMessage: function (element, rule) {
if (typeof rule === "string") {
2021-08-18 03:06:03 +01:00
rule = {method: rule};
}
var message = this.findDefined(
this.customMessage(element.name, rule.method),
this.customDataMessage(element, rule.method),
// 'title' is never undefined, so handle empty string as undefined
!this.settings.ignoreTitle && element.title || undefined,
$.validator.messages[rule.method],
"<strong>Warning: No message defined for " + element.name + "</strong>"
),
theregex = /\$?\{(\d+)\}/g;
2023-10-04 00:09:17 +01:00
if (typeof message === "function") {
2021-08-18 03:06:03 +01:00
message = message.call(this, rule.parameters, element);
2023-10-04 00:09:17 +01:00
} else if (theregex.test(message)) {
2021-08-18 03:06:03 +01:00
message = $.validator.format(message.replace(theregex, "{$1}"), rule.parameters);
}
return message;
},
2023-10-04 00:09:17 +01:00
formatAndAdd: function (element, rule) {
2021-08-18 03:06:03 +01:00
var message = this.defaultMessage(element, rule);
this.errorList.push({
message: message,
element: element,
method: rule.method
});
this.errorMap[element.name] = message;
this.submitted[element.name] = message;
},
2023-10-04 00:09:17 +01:00
addWrapper: function (toToggle) {
if (this.settings.wrapper) {
2021-08-18 03:06:03 +01:00
toToggle = toToggle.add(toToggle.parent(this.settings.wrapper));
}
return toToggle;
},
2023-10-04 00:09:17 +01:00
defaultShowErrors: function () {
2021-08-18 03:06:03 +01:00
var i, elements, error;
2023-10-04 00:09:17 +01:00
for (i = 0; this.errorList[i]; i++) {
2021-08-18 03:06:03 +01:00
error = this.errorList[i];
2023-10-04 00:09:17 +01:00
if (this.settings.highlight) {
2021-08-18 03:06:03 +01:00
this.settings.highlight.call(this, error.element, this.settings.errorClass, this.settings.validClass);
}
this.showLabel(error.element, error.message);
}
2023-10-04 00:09:17 +01:00
if (this.errorList.length) {
2021-08-18 03:06:03 +01:00
this.toShow = this.toShow.add(this.containers);
}
2023-10-04 00:09:17 +01:00
if (this.settings.success) {
for (i = 0; this.successList[i]; i++) {
2021-08-18 03:06:03 +01:00
this.showLabel(this.successList[i]);
}
}
2023-10-04 00:09:17 +01:00
if (this.settings.unhighlight) {
for (i = 0, elements = this.validElements(); elements[i]; i++) {
2021-08-18 03:06:03 +01:00
this.settings.unhighlight.call(this, elements[i], this.settings.errorClass, this.settings.validClass);
}
}
this.toHide = this.toHide.not(this.toShow);
this.hideErrors();
this.addWrapper(this.toShow).show();
},
2023-10-04 00:09:17 +01:00
validElements: function () {
2021-08-18 03:06:03 +01:00
return this.currentElements.not(this.invalidElements());
},
2023-10-04 00:09:17 +01:00
invalidElements: function () {
return $(this.errorList).map(function () {
2021-08-18 03:06:03 +01:00
return this.element;
});
},
2023-10-04 00:09:17 +01:00
showLabel: function (element, message) {
2021-08-18 03:06:03 +01:00
var place, group, errorID, v,
error = this.errorsFor(element),
elementID = this.idOrName(element),
describedBy = $(element).attr("aria-describedby");
2023-10-04 00:09:17 +01:00
if (error.length) {
2021-08-18 03:06:03 +01:00
// Refresh error/success class
error.removeClass(this.settings.validClass).addClass(this.settings.errorClass);
// Replace message on existing label
error.html(message);
2023-10-04 00:09:17 +01:00
} else {
2021-08-18 03:06:03 +01:00
// Create error element
error = $("<" + this.settings.errorElement + ">")
.attr("id", elementID + "-error")
.addClass(this.settings.errorClass)
.html(message || "");
// Maintain reference to the element to be placed into the DOM
place = error;
2023-10-04 00:09:17 +01:00
if (this.settings.wrapper) {
2021-08-18 03:06:03 +01:00
// Make sure the element is visible, even in IE
// actually showing the wrapped element is handled elsewhere
place = error.hide().show().wrap("<" + this.settings.wrapper + "/>").parent();
}
2023-10-04 00:09:17 +01:00
if (this.labelContainer.length) {
2021-08-18 03:06:03 +01:00
this.labelContainer.append(place);
2023-10-04 00:09:17 +01:00
} else if (this.settings.errorPlacement) {
2021-08-18 03:06:03 +01:00
this.settings.errorPlacement.call(this, place, $(element));
2023-10-04 00:09:17 +01:00
} else {
2021-08-18 03:06:03 +01:00
place.insertAfter(element);
}
// Link error back to the element
2023-10-04 00:09:17 +01:00
if (error.is("label")) {
2021-08-18 03:06:03 +01:00
// If the error is a label, then associate using 'for'
error.attr("for", elementID);
// If the element is not a child of an associated label, then it's necessary
// to explicitly apply aria-describedby
2023-10-04 00:09:17 +01:00
} else if (error.parents("label[for='" + this.escapeCssMeta(elementID) + "']").length === 0) {
2021-08-18 03:06:03 +01:00
errorID = error.attr("id");
// Respect existing non-error aria-describedby
2023-10-04 00:09:17 +01:00
if (!describedBy) {
2021-08-18 03:06:03 +01:00
describedBy = errorID;
2023-10-04 00:09:17 +01:00
} else if (!describedBy.match(new RegExp("\\b" + this.escapeCssMeta(errorID) + "\\b"))) {
2021-08-18 03:06:03 +01:00
// Add to end of list if not already present
describedBy += " " + errorID;
}
$(element).attr("aria-describedby", describedBy);
// If this element is grouped, then assign to all elements in the same group
group = this.groups[element.name];
2023-10-04 00:09:17 +01:00
if (group) {
2021-08-18 03:06:03 +01:00
v = this;
2023-10-04 00:09:17 +01:00
$.each(v.groups, function (name, testgroup) {
if (testgroup === group) {
2021-08-18 03:06:03 +01:00
$("[name='" + v.escapeCssMeta(name) + "']", v.currentForm)
.attr("aria-describedby", error.attr("id"));
}
});
}
}
}
2023-10-04 00:09:17 +01:00
if (!message && this.settings.success) {
2021-08-18 03:06:03 +01:00
error.text("");
2023-10-04 00:09:17 +01:00
if (typeof this.settings.success === "string") {
2021-08-18 03:06:03 +01:00
error.addClass(this.settings.success);
2023-10-04 00:09:17 +01:00
} else {
2021-08-18 03:06:03 +01:00
this.settings.success(error, element);
}
}
this.toShow = this.toShow.add(error);
},
2023-10-04 00:09:17 +01:00
errorsFor: function (element) {
2021-08-18 03:06:03 +01:00
var name = this.escapeCssMeta(this.idOrName(element)),
describer = $(element).attr("aria-describedby"),
selector = "label[for='" + name + "'], label[for='" + name + "'] *";
// 'aria-describedby' should directly reference the error element
2023-10-04 00:09:17 +01:00
if (describer) {
2021-08-18 03:06:03 +01:00
selector = selector + ", #" + this.escapeCssMeta(describer)
.replace(/\s+/g, ", #");
}
return this
.errors()
.filter(selector);
},
// See https://api.jquery.com/category/selectors/, for CSS
// meta-characters that should be escaped in order to be used with JQuery
// as a literal part of a name/id or any selector.
2023-10-04 00:09:17 +01:00
escapeCssMeta: function (string) {
2021-08-18 03:06:03 +01:00
return string.replace(/([\\!"#$%&'()*+,./:;<=>?@\[\]^`{|}~])/g, "\\$1");
},
2023-10-04 00:09:17 +01:00
idOrName: function (element) {
2021-08-18 03:06:03 +01:00
return this.groups[element.name] || (this.checkable(element) ? element.name : element.id || element.name);
},
2023-10-04 00:09:17 +01:00
validationTargetFor: function (element) {
2021-08-18 03:06:03 +01:00
// If radio/checkbox, validate first element in group instead
2023-10-04 00:09:17 +01:00
if (this.checkable(element)) {
2021-08-18 03:06:03 +01:00
element = this.findByName(element.name);
}
// Always apply ignore filter
return $(element).not(this.settings.ignore)[0];
},
2023-10-04 00:09:17 +01:00
checkable: function (element) {
2021-08-18 03:06:03 +01:00
return (/radio|checkbox/i).test(element.type);
},
2023-10-04 00:09:17 +01:00
findByName: function (name) {
2021-08-18 03:06:03 +01:00
return $(this.currentForm).find("[name='" + this.escapeCssMeta(name) + "']");
},
2023-10-04 00:09:17 +01:00
getLength: function (value, element) {
switch (element.nodeName.toLowerCase()) {
2021-08-18 03:06:03 +01:00
case "select":
return $("option:selected", element).length;
case "input":
2023-10-04 00:09:17 +01:00
if (this.checkable(element)) {
2021-08-18 03:06:03 +01:00
return this.findByName(element.name).filter(":checked").length;
}
}
return value.length;
},
2023-10-04 00:09:17 +01:00
depend: function (param, element) {
2021-08-18 03:06:03 +01:00
return this.dependTypes[typeof param] ? this.dependTypes[typeof param](param, element) : true;
},
dependTypes: {
2023-10-04 00:09:17 +01:00
"boolean": function (param) {
2021-08-18 03:06:03 +01:00
return param;
},
2023-10-04 00:09:17 +01:00
"string": function (param, element) {
2021-08-18 03:06:03 +01:00
return !!$(param, element.form).length;
},
2023-10-04 00:09:17 +01:00
"function": function (param, element) {
2021-08-18 03:06:03 +01:00
return param(element);
}
},
2023-10-04 00:09:17 +01:00
optional: function (element) {
2021-08-18 03:06:03 +01:00
var val = this.elementValue(element);
return !$.validator.methods.required.call(this, val, element) && "dependency-mismatch";
},
2023-10-04 00:09:17 +01:00
startRequest: function (element) {
if (!this.pending[element.name]) {
2021-08-18 03:06:03 +01:00
this.pendingRequest++;
$(element).addClass(this.settings.pendingClass);
this.pending[element.name] = true;
}
},
2023-10-04 00:09:17 +01:00
stopRequest: function (element, valid) {
2021-08-18 03:06:03 +01:00
this.pendingRequest--;
// Sometimes synchronization fails, make sure pendingRequest is never < 0
2023-10-04 00:09:17 +01:00
if (this.pendingRequest < 0) {
2021-08-18 03:06:03 +01:00
this.pendingRequest = 0;
}
delete this.pending[element.name];
$(element).removeClass(this.settings.pendingClass);
2023-10-04 00:09:17 +01:00
if (valid && this.pendingRequest === 0 && this.formSubmitted && this.form()) {
2021-08-18 03:06:03 +01:00
$(this.currentForm).submit();
// Remove the hidden input that was used as a replacement for the
// missing submit button. The hidden input is added by `handle()`
// to ensure that the value of the used submit button is passed on
// for scripted submits triggered by this method
2023-10-04 00:09:17 +01:00
if (this.submitButton) {
2021-08-18 03:06:03 +01:00
$("input:hidden[name='" + this.submitButton.name + "']", this.currentForm).remove();
}
this.formSubmitted = false;
2023-10-04 00:09:17 +01:00
} else if (!valid && this.pendingRequest === 0 && this.formSubmitted) {
2021-08-18 03:06:03 +01:00
$(this.currentForm).triggerHandler("invalid-form", [this]);
this.formSubmitted = false;
}
},
2023-10-04 00:09:17 +01:00
previousValue: function (element, method) {
2021-08-18 03:06:03 +01:00
method = typeof method === "string" && method || "remote";
return $.data(element, "previousValue") || $.data(element, "previousValue", {
old: null,
valid: true,
message: this.defaultMessage(element, {method: method})
});
},
// Cleans up all forms and elements, removes validator-specific events
2023-10-04 00:09:17 +01:00
destroy: function () {
2021-08-18 03:06:03 +01:00
this.resetForm();
$(this.currentForm)
.off(".validate")
.removeData("validator")
.find(".validate-equalTo-blur")
.off(".validate-equalTo")
.removeClass("validate-equalTo-blur");
}
},
classRuleSettings: {
required: {required: true},
email: {email: true},
url: {url: true},
date: {date: true},
dateISO: {dateISO: true},
number: {number: true},
digits: {digits: true},
creditcard: {creditcard: true}
},
2023-10-04 00:09:17 +01:00
addClassRules: function (className, rules) {
if (className.constructor === String) {
2021-08-18 03:06:03 +01:00
this.classRuleSettings[className] = rules;
2023-10-04 00:09:17 +01:00
} else {
2021-08-18 03:06:03 +01:00
$.extend(this.classRuleSettings, className);
}
},
2023-10-04 00:09:17 +01:00
classRules: function (element) {
2021-08-18 03:06:03 +01:00
var rules = {},
classes = $(element).attr("class");
2023-10-04 00:09:17 +01:00
if (classes) {
$.each(classes.split(" "), function () {
if (this in $.validator.classRuleSettings) {
2021-08-18 03:06:03 +01:00
$.extend(rules, $.validator.classRuleSettings[this]);
}
});
}
return rules;
},
2023-10-04 00:09:17 +01:00
normalizeAttributeRule: function (rules, type, method, value) {
2021-08-18 03:06:03 +01:00
// Convert the value to a number for number inputs, and for text for backwards compability
// allows type="date" and others to be compared as strings
2023-10-04 00:09:17 +01:00
if (/min|max|step/.test(method) && (type === null || /number|range|text/.test(type))) {
2021-08-18 03:06:03 +01:00
value = Number(value);
// Support Opera Mini, which returns NaN for undefined minlength
2023-10-04 00:09:17 +01:00
if (isNaN(value)) {
2021-08-18 03:06:03 +01:00
value = undefined;
}
}
2023-10-04 00:09:17 +01:00
if (value || value === 0) {
2021-08-18 03:06:03 +01:00
rules[method] = value;
2023-10-04 00:09:17 +01:00
} else if (type === method && type !== "range") {
2021-08-18 03:06:03 +01:00
// Exception: the jquery validate 'range' method
// does not test for the html5 'range' type
rules[method] = true;
}
},
2023-10-04 00:09:17 +01:00
attributeRules: function (element) {
2021-08-18 03:06:03 +01:00
var rules = {},
$element = $(element),
type = element.getAttribute("type"),
method, value;
2023-10-04 00:09:17 +01:00
for (method in $.validator.methods) {
2021-08-18 03:06:03 +01:00
// Support for <input required> in both html5 and older browsers
2023-10-04 00:09:17 +01:00
if (method === "required") {
2021-08-18 03:06:03 +01:00
value = element.getAttribute(method);
// Some browsers return an empty string for the required attribute
// and non-HTML5 browsers might have required="" markup
2023-10-04 00:09:17 +01:00
if (value === "") {
2021-08-18 03:06:03 +01:00
value = true;
}
// Force non-HTML5 browsers to return bool
value = !!value;
2023-10-04 00:09:17 +01:00
} else {
2021-08-18 03:06:03 +01:00
value = $element.attr(method);
}
this.normalizeAttributeRule(rules, type, method, value);
}
// 'maxlength' may be returned as -1, 2147483647 ( IE ) and 524288 ( safari ) for text inputs
2023-10-04 00:09:17 +01:00
if (rules.maxlength && /-1|2147483647|524288/.test(rules.maxlength)) {
2021-08-18 03:06:03 +01:00
delete rules.maxlength;
}
return rules;
},
2023-10-04 00:09:17 +01:00
dataRules: function (element) {
2021-08-18 03:06:03 +01:00
var rules = {},
$element = $(element),
type = element.getAttribute("type"),
method, value;
2023-10-04 00:09:17 +01:00
for (method in $.validator.methods) {
2021-08-18 03:06:03 +01:00
value = $element.data("rule" + method.charAt(0).toUpperCase() + method.substring(1).toLowerCase());
this.normalizeAttributeRule(rules, type, method, value);
}
return rules;
},
2023-10-04 00:09:17 +01:00
staticRules: function (element) {
2021-08-18 03:06:03 +01:00
var rules = {},
validator = $.data(element.form, "validator");
2023-10-04 00:09:17 +01:00
if (validator.settings.rules) {
2021-08-18 03:06:03 +01:00
rules = $.validator.normalizeRule(validator.settings.rules[element.name]) || {};
}
return rules;
},
2023-10-04 00:09:17 +01:00
normalizeRules: function (rules, element) {
2021-08-18 03:06:03 +01:00
// Handle dependency check
2023-10-04 00:09:17 +01:00
$.each(rules, function (prop, val) {
2021-08-18 03:06:03 +01:00
// Ignore rule when param is explicitly false, eg. required:false
2023-10-04 00:09:17 +01:00
if (val === false) {
2021-08-18 03:06:03 +01:00
delete rules[prop];
return;
}
2023-10-04 00:09:17 +01:00
if (val.param || val.depends) {
2021-08-18 03:06:03 +01:00
var keepRule = true;
2023-10-04 00:09:17 +01:00
switch (typeof val.depends) {
2021-08-18 03:06:03 +01:00
case "string":
keepRule = !!$(val.depends, element.form).length;
break;
case "function":
keepRule = val.depends.call(element, element);
break;
}
2023-10-04 00:09:17 +01:00
if (keepRule) {
2021-08-18 03:06:03 +01:00
rules[prop] = val.param !== undefined ? val.param : true;
2023-10-04 00:09:17 +01:00
} else {
2021-08-18 03:06:03 +01:00
$.data(element.form, "validator").resetElements($(element));
delete rules[prop];
}
}
});
// Evaluate parameters
2023-10-04 00:09:17 +01:00
$.each(rules, function (rule, parameter) {
2021-08-18 03:06:03 +01:00
rules[rule] = $.isFunction(parameter) && rule !== "normalizer" ? parameter(element) : parameter;
});
// Clean number parameters
2023-10-04 00:09:17 +01:00
$.each(["minlength", "maxlength"], function () {
if (rules[this]) {
2021-08-18 03:06:03 +01:00
rules[this] = Number(rules[this]);
}
});
2023-10-04 00:09:17 +01:00
$.each(["rangelength", "range"], function () {
2021-08-18 03:06:03 +01:00
var parts;
2023-10-04 00:09:17 +01:00
if (rules[this]) {
if ($.isArray(rules[this])) {
2021-08-18 03:06:03 +01:00
rules[this] = [Number(rules[this][0]), Number(rules[this][1])];
2023-10-04 00:09:17 +01:00
} else if (typeof rules[this] === "string") {
2021-08-18 03:06:03 +01:00
parts = rules[this].replace(/[\[\]]/g, "").split(/[\s,]+/);
rules[this] = [Number(parts[0]), Number(parts[1])];
}
}
});
2023-10-04 00:09:17 +01:00
if ($.validator.autoCreateRanges) {
2021-08-18 03:06:03 +01:00
// Auto-create ranges
2023-10-04 00:09:17 +01:00
if (rules.min != null && rules.max != null) {
2021-08-18 03:06:03 +01:00
rules.range = [rules.min, rules.max];
delete rules.min;
delete rules.max;
}
2023-10-04 00:09:17 +01:00
if (rules.minlength != null && rules.maxlength != null) {
2021-08-18 03:06:03 +01:00
rules.rangelength = [rules.minlength, rules.maxlength];
delete rules.minlength;
delete rules.maxlength;
}
}
return rules;
},
// Converts a simple string to a {string: true} rule, e.g., "required" to {required:true}
2023-10-04 00:09:17 +01:00
normalizeRule: function (data) {
if (typeof data === "string") {
2021-08-18 03:06:03 +01:00
var transformed = {};
2023-10-04 00:09:17 +01:00
$.each(data.split(/\s/), function () {
2021-08-18 03:06:03 +01:00
transformed[this] = true;
});
data = transformed;
}
return data;
},
// https://jqueryvalidation.org/jQuery.validator.addMethod/
2023-10-04 00:09:17 +01:00
addMethod: function (name, method, message) {
2021-08-18 03:06:03 +01:00
$.validator.methods[name] = method;
$.validator.messages[name] = message !== undefined ? message : $.validator.messages[name];
2023-10-04 00:09:17 +01:00
if (method.length < 3) {
2021-08-18 03:06:03 +01:00
$.validator.addClassRules(name, $.validator.normalizeRule(name));
}
},
// https://jqueryvalidation.org/jQuery.validator.methods/
methods: {
// https://jqueryvalidation.org/required-method/
2023-10-04 00:09:17 +01:00
required: function (value, element, param) {
2021-08-18 03:06:03 +01:00
// Check if dependency is met
2023-10-04 00:09:17 +01:00
if (!this.depend(param, element)) {
2021-08-18 03:06:03 +01:00
return "dependency-mismatch";
}
2023-10-04 00:09:17 +01:00
if (element.nodeName.toLowerCase() === "select") {
2021-08-18 03:06:03 +01:00
// Could be an array for select-multiple or a string, both are fine this way
var val = $(element).val();
return val && val.length > 0;
}
2023-10-04 00:09:17 +01:00
if (this.checkable(element)) {
2021-08-18 03:06:03 +01:00
return this.getLength(value, element) > 0;
}
return value.length > 0;
},
// https://jqueryvalidation.org/email-method/
2023-10-04 00:09:17 +01:00
email: function (value, element) {
2021-08-18 03:06:03 +01:00
// From https://html.spec.whatwg.org/multipage/forms.html#valid-e-mail-address
// Retrieved 2014-01-14
// If you have a problem with this implementation, report a bug against the above spec
// Or use custom methods to implement your own email validation
return this.optional(element) || /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.test(value);
},
// https://jqueryvalidation.org/url-method/
2023-10-04 00:09:17 +01:00
url: function (value, element) {
2021-08-18 03:06:03 +01:00
// Copyright (c) 2010-2013 Diego Perini, MIT licensed
// https://gist.github.com/dperini/729294
// see also https://mathiasbynens.be/demo/url-regex
// modified to allow protocol-relative URLs
return this.optional(element) || /^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})).?)(?::\d{2,5})?(?:[/?#]\S*)?$/i.test(value);
},
// https://jqueryvalidation.org/date-method/
2023-10-04 00:09:17 +01:00
date: function (value, element) {
2021-08-18 03:06:03 +01:00
return this.optional(element) || !/Invalid|NaN/.test(new Date(value).toString());
},
// https://jqueryvalidation.org/dateISO-method/
2023-10-04 00:09:17 +01:00
dateISO: function (value, element) {
2021-08-18 03:06:03 +01:00
return this.optional(element) || /^\d{4}[\/\-](0?[1-9]|1[012])[\/\-](0?[1-9]|[12][0-9]|3[01])$/.test(value);
},
// https://jqueryvalidation.org/number-method/
2023-10-04 00:09:17 +01:00
number: function (value, element) {
2021-08-18 03:06:03 +01:00
return this.optional(element) || /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/.test(value);
},
// https://jqueryvalidation.org/digits-method/
2023-10-04 00:09:17 +01:00
digits: function (value, element) {
2021-08-18 03:06:03 +01:00
return this.optional(element) || /^\d+$/.test(value);
},
// https://jqueryvalidation.org/minlength-method/
2023-10-04 00:09:17 +01:00
minlength: function (value, element, param) {
2021-08-18 03:06:03 +01:00
var length = $.isArray(value) ? value.length : this.getLength(value, element);
return this.optional(element) || length >= param;
},
// https://jqueryvalidation.org/maxlength-method/
2023-10-04 00:09:17 +01:00
maxlength: function (value, element, param) {
2021-08-18 03:06:03 +01:00
var length = $.isArray(value) ? value.length : this.getLength(value, element);
return this.optional(element) || length <= param;
},
// https://jqueryvalidation.org/rangelength-method/
2023-10-04 00:09:17 +01:00
rangelength: function (value, element, param) {
2021-08-18 03:06:03 +01:00
var length = $.isArray(value) ? value.length : this.getLength(value, element);
return this.optional(element) || (length >= param[0] && length <= param[1]);
},
// https://jqueryvalidation.org/min-method/
2023-10-04 00:09:17 +01:00
min: function (value, element, param) {
2021-08-18 03:06:03 +01:00
return this.optional(element) || value >= param;
},
// https://jqueryvalidation.org/max-method/
2023-10-04 00:09:17 +01:00
max: function (value, element, param) {
2021-08-18 03:06:03 +01:00
return this.optional(element) || value <= param;
},
// https://jqueryvalidation.org/range-method/
2023-10-04 00:09:17 +01:00
range: function (value, element, param) {
2021-08-18 03:06:03 +01:00
return this.optional(element) || (value >= param[0] && value <= param[1]);
},
// https://jqueryvalidation.org/step-method/
2023-10-04 00:09:17 +01:00
step: function (value, element, param) {
2021-08-18 03:06:03 +01:00
var type = $(element).attr("type"),
errorMessage = "Step attribute on input type " + type + " is not supported.",
supportedTypes = ["text", "number", "range"],
re = new RegExp("\\b" + type + "\\b"),
notSupported = type && !re.test(supportedTypes.join()),
2023-10-04 00:09:17 +01:00
decimalPlaces = function (num) {
2021-08-18 03:06:03 +01:00
var match = ("" + num).match(/(?:\.(\d+))?$/);
2023-10-04 00:09:17 +01:00
if (!match) {
2021-08-18 03:06:03 +01:00
return 0;
}
// Number of digits right of decimal point.
return match[1] ? match[1].length : 0;
},
2023-10-04 00:09:17 +01:00
toInt = function (num) {
2021-08-18 03:06:03 +01:00
return Math.round(num * Math.pow(10, decimals));
},
valid = true,
decimals;
// Works only for text, number and range input types
// TODO find a way to support input types date, datetime, datetime-local, month, time and week
2023-10-04 00:09:17 +01:00
if (notSupported) {
2021-08-18 03:06:03 +01:00
throw new Error(errorMessage);
}
decimals = decimalPlaces(param);
// Value can't have too many decimals
2023-10-04 00:09:17 +01:00
if (decimalPlaces(value) > decimals || toInt(value) % toInt(param) !== 0) {
2021-08-18 03:06:03 +01:00
valid = false;
}
return this.optional(element) || valid;
},
// https://jqueryvalidation.org/equalTo-method/
2023-10-04 00:09:17 +01:00
equalTo: function (value, element, param) {
2021-08-18 03:06:03 +01:00
// Bind to the blur event of the target in order to revalidate whenever the target field is updated
var target = $(param);
2023-10-04 00:09:17 +01:00
if (this.settings.onfocusout && target.not(".validate-equalTo-blur").length) {
target.addClass("validate-equalTo-blur").on("blur.validate-equalTo", function () {
2021-08-18 03:06:03 +01:00
$(element).valid();
});
}
return value === target.val();
},
// https://jqueryvalidation.org/remote-method/
2023-10-04 00:09:17 +01:00
remote: function (value, element, param, method) {
if (this.optional(element)) {
2021-08-18 03:06:03 +01:00
return "dependency-mismatch";
}
method = typeof method === "string" && method || "remote";
var previous = this.previousValue(element, method),
validator, data, optionDataString;
2023-10-04 00:09:17 +01:00
if (!this.settings.messages[element.name]) {
2021-08-18 03:06:03 +01:00
this.settings.messages[element.name] = {};
}
previous.originalMessage = previous.originalMessage || this.settings.messages[element.name][method];
this.settings.messages[element.name][method] = previous.message;
param = typeof param === "string" && {url: param} || param;
optionDataString = $.param($.extend({data: value}, param.data));
2023-10-04 00:09:17 +01:00
if (previous.old === optionDataString) {
2021-08-18 03:06:03 +01:00
return previous.valid;
}
previous.old = optionDataString;
validator = this;
this.startRequest(element);
data = {};
data[element.name] = value;
$.ajax($.extend(true, {
mode: "abort",
port: "validate" + element.name,
dataType: "json",
data: data,
context: validator.currentForm,
2023-10-04 00:09:17 +01:00
success: function (response) {
2021-08-18 03:06:03 +01:00
var valid = response === true || response === "true",
errors, message, submitted;
validator.settings.messages[element.name][method] = previous.originalMessage;
2023-10-04 00:09:17 +01:00
if (valid) {
2021-08-18 03:06:03 +01:00
submitted = validator.formSubmitted;
validator.resetInternals();
validator.toHide = validator.errorsFor(element);
validator.formSubmitted = submitted;
validator.successList.push(element);
validator.invalid[element.name] = false;
validator.showErrors();
2023-10-04 00:09:17 +01:00
} else {
2021-08-18 03:06:03 +01:00
errors = {};
message = response || validator.defaultMessage(element, {
method: method,
parameters: value
});
errors[element.name] = previous.message = message;
validator.invalid[element.name] = true;
validator.showErrors(errors);
}
previous.valid = valid;
validator.stopRequest(element, valid);
}
}, param));
return "pending";
}
}
});
// Ajax mode: abort
// usage: $.ajax({ mode: "abort"[, port: "uniqueport"]});
// if mode:"abort" is used, the previous request on that port (port can be undefined) is aborted via XMLHttpRequest.abort()
2021-08-18 03:06:03 +01:00
var pendingRequests = {},
ajax;
// Use a prefilter if available (1.5+)
2023-10-04 00:09:17 +01:00
if ($.ajaxPrefilter) {
$.ajaxPrefilter(function (settings, _, xhr) {
2021-08-18 03:06:03 +01:00
var port = settings.port;
2023-10-04 00:09:17 +01:00
if (settings.mode === "abort") {
if (pendingRequests[port]) {
2021-08-18 03:06:03 +01:00
pendingRequests[port].abort();
}
pendingRequests[port] = xhr;
}
});
2023-10-04 00:09:17 +01:00
} else {
2021-08-18 03:06:03 +01:00
// Proxy ajax
ajax = $.ajax;
2023-10-04 00:09:17 +01:00
$.ajax = function (settings) {
2021-08-18 03:06:03 +01:00
var mode = ("mode" in settings ? settings : $.ajaxSettings).mode,
port = ("port" in settings ? settings : $.ajaxSettings).port;
2023-10-04 00:09:17 +01:00
if (mode === "abort") {
if (pendingRequests[port]) {
2021-08-18 03:06:03 +01:00
pendingRequests[port].abort();
}
pendingRequests[port] = ajax.apply(this, arguments);
return pendingRequests[port];
}
return ajax.apply(this, arguments);
};
}
return $;
}));