/*!
* jquery validation plugin v1.19.3
*
* https://jqueryvalidation.org/
*
* copyright (c) 2021 jörn zaefferer
* released under the mit license
*/
(function( factory ) {
if ( typeof define === "function" && define.amd ) {
define( ["jquery"], factory );
} else if (typeof module === "object" && module.exports) {
module.exports = factory( require( "jquery" ) );
} else {
factory( jquery );
}
}(function( $ ) {
$.extend( $.fn, {
// https://jqueryvalidation.org/validate/
validate: function( options ) {
// if nothing is selected, return nothing; can't chain anyway
if ( !this.length ) {
if ( options && options.debug && window.console ) {
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" );
if ( validator ) {
return validator;
}
// add novalidate tag if html5.
this.attr( "novalidate", "novalidate" );
validator = new $.validator( options, this[ 0 ] );
$.data( this[ 0 ], "validator", validator );
if ( validator.settings.onsubmit ) {
this.on( "click.validate", ":submit", function( event ) {
// 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
if ( $( this ).hasclass( "cancel" ) ) {
validator.cancelsubmit = true;
}
// allow suppressing validation by adding the html5 formnovalidate attribute to the submit button
if ( $( this ).attr( "formnovalidate" ) !== undefined ) {
validator.cancelsubmit = true;
}
} );
// validate the form on submit
this.on( "submit.validate", function( event ) {
if ( validator.settings.debug ) {
// prevent form submit to be able to see console output
event.preventdefault();
}
function handle() {
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
if ( validator.submitbutton && ( validator.settings.submithandler || validator.formsubmitted ) ) {
hidden = $( "" )
.attr( "name", validator.submitbutton.name )
.val( $( validator.submitbutton ).val() )
.appendto( validator.currentform );
}
if ( validator.settings.submithandler && !validator.settings.debug ) {
result = validator.settings.submithandler.call( validator, validator.currentform, event );
if ( hidden ) {
// and clean up afterwards; thanks to no-block-scope, hidden can be referenced
hidden.remove();
}
if ( result !== undefined ) {
return result;
}
return false;
}
return true;
}
// prevent submit for invalid forms or custom submit handlers
if ( validator.cancelsubmit ) {
validator.cancelsubmit = false;
return handle();
}
if ( validator.form() ) {
if ( validator.pendingrequest ) {
validator.formsubmitted = true;
return false;
}
return handle();
} else {
validator.focusinvalid();
return false;
}
} );
}
return validator;
},
// https://jqueryvalidation.org/valid/
valid: function() {
var valid, validator, errorlist;
if ( $( this[ 0 ] ).is( "form" ) ) {
valid = this.validate().form();
} else {
errorlist = [];
valid = true;
validator = $( this[ 0 ].form ).validate();
this.each( function() {
valid = validator.element( this ) && valid;
if ( !valid ) {
errorlist = errorlist.concat( validator.errorlist );
}
} );
validator.errorlist = errorlist;
}
return valid;
},
// https://jqueryvalidation.org/rules/
rules: function( command, argument ) {
var element = this[ 0 ],
iscontenteditable = typeof this.attr( "contenteditable" ) !== "undefined" && this.attr( "contenteditable" ) !== "false",
settings, staticrules, existingrules, data, param, filtered;
// if nothing is selected, return empty object; can't chain anyway
if ( element == null ) {
return;
}
if ( !element.form && iscontenteditable ) {
element.form = this.closest( "form" )[ 0 ];
element.name = this.attr( "name" );
}
if ( element.form == null ) {
return;
}
if ( command ) {
settings = $.data( element.form, "validator" ).settings;
staticrules = settings.rules;
existingrules = $.validator.staticrules( element );
switch ( command ) {
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;
if ( argument.messages ) {
settings.messages[ element.name ] = $.extend( settings.messages[ element.name ], argument.messages );
}
break;
case "remove":
if ( !argument ) {
delete staticrules[ element.name ];
return existingrules;
}
filtered = {};
$.each( argument.split( /\s/ ), function( index, method ) {
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
if ( data.required ) {
param = data.required;
delete data.required;
data = $.extend( { required: param }, data );
}
// make sure remote is at back
if ( data.remote ) {
param = data.remote;
delete data.remote;
data = $.extend( data, { remote: param } );
}
return data;
}
} );
// jquery trim is deprecated, provide a trim method based on string.prototype.trim
var trim = function( str ) {
// https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/string/trim#polyfill
return str.replace( /^[\s\ufeff\xa0]+|[\s\ufeff\xa0]+$/g, "" );
};
// custom selectors
$.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/
blank: function( a ) {
return !trim( "" + $( a ).val() );
},
// https://jqueryvalidation.org/filled-selector/
filled: function( a ) {
var val = $( a ).val();
return val !== null && !!trim( "" + val );
},
// https://jqueryvalidation.org/unchecked-selector/
unchecked: function( a ) {
return !$( a ).prop( "checked" );
}
} );
// constructor for validator
$.validator = function( options, form ) {
this.settings = $.extend( true, {}, $.validator.defaults, options );
this.currentform = form;
this.init();
};
// https://jqueryvalidation.org/jquery.validator.format/
$.validator.format = function( source, params ) {
if ( arguments.length === 1 ) {
return function() {
var args = $.makearray( arguments );
args.unshift( source );
return $.validator.format.apply( this, args );
};
}
if ( params === undefined ) {
return source;
}
if ( arguments.length > 2 && params.constructor !== array ) {
params = $.makearray( arguments ).slice( 1 );
}
if ( params.constructor !== array ) {
params = [ params ];
}
$.each( params, function( i, n ) {
source = source.replace( new regexp( "\\{" + i + "\\}", "g" ), function() {
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,
onfocusin: function( element ) {
this.lastactive = element;
// hide error label and remove error class on focus if enabled
if ( this.settings.focuscleanup ) {
if ( this.settings.unhighlight ) {
this.settings.unhighlight.call( this, element, this.settings.errorclass, this.settings.validclass );
}
this.hidethese( this.errorsfor( element ) );
}
},
onfocusout: function( element ) {
if ( !this.checkable( element ) && ( element.name in this.submitted || !this.optional( element ) ) ) {
this.element( element );
}
},
onkeyup: function( element, event ) {
// 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
];
if ( event.which === 9 && this.elementvalue( element ) === "" || $.inarray( event.keycode, excludedkeys ) !== -1 ) {
return;
} else if ( element.name in this.submitted || element.name in this.invalid ) {
this.element( element );
}
},
onclick: function( element ) {
// click on selects, radiobuttons and checkboxes
if ( element.name in this.submitted ) {
this.element( element );
// or option elements, check parent select in that case
} else if ( element.parentnode.name in this.submitted ) {
this.element( element.parentnode );
}
},
highlight: function( element, errorclass, validclass ) {
if ( element.type === "radio" ) {
this.findbyname( element.name ).addclass( errorclass ).removeclass( validclass );
} else {
$( element ).addclass( errorclass ).removeclass( validclass );
}
},
unhighlight: function( element, errorclass, validclass ) {
if ( element.type === "radio" ) {
this.findbyname( element.name ).removeclass( errorclass ).addclass( validclass );
} else {
$( element ).removeclass( errorclass ).addclass( validclass );
}
}
},
// https://jqueryvalidation.org/jquery.validator.setdefaults/
setdefaults: function( settings ) {
$.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: {
init: function() {
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 currentform = this.currentform,
groups = ( this.groups = {} ),
rules;
$.each( this.settings.groups, function( key, value ) {
if ( typeof value === "string" ) {
value = value.split( /\s/ );
}
$.each( value, function( index, name ) {
groups[ name ] = key;
} );
} );
rules = this.settings.rules;
$.each( rules, function( key, value ) {
rules[ key ] = $.validator.normalizerule( value );
} );
function delegate( event ) {
var iscontenteditable = typeof $( this ).attr( "contenteditable" ) !== "undefined" && $( this ).attr( "contenteditable" ) !== "false";
// set form expando on contenteditable
if ( !this.form && iscontenteditable ) {
this.form = $( this ).closest( "form" )[ 0 ];
this.name = $( this ).attr( "name" );
}
// ignore the element if it belongs to another form. this will happen mainly
// when setting the `form` attribute of an input to the id of another form.
if ( currentform !== this.form ) {
return;
}
var validator = $.data( this.form, "validator" ),
eventtype = "on" + event.type.replace( /^validate/, "" ),
settings = validator.settings;
if ( settings[ eventtype ] && !$( this ).is( settings.ignore ) ) {
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 );
if ( this.settings.invalidhandler ) {
$( this.currentform ).on( "invalid-form.validate", this.settings.invalidhandler );
}
},
// https://jqueryvalidation.org/validator.form/
form: function() {
this.checkform();
$.extend( this.submitted, this.errormap );
this.invalid = $.extend( {}, this.errormap );
if ( !this.valid() ) {
$( this.currentform ).triggerhandler( "invalid-form", [ this ] );
}
this.showerrors();
return this.valid();
},
checkform: function() {
this.prepareform();
for ( var i = 0, elements = ( this.currentelements = this.elements() ); elements[ i ]; i++ ) {
this.check( elements[ i ] );
}
return this.valid();
},
// https://jqueryvalidation.org/validator.element/
element: function( element ) {
var cleanelement = this.clean( element ),
checkelement = this.validationtargetfor( cleanelement ),
v = this,
result = true,
rs, group;
if ( checkelement === undefined ) {
delete this.invalid[ cleanelement.name ];
} else {
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 ];
if ( group ) {
$.each( this.groups, function( name, testgroup ) {
if ( testgroup === group && name !== checkelement.name ) {
cleanelement = v.validationtargetfor( v.clean( v.findbyname( name ) ) );
if ( cleanelement && cleanelement.name in v.invalid ) {
v.currentelements.push( cleanelement );
result = v.check( cleanelement ) && result;
}
}
} );
}
rs = this.check( checkelement ) !== false;
result = result && rs;
if ( rs ) {
this.invalid[ checkelement.name ] = false;
} else {
this.invalid[ checkelement.name ] = true;
}
if ( !this.numberofinvalids() ) {
// 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/
showerrors: function( errors ) {
if ( errors ) {
var validator = this;
// add items to error list and map
$.extend( this.errormap, errors );
this.errorlist = $.map( this.errormap, function( message, name ) {
return {
message: message,
element: validator.findbyname( name )[ 0 ]
};
} );
// remove items from success list
this.successlist = $.grep( this.successlist, function( element ) {
return !( element.name in errors );
} );
}
if ( this.settings.showerrors ) {
this.settings.showerrors.call( this, this.errormap, this.errorlist );
} else {
this.defaultshowerrors();
}
},
// https://jqueryvalidation.org/validator.resetform/
resetform: function() {
if ( $.fn.resetform ) {
$( this.currentform ).resetform();
}
this.invalid = {};
this.submitted = {};
this.prepareform();
this.hideerrors();
var elements = this.elements()
.removedata( "previousvalue" )
.removeattr( "aria-invalid" );
this.resetelements( elements );
},
resetelements: function( elements ) {
var i;
if ( this.settings.unhighlight ) {
for ( i = 0; elements[ i ]; i++ ) {
this.settings.unhighlight.call( this, elements[ i ],
this.settings.errorclass, "" );
this.findbyname( elements[ i ].name ).removeclass( this.settings.validclass );
}
} else {
elements
.removeclass( this.settings.errorclass )
.removeclass( this.settings.validclass );
}
},
numberofinvalids: function() {
return this.objectlength( this.invalid );
},
objectlength: function( obj ) {
/* jshint unused: false */
var count = 0,
i;
for ( i in obj ) {
// this check allows counting elements with empty error
// message as invalid elements
if ( obj[ i ] !== undefined && obj[ i ] !== null && obj[ i ] !== false ) {
count++;
}
}
return count;
},
hideerrors: function() {
this.hidethese( this.tohide );
},
hidethese: function( errors ) {
errors.not( this.containers ).text( "" );
this.addwrapper( errors ).hide();
},
valid: function() {
return this.size() === 0;
},
size: function() {
return this.errorlist.length;
},
focusinvalid: function() {
if ( this.settings.focusinvalid ) {
try {
$( this.findlastactive() || this.errorlist.length && this.errorlist[ 0 ].element || [] )
.filter( ":visible" )
.trigger( "focus" )
// manually trigger focusin event; without it, focusin handler isn't called, findlastactive won't have anything to find
.trigger( "focusin" );
} catch ( e ) {
// ignore ie throwing errors when focusing hidden elements
}
}
},
findlastactive: function() {
var lastactive = this.lastactive;
return lastactive && $.grep( this.errorlist, function( n ) {
return n.element.name === lastactive.name;
} ).length === 1 && lastactive;
},
elements: function() {
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 )
.filter( function() {
var name = this.name || $( this ).attr( "name" ); // for contenteditable
var iscontenteditable = typeof $( this ).attr( "contenteditable" ) !== "undefined" && $( this ).attr( "contenteditable" ) !== "false";
if ( !name && validator.settings.debug && window.console ) {
console.error( "%o has no name assigned", this );
}
// set form expando on contenteditable
if ( iscontenteditable ) {
this.form = $( this ).closest( "form" )[ 0 ];
this.name = name;
}
// ignore elements that belong to other/nested forms
if ( this.form !== validator.currentform ) {
return false;
}
// select only the first element for each name, and only those with rules specified
if ( name in rulescache || !validator.objectlength( $( this ).rules() ) ) {
return false;
}
rulescache[ name ] = true;
return true;
} );
},
clean: function( selector ) {
return $( selector )[ 0 ];
},
errors: function() {
var errorclass = this.settings.errorclass.split( " " ).join( "." );
return $( this.settings.errorelement + "." + errorclass, this.errorcontext );
},
resetinternals: function() {
this.successlist = [];
this.errorlist = [];
this.errormap = {};
this.toshow = $( [] );
this.tohide = $( [] );
},
reset: function() {
this.resetinternals();
this.currentelements = $( [] );
},
prepareform: function() {
this.reset();
this.tohide = this.errors().add( this.containers );
},
prepareelement: function( element ) {
this.reset();
this.tohide = this.errorsfor( element );
},
elementvalue: function( element ) {
var $element = $( element ),
type = element.type,
iscontenteditable = typeof $element.attr( "contenteditable" ) !== "undefined" && $element.attr( "contenteditable" ) !== "false",
val, idx;
if ( type === "radio" || type === "checkbox" ) {
return this.findbyname( element.name ).filter( ":checked" ).val();
} else if ( type === "number" && typeof element.validity !== "undefined" ) {
return element.validity.badinput ? "nan" : $element.val();
}
if ( iscontenteditable ) {
val = $element.text();
} else {
val = $element.val();
}
if ( type === "file" ) {
// modern browser (chrome & safari)
if ( val.substr( 0, 12 ) === "c:\\fakepath\\" ) {
return val.substr( 12 );
}
// legacy browsers
// unix-based path
idx = val.lastindexof( "/" );
if ( idx >= 0 ) {
return val.substr( idx + 1 );
}
// windows-based path
idx = val.lastindexof( "\\" );
if ( idx >= 0 ) {
return val.substr( idx + 1 );
}
// just the file name
return val;
}
if ( typeof val === "string" ) {
return val.replace( /\r/g, "" );
}
return val;
},
check: function( element ) {
element = this.validationtargetfor( this.clean( element ) );
var rules = $( element ).rules(),
rulescount = $.map( rules, function( n, i ) {
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.
if ( typeof rules.normalizer === "function" ) {
normalizer = rules.normalizer;
} else if ( typeof this.settings.normalizer === "function" ) {
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`.
if ( normalizer ) {
val = normalizer.call( element, val );
// delete the normalizer from rules to avoid treating it as a pre-defined method.
delete rules.normalizer;
}
for ( method in rules ) {
rule = { method: method, parameters: rules[ method ] };
try {
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
if ( result === "dependency-mismatch" && rulescount === 1 ) {
dependencymismatch = true;
continue;
}
dependencymismatch = false;
if ( result === "pending" ) {
this.tohide = this.tohide.not( this.errorsfor( element ) );
return;
}
if ( !result ) {
this.formatandadd( element, rule );
return false;
}
} catch ( e ) {
if ( this.settings.debug && window.console ) {
console.log( "exception occurred when checking element " + element.id + ", check the '" + rule.method + "' method.", e );
}
if ( e instanceof typeerror ) {
e.message += ". exception occurred when checking element " + element.id + ", check the '" + rule.method + "' method.";
}
throw e;
}
}
if ( dependencymismatch ) {
return;
}
if ( this.objectlength( rules ) ) {
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
customdatamessage: function( element, method ) {
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
custommessage: function( name, method ) {
var m = this.settings.messages[ name ];
return m && ( m.constructor === string ? m : m[ method ] );
},
// return the first defined argument, allowing empty strings
finddefined: function() {
for ( var i = 0; i < arguments.length; i++ ) {
if ( arguments[ i ] !== undefined ) {
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.
defaultmessage: function( element, rule ) {
if ( typeof rule === "string" ) {
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 ],
"warning: no message defined for " + element.name + ""
),
theregex = /\$?\{(\d+)\}/g;
if ( typeof message === "function" ) {
message = message.call( this, rule.parameters, element );
} else if ( theregex.test( message ) ) {
message = $.validator.format( message.replace( theregex, "{$1}" ), rule.parameters );
}
return message;
},
formatandadd: function( element, rule ) {
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;
},
addwrapper: function( totoggle ) {
if ( this.settings.wrapper ) {
totoggle = totoggle.add( totoggle.parent( this.settings.wrapper ) );
}
return totoggle;
},
defaultshowerrors: function() {
var i, elements, error;
for ( i = 0; this.errorlist[ i ]; i++ ) {
error = this.errorlist[ i ];
if ( this.settings.highlight ) {
this.settings.highlight.call( this, error.element, this.settings.errorclass, this.settings.validclass );
}
this.showlabel( error.element, error.message );
}
if ( this.errorlist.length ) {
this.toshow = this.toshow.add( this.containers );
}
if ( this.settings.success ) {
for ( i = 0; this.successlist[ i ]; i++ ) {
this.showlabel( this.successlist[ i ] );
}
}
if ( this.settings.unhighlight ) {
for ( i = 0, elements = this.validelements(); elements[ i ]; i++ ) {
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();
},
validelements: function() {
return this.currentelements.not( this.invalidelements() );
},
invalidelements: function() {
return $( this.errorlist ).map( function() {
return this.element;
} );
},
showlabel: function( element, message ) {
var place, group, errorid, v,
error = this.errorsfor( element ),
elementid = this.idorname( element ),
describedby = $( element ).attr( "aria-describedby" );
if ( error.length ) {
// refresh error/success class
error.removeclass( this.settings.validclass ).addclass( this.settings.errorclass );
// replace message on existing label
error.html( message );
} else {
// 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;
if ( this.settings.wrapper ) {
// 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();
}
if ( this.labelcontainer.length ) {
this.labelcontainer.append( place );
} else if ( this.settings.errorplacement ) {
this.settings.errorplacement.call( this, place, $( element ) );
} else {
place.insertafter( element );
}
// link error back to the element
if ( error.is( "label" ) ) {
// 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
} else if ( error.parents( "label[for='" + this.escapecssmeta( elementid ) + "']" ).length === 0 ) {
errorid = error.attr( "id" );
// respect existing non-error aria-describedby
if ( !describedby ) {
describedby = errorid;
} else if ( !describedby.match( new regexp( "\\b" + this.escapecssmeta( errorid ) + "\\b" ) ) ) {
// 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 ];
if ( group ) {
v = this;
$.each( v.groups, function( name, testgroup ) {
if ( testgroup === group ) {
$( "[name='" + v.escapecssmeta( name ) + "']", v.currentform )
.attr( "aria-describedby", error.attr( "id" ) );
}
} );
}
}
}
if ( !message && this.settings.success ) {
error.text( "" );
if ( typeof this.settings.success === "string" ) {
error.addclass( this.settings.success );
} else {
this.settings.success( error, element );
}
}
this.toshow = this.toshow.add( error );
},
errorsfor: function( element ) {
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
if ( describer ) {
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.
escapecssmeta: function( string ) {
return string.replace( /([\\!"#$%&'()*+,./:;<=>?@\[\]^`{|}~])/g, "\\$1" );
},
idorname: function( element ) {
return this.groups[ element.name ] || ( this.checkable( element ) ? element.name : element.id || element.name );
},
validationtargetfor: function( element ) {
// if radio/checkbox, validate first element in group instead
if ( this.checkable( element ) ) {
element = this.findbyname( element.name );
}
// always apply ignore filter
return $( element ).not( this.settings.ignore )[ 0 ];
},
checkable: function( element ) {
return ( /radio|checkbox/i ).test( element.type );
},
findbyname: function( name ) {
return $( this.currentform ).find( "[name='" + this.escapecssmeta( name ) + "']" );
},
getlength: function( value, element ) {
switch ( element.nodename.tolowercase() ) {
case "select":
return $( "option:selected", element ).length;
case "input":
if ( this.checkable( element ) ) {
return this.findbyname( element.name ).filter( ":checked" ).length;
}
}
return value.length;
},
depend: function( param, element ) {
return this.dependtypes[ typeof param ] ? this.dependtypes[ typeof param ]( param, element ) : true;
},
dependtypes: {
"boolean": function( param ) {
return param;
},
"string": function( param, element ) {
return !!$( param, element.form ).length;
},
"function": function( param, element ) {
return param( element );
}
},
optional: function( element ) {
var val = this.elementvalue( element );
return !$.validator.methods.required.call( this, val, element ) && "dependency-mismatch";
},
startrequest: function( element ) {
if ( !this.pending[ element.name ] ) {
this.pendingrequest++;
$( element ).addclass( this.settings.pendingclass );
this.pending[ element.name ] = true;
}
},
stoprequest: function( element, valid ) {
this.pendingrequest--;
// sometimes synchronization fails, make sure pendingrequest is never < 0
if ( this.pendingrequest < 0 ) {
this.pendingrequest = 0;
}
delete this.pending[ element.name ];
$( element ).removeclass( this.settings.pendingclass );
if ( valid && this.pendingrequest === 0 && this.formsubmitted && this.form() ) {
$( 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
if ( this.submitbutton ) {
$( "input:hidden[name='" + this.submitbutton.name + "']", this.currentform ).remove();
}
this.formsubmitted = false;
} else if ( !valid && this.pendingrequest === 0 && this.formsubmitted ) {
$( this.currentform ).triggerhandler( "invalid-form", [ this ] );
this.formsubmitted = false;
}
},
previousvalue: function( element, method ) {
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
destroy: function() {
this.resetform();
$( this.currentform )
.off( ".validate" )
.removedata( "validator" )
.find( ".validate-equalto-blur" )
.off( ".validate-equalto" )
.removeclass( "validate-equalto-blur" )
.find( ".validate-lessthan-blur" )
.off( ".validate-lessthan" )
.removeclass( "validate-lessthan-blur" )
.find( ".validate-lessthanequal-blur" )
.off( ".validate-lessthanequal" )
.removeclass( "validate-lessthanequal-blur" )
.find( ".validate-greaterthanequal-blur" )
.off( ".validate-greaterthanequal" )
.removeclass( "validate-greaterthanequal-blur" )
.find( ".validate-greaterthan-blur" )
.off( ".validate-greaterthan" )
.removeclass( "validate-greaterthan-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 }
},
addclassrules: function( classname, rules ) {
if ( classname.constructor === string ) {
this.classrulesettings[ classname ] = rules;
} else {
$.extend( this.classrulesettings, classname );
}
},
classrules: function( element ) {
var rules = {},
classes = $( element ).attr( "class" );
if ( classes ) {
$.each( classes.split( " " ), function() {
if ( this in $.validator.classrulesettings ) {
$.extend( rules, $.validator.classrulesettings[ this ] );
}
} );
}
return rules;
},
normalizeattributerule: function( rules, type, method, value ) {
// 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
if ( /min|max|step/.test( method ) && ( type === null || /number|range|text/.test( type ) ) ) {
value = number( value );
// support opera mini, which returns nan for undefined minlength
if ( isnan( value ) ) {
value = undefined;
}
}
if ( value || value === 0 ) {
rules[ method ] = value;
} else if ( type === method && type !== "range" ) {
// exception: the jquery validate 'range' method
// does not test for the html5 'range' type
rules[ method ] = true;
}
},
attributerules: function( element ) {
var rules = {},
$element = $( element ),
type = element.getattribute( "type" ),
method, value;
for ( method in $.validator.methods ) {
// support for in both html5 and older browsers
if ( method === "required" ) {
value = element.getattribute( method );
// some browsers return an empty string for the required attribute
// and non-html5 browsers might have required="" markup
if ( value === "" ) {
value = true;
}
// force non-html5 browsers to return bool
value = !!value;
} else {
value = $element.attr( method );
}
this.normalizeattributerule( rules, type, method, value );
}
// 'maxlength' may be returned as -1, 2147483647 ( ie ) and 524288 ( safari ) for text inputs
if ( rules.maxlength && /-1|2147483647|524288/.test( rules.maxlength ) ) {
delete rules.maxlength;
}
return rules;
},
datarules: function( element ) {
var rules = {},
$element = $( element ),
type = element.getattribute( "type" ),
method, value;
for ( method in $.validator.methods ) {
value = $element.data( "rule" + method.charat( 0 ).touppercase() + method.substring( 1 ).tolowercase() );
// cast empty attributes like `data-rule-required` to `true`
if ( value === "" ) {
value = true;
}
this.normalizeattributerule( rules, type, method, value );
}
return rules;
},
staticrules: function( element ) {
var rules = {},
validator = $.data( element.form, "validator" );
if ( validator.settings.rules ) {
rules = $.validator.normalizerule( validator.settings.rules[ element.name ] ) || {};
}
return rules;
},
normalizerules: function( rules, element ) {
// handle dependency check
$.each( rules, function( prop, val ) {
// ignore rule when param is explicitly false, eg. required:false
if ( val === false ) {
delete rules[ prop ];
return;
}
if ( val.param || val.depends ) {
var keeprule = true;
switch ( typeof val.depends ) {
case "string":
keeprule = !!$( val.depends, element.form ).length;
break;
case "function":
keeprule = val.depends.call( element, element );
break;
}
if ( keeprule ) {
rules[ prop ] = val.param !== undefined ? val.param : true;
} else {
$.data( element.form, "validator" ).resetelements( $( element ) );
delete rules[ prop ];
}
}
} );
// evaluate parameters
$.each( rules, function( rule, parameter ) {
rules[ rule ] = typeof parameter === "function" && rule !== "normalizer" ? parameter( element ) : parameter;
} );
// clean number parameters
$.each( [ "minlength", "maxlength" ], function() {
if ( rules[ this ] ) {
rules[ this ] = number( rules[ this ] );
}
} );
$.each( [ "rangelength", "range" ], function() {
var parts;
if ( rules[ this ] ) {
if ( array.isarray( rules[ this ] ) ) {
rules[ this ] = [ number( rules[ this ][ 0 ] ), number( rules[ this ][ 1 ] ) ];
} else if ( typeof rules[ this ] === "string" ) {
parts = rules[ this ].replace( /[\[\]]/g, "" ).split( /[\s,]+/ );
rules[ this ] = [ number( parts[ 0 ] ), number( parts[ 1 ] ) ];
}
}
} );
if ( $.validator.autocreateranges ) {
// auto-create ranges
if ( rules.min != null && rules.max != null ) {
rules.range = [ rules.min, rules.max ];
delete rules.min;
delete rules.max;
}
if ( rules.minlength != null && rules.maxlength != null ) {
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}
normalizerule: function( data ) {
if ( typeof data === "string" ) {
var transformed = {};
$.each( data.split( /\s/ ), function() {
transformed[ this ] = true;
} );
data = transformed;
}
return data;
},
// https://jqueryvalidation.org/jquery.validator.addmethod/
addmethod: function( name, method, message ) {
$.validator.methods[ name ] = method;
$.validator.messages[ name ] = message !== undefined ? message : $.validator.messages[ name ];
if ( method.length < 3 ) {
$.validator.addclassrules( name, $.validator.normalizerule( name ) );
}
},
// https://jqueryvalidation.org/jquery.validator.methods/
methods: {
// https://jqueryvalidation.org/required-method/
required: function( value, element, param ) {
// check if dependency is met
if ( !this.depend( param, element ) ) {
return "dependency-mismatch";
}
if ( element.nodename.tolowercase() === "select" ) {
// could be an array for select-multiple or a string, both are fine this way
var val = $( element ).val();
return val && val.length > 0;
}
if ( this.checkable( element ) ) {
return this.getlength( value, element ) > 0;
}
return value !== undefined && value !== null && value.length > 0;
},
// https://jqueryvalidation.org/email-method/
email: function( value, element ) {
// 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/
url: function( value, element ) {
// 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-z0-9\u00a1-\uffff][a-z0-9\u00a1-\uffff_-]{0,62})?[a-z0-9\u00a1-\uffff]\.)+(?:[a-z\u00a1-\uffff]{2,}\.?))(?::\d{2,5})?(?:[/?#]\s*)?$/i.test( value );
},
// https://jqueryvalidation.org/date-method/
date: ( function() {
var called = false;
return function( value, element ) {
if ( !called ) {
called = true;
if ( this.settings.debug && window.console ) {
console.warn(
"the `date` method is deprecated and will be removed in version '2.0.0'.\n" +
"please don't use it, since it relies on the date constructor, which\n" +
"behaves very differently across browsers and locales. use `dateiso`\n" +
"instead or one of the locale specific methods in `localizations/`\n" +
"and `additional-methods.js`."
);
}
}
return this.optional( element ) || !/invalid|nan/.test( new date( value ).tostring() );
};
}() ),
// https://jqueryvalidation.org/dateiso-method/
dateiso: function( value, element ) {
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/
number: function( value, element ) {
return this.optional( element ) || /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/.test( value );
},
// https://jqueryvalidation.org/digits-method/
digits: function( value, element ) {
return this.optional( element ) || /^\d+$/.test( value );
},
// https://jqueryvalidation.org/minlength-method/
minlength: function( value, element, param ) {
var length = array.isarray( value ) ? value.length : this.getlength( value, element );
return this.optional( element ) || length >= param;
},
// https://jqueryvalidation.org/maxlength-method/
maxlength: function( value, element, param ) {
var length = array.isarray( value ) ? value.length : this.getlength( value, element );
return this.optional( element ) || length <= param;
},
// https://jqueryvalidation.org/rangelength-method/
rangelength: function( value, element, param ) {
var length = array.isarray( value ) ? value.length : this.getlength( value, element );
return this.optional( element ) || ( length >= param[ 0 ] && length <= param[ 1 ] );
},
// https://jqueryvalidation.org/min-method/
min: function( value, element, param ) {
return this.optional( element ) || value >= param;
},
// https://jqueryvalidation.org/max-method/
max: function( value, element, param ) {
return this.optional( element ) || value <= param;
},
// https://jqueryvalidation.org/range-method/
range: function( value, element, param ) {
return this.optional( element ) || ( value >= param[ 0 ] && value <= param[ 1 ] );
},
// https://jqueryvalidation.org/step-method/
step: function( value, element, param ) {
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() ),
decimalplaces = function( num ) {
var match = ( "" + num ).match( /(?:\.(\d+))?$/ );
if ( !match ) {
return 0;
}
// number of digits right of decimal point.
return match[ 1 ] ? match[ 1 ].length : 0;
},
toint = function( num ) {
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
if ( notsupported ) {
throw new error( errormessage );
}
decimals = decimalplaces( param );
// value can't have too many decimals
if ( decimalplaces( value ) > decimals || toint( value ) % toint( param ) !== 0 ) {
valid = false;
}
return this.optional( element ) || valid;
},
// https://jqueryvalidation.org/equalto-method/
equalto: function( value, element, param ) {
// bind to the blur event of the target in order to revalidate whenever the target field is updated
var target = $( param );
if ( this.settings.onfocusout && target.not( ".validate-equalto-blur" ).length ) {
target.addclass( "validate-equalto-blur" ).on( "blur.validate-equalto", function() {
$( element ).valid();
} );
}
return value === target.val();
},
// https://jqueryvalidation.org/remote-method/
remote: function( value, element, param, method ) {
if ( this.optional( element ) ) {
return "dependency-mismatch";
}
method = typeof method === "string" && method || "remote";
var previous = this.previousvalue( element, method ),
validator, data, optiondatastring;
if ( !this.settings.messages[ element.name ] ) {
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 ) );
if ( previous.old === optiondatastring ) {
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,
success: function( response ) {
var valid = response === true || response === "true",
errors, message, submitted;
validator.settings.messages[ element.name ][ method ] = previous.originalmessage;
if ( valid ) {
submitted = validator.formsubmitted;
validator.resetinternals();
validator.tohide = validator.errorsfor( element );
validator.formsubmitted = submitted;
validator.successlist.push( element );
validator.invalid[ element.name ] = false;
validator.showerrors();
} else {
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()
var pendingrequests = {},
ajax;
// use a prefilter if available (1.5+)
if ( $.ajaxprefilter ) {
$.ajaxprefilter( function( settings, _, xhr ) {
var port = settings.port;
if ( settings.mode === "abort" ) {
if ( pendingrequests[ port ] ) {
pendingrequests[ port ].abort();
}
pendingrequests[ port ] = xhr;
}
} );
} else {
// proxy ajax
ajax = $.ajax;
$.ajax = function( settings ) {
var mode = ( "mode" in settings ? settings : $.ajaxsettings ).mode,
port = ( "port" in settings ? settings : $.ajaxsettings ).port;
if ( mode === "abort" ) {
if ( pendingrequests[ port ] ) {
pendingrequests[ port ].abort();
}
pendingrequests[ port ] = ajax.apply( this, arguments );
return pendingrequests[ port ];
}
return ajax.apply( this, arguments );
};
}
return $;
}));