/*!
* jquery validation plugin v1.13.1
*
* http://jqueryvalidation.org/
*
* copyright (c) 2014 jörn zaefferer
* released under the mit license
*/
(function( factory ) {
if ( typeof define === "function" && define.amd ) {
define( ["jquery"], factory );
} else {
factory( jquery );
}
}(function( $ ) {
$.extend($.fn, {
// http://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.validatedelegate( ":submit", "click", function( event ) {
if ( validator.settings.submithandler ) {
validator.submitbutton = event.target;
}
// allow suppressing validation by adding a cancel class to the submit button
if ( $( event.target ).hasclass( "cancel" ) ) {
validator.cancelsubmit = true;
}
// allow suppressing validation by adding the html5 formnovalidate attribute to the submit button
if ( $( event.target ).attr( "formnovalidate" ) !== undefined ) {
validator.cancelsubmit = true;
}
});
// validate the form on submit
this.submit( function( event ) {
if ( validator.settings.debug ) {
// prevent form submit to be able to see console output
event.preventdefault();
}
function handle() {
var hidden, result;
if ( validator.settings.submithandler ) {
if ( validator.submitbutton ) {
// insert a hidden input as a replacement for the missing submit button
hidden = $( "" )
.attr( "name", validator.submitbutton.name )
.val( $( validator.submitbutton ).val() )
.appendto( validator.currentform );
}
result = validator.settings.submithandler.call( validator, validator.currentform, event );
if ( validator.submitbutton ) {
// 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;
},
// http://jqueryvalidation.org/valid/
valid: function() {
var valid, validator;
if ( $( this[ 0 ] ).is( "form" ) ) {
valid = this.validate().form();
} else {
valid = true;
validator = $( this[ 0 ].form ).validate();
this.each( function() {
valid = validator.element( this ) && valid;
});
}
return valid;
},
// attributes: space separated list of attributes to retrieve and remove
removeattrs: function( attributes ) {
var result = {},
$element = this;
$.each( attributes.split( /\s/ ), function( index, value ) {
result[ value ] = $element.attr( value );
$element.removeattr( value );
});
return result;
},
// http://jqueryvalidation.org/rules/
rules: function( command, argument ) {
var element = this[ 0 ],
settings, staticrules, existingrules, data, param, filtered;
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 ];
if ( method === "required" ) {
$( element ).removeattr( "aria-required" );
}
});
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 );
$( element ).attr( "aria-required", "true" );
}
// make sure remote is at back
if ( data.remote ) {
param = data.remote;
delete data.remote;
data = $.extend( data, { remote: param });
}
return data;
}
});
// custom selectors
$.extend( $.expr[ ":" ], {
// http://jqueryvalidation.org/blank-selector/
blank: function( a ) {
return !$.trim( "" + $( a ).val() );
},
// http://jqueryvalidation.org/filled-selector/
filled: function( a ) {
return !!$.trim( "" + $( a ).val() );
},
// http://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();
};
// http://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 ( 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",
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 ) {
if ( event.which === 9 && this.elementvalue( element ) === "" ) {
return;
} else if ( element.name in this.submitted || element === this.lastelement ) {
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 );
}
}
},
// http://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.",
creditcard: "please enter a valid credit card number.",
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}." )
},
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 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 validator = $.data( this[ 0 ].form, "validator" ),
eventtype = "on" + event.type.replace( /^validate/, "" ),
settings = validator.settings;
if ( settings[ eventtype ] && !this.is( settings.ignore ) ) {
settings[ eventtype ].call( validator, this[ 0 ], event );
}
}
$( this.currentform )
.validatedelegate( ":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']",
"focusin focusout keyup", delegate)
// support: chrome, oldie
// "select" is provided as event.target when clicking a option
.validatedelegate("select, option, [type='radio'], [type='checkbox']", "click", delegate);
if ( this.settings.invalidhandler ) {
$( this.currentform ).bind( "invalid-form.validate", this.settings.invalidhandler );
}
// add aria-required to any static/data/class required fields before first validation
// screen readers require this attribute to be present before the initial submission http://www.w3.org/tr/wcag-techs/aria2.html
$( this.currentform ).find( "[required], [data-rule-required], .required" ).attr( "aria-required", "true" );
},
// http://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();
},
// http://jqueryvalidation.org/validator.element/
element: function( element ) {
var cleanelement = this.clean( element ),
checkelement = this.validationtargetfor( cleanelement ),
result = true;
this.lastelement = checkelement;
if ( checkelement === undefined ) {
delete this.invalid[ cleanelement.name ];
} else {
this.prepareelement( checkelement );
this.currentelements = $( checkelement );
result = this.check( checkelement ) !== false;
if ( result ) {
delete this.invalid[ checkelement.name ];
} else {
this.invalid[ checkelement.name ] = true;
}
}
// add aria-invalid status for screen readers
$( element ).attr( "aria-invalid", !result );
if ( !this.numberofinvalids() ) {
// hide error containers on last error
this.tohide = this.tohide.add( this.containers );
}
this.showerrors();
return result;
},
// http://jqueryvalidation.org/validator.showerrors/
showerrors: function( errors ) {
if ( errors ) {
// add items to error list and map
$.extend( this.errormap, errors );
this.errorlist = [];
for ( var name in errors ) {
this.errorlist.push({
message: errors[ name ],
element: this.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();
}
},
// http://jqueryvalidation.org/validator.resetform/
resetform: function() {
if ( $.fn.resetform ) {
$( this.currentform ).resetform();
}
this.submitted = {};
this.lastelement = null;
this.prepareform();
this.hideerrors();
this.elements()
.removeclass( this.settings.errorclass )
.removedata( "previousvalue" )
.removeattr( "aria-invalid" );
},
numberofinvalids: function() {
return this.objectlength( this.invalid );
},
objectlength: function( obj ) {
/* jshint unused: false */
var count = 0,
i;
for ( i in obj ) {
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" )
.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" )
.not( ":submit, :reset, :image, [disabled], [readonly]" )
.not( this.settings.ignore )
.filter( function() {
if ( !this.name && validator.settings.debug && window.console ) {
console.error( "%o has no name assigned", this );
}
// select only the first element for each name, and only those with rules specified
if ( this.name in rulescache || !validator.objectlength( $( this ).rules() ) ) {
return false;
}
rulescache[ this.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 );
},
reset: function() {
this.successlist = [];
this.errorlist = [];
this.errormap = {};
this.toshow = $( [] );
this.tohide = $( [] );
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 val,
$element = $( element ),
type = element.type;
if ( type === "radio" || type === "checkbox" ) {
return $( "input[name='" + element.name + "']:checked" ).val();
} else if ( type === "number" && typeof element.validity !== "undefined" ) {
return element.validity.badinput ? false : $element.val();
}
val = $element.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;
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 );
}
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;
},
defaultmessage: function( element, method ) {
return this.finddefined(
this.custommessage( element.name, method ),
this.customdatamessage( element, method ),
// title is never undefined, so handle empty string as undefined
!this.settings.ignoretitle && element.title || undefined,
$.validator.messages[ method ],
"warning: no message defined for " + element.name + ""
);
},
formatandadd: function( element, rule ) {
var message = this.defaultmessage( element, rule.method ),
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 );
}
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,
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( 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 );
} else if ( error.parents( "label[for='" + elementid + "']" ).length === 0 ) {
// if the element is not a child of an associated label, then it's necessary
// to explicitly apply aria-describedby
errorid = error.attr( "id" ).replace( /(:|\.|\[|\])/g, "\\$1");
// respect existing non-error aria-describedby
if ( !describedby ) {
describedby = errorid;
} else if ( !describedby.match( new regexp( "\\b" + 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 ) {
$.each( this.groups, function( name, testgroup ) {
if ( testgroup === group ) {
$( "[name='" + name + "']", this.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.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 + ", #" + describer.replace( /\s+/g, ", #" );
}
return this
.errors()
.filter( selector );
},
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='" + 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++;
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 ];
if ( valid && this.pendingrequest === 0 && this.formsubmitted && this.form() ) {
$( this.currentform ).submit();
this.formsubmitted = false;
} else if (!valid && this.pendingrequest === 0 && this.formsubmitted ) {
$( this.currentform ).triggerhandler( "invalid-form", [ this ]);
this.formsubmitted = false;
}
},
previousvalue: function( element ) {
return $.data( element, "previousvalue" ) || $.data( element, "previousvalue", {
old: null,
valid: true,
message: this.defaultmessage( element, "remote" )
});
}
},
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;
},
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 );
}
// 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/.test( method ) && ( type === null || /number|range|text/.test( type ) ) ) {
value = number( value );
}
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;
}
}
// 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 method, value,
rules = {}, $element = $( element );
for ( method in $.validator.methods ) {
value = $element.data( "rule" + method.charat( 0 ).touppercase() + method.substring( 1 ).tolowercase() );
if ( value !== undefined ) {
rules[ 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 {
delete rules[ prop ];
}
}
});
// evaluate parameters
$.each( rules, function( rule, parameter ) {
rules[ rule ] = $.isfunction( parameter ) ? 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 ( $.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;
},
// http://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 ) );
}
},
methods: {
// http://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 $.trim( value ).length > 0;
},
// http://jqueryvalidation.org/email-method/
email: function( value, element ) {
// from http://www.whatwg.org/specs/web-apps/current-work/multipage/states-of-the-type-attribute.html#e-mail-state-%28type=email%29
// 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 );
},
// http://jqueryvalidation.org/url-method/
url: function( value, element ) {
// contributed by scott gonzalez: http://projects.scottsplayground.com/iri/
return this.optional( element ) || /^(https?|s?ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])|(([a-z]|\d|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])([a-z]|\d|-|\.|_|~|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])*([a-z]|\d|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])))\.)+(([a-z]|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])|(([a-z]|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])([a-z]|\d|-|\.|_|~|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])*([a-z]|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\ue000-\uf8ff]|\/|\?)*)?(#((([a-z]|\d|-|\.|_|~|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test( value );
},
// http://jqueryvalidation.org/date-method/
date: function( value, element ) {
return this.optional( element ) || !/invalid|nan/.test( new date( value ).tostring() );
},
// http://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 );
},
// http://jqueryvalidation.org/number-method/
number: function( value, element ) {
return this.optional( element ) || /^-?(?:\d+|\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/.test( value );
},
// http://jqueryvalidation.org/digits-method/
digits: function( value, element ) {
return this.optional( element ) || /^\d+$/.test( value );
},
// http://jqueryvalidation.org/creditcard-method/
// based on http://en.wikipedia.org/wiki/luhn/
creditcard: function( value, element ) {
if ( this.optional( element ) ) {
return "dependency-mismatch";
}
// accept only spaces, digits and dashes
if ( /[^0-9 \-]+/.test( value ) ) {
return false;
}
var ncheck = 0,
ndigit = 0,
beven = false,
n, cdigit;
value = value.replace( /\d/g, "" );
// basing min and max length on
// http://developer.ean.com/general_info/valid_credit_card_types
if ( value.length < 13 || value.length > 19 ) {
return false;
}
for ( n = value.length - 1; n >= 0; n--) {
cdigit = value.charat( n );
ndigit = parseint( cdigit, 10 );
if ( beven ) {
if ( ( ndigit *= 2 ) > 9 ) {
ndigit -= 9;
}
}
ncheck += ndigit;
beven = !beven;
}
return ( ncheck % 10 ) === 0;
},
// http://jqueryvalidation.org/minlength-method/
minlength: function( value, element, param ) {
var length = $.isarray( value ) ? value.length : this.getlength( value, element );
return this.optional( element ) || length >= param;
},
// http://jqueryvalidation.org/maxlength-method/
maxlength: function( value, element, param ) {
var length = $.isarray( value ) ? value.length : this.getlength( value, element );
return this.optional( element ) || length <= param;
},
// http://jqueryvalidation.org/rangelength-method/
rangelength: function( value, element, param ) {
var length = $.isarray( value ) ? value.length : this.getlength( value, element );
return this.optional( element ) || ( length >= param[ 0 ] && length <= param[ 1 ] );
},
// http://jqueryvalidation.org/min-method/
min: function( value, element, param ) {
return this.optional( element ) || value >= param;
},
// http://jqueryvalidation.org/max-method/
max: function( value, element, param ) {
return this.optional( element ) || value <= param;
},
// http://jqueryvalidation.org/range-method/
range: function( value, element, param ) {
return this.optional( element ) || ( value >= param[ 0 ] && value <= param[ 1 ] );
},
// http://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
// todo find a way to bind the event just once, avoiding the unbind-rebind overhead
var target = $( param );
if ( this.settings.onfocusout ) {
target.unbind( ".validate-equalto" ).bind( "blur.validate-equalto", function() {
$( element ).valid();
});
}
return value === target.val();
},
// http://jqueryvalidation.org/remote-method/
remote: function( value, element, param ) {
if ( this.optional( element ) ) {
return "dependency-mismatch";
}
var previous = this.previousvalue( element ),
validator, data;
if (!this.settings.messages[ element.name ] ) {
this.settings.messages[ element.name ] = {};
}
previous.originalmessage = this.settings.messages[ element.name ].remote;
this.settings.messages[ element.name ].remote = previous.message;
param = typeof param === "string" && { url: param } || param;
if ( previous.old === value ) {
return previous.valid;
}
previous.old = value;
validator = this;
this.startrequest( element );
data = {};
data[ element.name ] = value;
$.ajax( $.extend( true, {
url: param,
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 ].remote = previous.originalmessage;
if ( valid ) {
submitted = validator.formsubmitted;
validator.prepareelement( element );
validator.formsubmitted = submitted;
validator.successlist.push( element );
delete validator.invalid[ element.name ];
validator.showerrors();
} else {
errors = {};
message = response || validator.defaultmessage( element, "remote" );
errors[ element.name ] = previous.message = $.isfunction( message ) ? message( value ) : message;
validator.invalid[ element.name ] = true;
validator.showerrors( errors );
}
previous.valid = valid;
validator.stoprequest( element, valid );
}
}, param ) );
return "pending";
}
}
});
$.format = function deprecated() {
throw "$.format has been deprecated. please use $.validator.format instead.";
};
// 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);
};
}
// provides delegate(type: string, delegate: selector, handler: callback) plugin for easier event delegation
// handler is only called when $(event.target).is(delegate), in the scope of the jquery-object for event.target
$.extend($.fn, {
validatedelegate: function( delegate, type, handler ) {
return this.bind(type, function( event ) {
var target = $(event.target);
if ( target.is(delegate) ) {
return handler.apply(target, arguments);
}
});
}
});
}));