2013-03-19 00:06:47 +01:00
|
|
|
/*!
|
2014-04-16 01:48:10 +02:00
|
|
|
* jQuery Validation Plugin v1.12.1pre
|
2013-03-19 00:06:47 +01:00
|
|
|
*
|
2014-04-16 01:48:10 +02:00
|
|
|
* http://jqueryvalidation.org/
|
2013-03-19 00:06:47 +01:00
|
|
|
*
|
2014-04-16 01:48:10 +02:00
|
|
|
* Copyright (c) 2014 Jörn Zaefferer
|
|
|
|
* Released under the MIT license
|
2013-03-19 00:06:47 +01:00
|
|
|
*/
|
2014-04-16 01:48:10 +02:00
|
|
|
(function( factory ) {
|
|
|
|
if ( typeof define === "function" && define.amd ) {
|
|
|
|
define( ["jquery"], factory );
|
|
|
|
} else {
|
|
|
|
factory( jQuery );
|
|
|
|
}
|
|
|
|
}(function( $ ) {
|
2013-03-19 00:06:47 +01:00
|
|
|
|
|
|
|
$.extend($.fn, {
|
2014-04-16 01:48:10 +02:00
|
|
|
// http://jqueryvalidation.org/validate/
|
2013-03-19 00:06:47 +01:00
|
|
|
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
|
2014-04-16 01:48:10 +02:00
|
|
|
var validator = $.data( this[ 0 ], "validator" );
|
2013-03-19 00:06:47 +01:00
|
|
|
if ( validator ) {
|
|
|
|
return validator;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add novalidate tag if HTML5.
|
|
|
|
this.attr( "novalidate", "novalidate" );
|
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
validator = new $.validator( options, this[ 0 ] );
|
|
|
|
$.data( this[ 0 ], "validator", validator );
|
2013-03-19 00:06:47 +01:00
|
|
|
|
|
|
|
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
|
2014-04-16 01:48:10 +02:00
|
|
|
if ( $( event.target ).hasClass( "cancel" ) ) {
|
2013-03-19 00:06:47 +01:00
|
|
|
validator.cancelSubmit = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// allow suppressing validation by adding the html5 formnovalidate attribute to the submit button
|
2014-04-16 01:48:10 +02:00
|
|
|
if ( $( event.target ).attr( "formnovalidate" ) !== undefined ) {
|
2013-03-19 00:06:47 +01:00
|
|
|
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;
|
|
|
|
if ( validator.settings.submitHandler ) {
|
|
|
|
if ( validator.submitButton ) {
|
|
|
|
// insert a hidden input as a replacement for the missing submit button
|
2014-04-16 01:48:10 +02:00
|
|
|
hidden = $( "<input type='hidden'/>" )
|
|
|
|
.attr( "name", validator.submitButton.name )
|
|
|
|
.val( $( validator.submitButton ).val() )
|
|
|
|
.appendTo( validator.currentForm );
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
},
|
2014-04-16 01:48:10 +02:00
|
|
|
// http://jqueryvalidation.org/valid/
|
2013-03-19 00:06:47 +01:00
|
|
|
valid: function() {
|
2014-04-16 01:48:10 +02:00
|
|
|
var valid, validator;
|
|
|
|
|
|
|
|
if ( $( this[ 0 ] ).is( "form" ) ) {
|
|
|
|
valid = this.validate().form();
|
2013-03-19 00:06:47 +01:00
|
|
|
} else {
|
2014-04-16 01:48:10 +02:00
|
|
|
valid = true;
|
|
|
|
validator = $( this[ 0 ].form ).validate();
|
|
|
|
this.each( function() {
|
|
|
|
valid = validator.element( this ) && valid;
|
2013-03-19 00:06:47 +01:00
|
|
|
});
|
|
|
|
}
|
2014-04-16 01:48:10 +02:00
|
|
|
return valid;
|
2013-03-19 00:06:47 +01:00
|
|
|
},
|
2014-04-16 01:48:10 +02:00
|
|
|
// attributes: space separated list of attributes to retrieve and remove
|
2013-03-19 00:06:47 +01:00
|
|
|
removeAttrs: function( attributes ) {
|
|
|
|
var result = {},
|
|
|
|
$element = this;
|
2014-04-16 01:48:10 +02:00
|
|
|
$.each( attributes.split( /\s/ ), function( index, value ) {
|
|
|
|
result[ value ] = $element.attr( value );
|
|
|
|
$element.removeAttr( value );
|
2013-03-19 00:06:47 +01:00
|
|
|
});
|
|
|
|
return result;
|
|
|
|
},
|
2014-04-16 01:48:10 +02:00
|
|
|
// http://jqueryvalidation.org/rules/
|
2013-03-19 00:06:47 +01:00
|
|
|
rules: function( command, argument ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
var element = this[ 0 ],
|
|
|
|
settings, staticRules, existingRules, data, param, filtered;
|
2013-03-19 00:06:47 +01:00
|
|
|
|
|
|
|
if ( command ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
settings = $.data( element.form, "validator" ).settings;
|
|
|
|
staticRules = settings.rules;
|
|
|
|
existingRules = $.validator.staticRules( element );
|
|
|
|
switch ( command ) {
|
2013-03-19 00:06:47 +01:00
|
|
|
case "add":
|
2014-04-16 01:48:10 +02:00
|
|
|
$.extend( existingRules, $.validator.normalizeRule( argument ) );
|
|
|
|
// remove messages from rules, but allow them to be set separately
|
|
|
|
delete existingRules.messages;
|
|
|
|
staticRules[ element.name ] = existingRules;
|
2013-03-19 00:06:47 +01:00
|
|
|
if ( argument.messages ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
settings.messages[ element.name ] = $.extend( settings.messages[ element.name ], argument.messages );
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "remove":
|
|
|
|
if ( !argument ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
delete staticRules[ element.name ];
|
2013-03-19 00:06:47 +01:00
|
|
|
return existingRules;
|
|
|
|
}
|
2014-04-16 01:48:10 +02:00
|
|
|
filtered = {};
|
|
|
|
$.each( argument.split( /\s/ ), function( index, method ) {
|
|
|
|
filtered[ method ] = existingRules[ method ];
|
|
|
|
delete existingRules[ method ];
|
|
|
|
if ( method === "required" ) {
|
|
|
|
$( element ).removeAttr( "aria-required" );
|
|
|
|
}
|
2013-03-19 00:06:47 +01:00
|
|
|
});
|
|
|
|
return filtered;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
data = $.validator.normalizeRules(
|
2013-03-19 00:06:47 +01:00
|
|
|
$.extend(
|
|
|
|
{},
|
2014-04-16 01:48:10 +02:00
|
|
|
$.validator.classRules( element ),
|
|
|
|
$.validator.attributeRules( element ),
|
|
|
|
$.validator.dataRules( element ),
|
|
|
|
$.validator.staticRules( element )
|
|
|
|
), element );
|
2013-03-19 00:06:47 +01:00
|
|
|
|
|
|
|
// make sure required is at front
|
|
|
|
if ( data.required ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
param = data.required;
|
2013-03-19 00:06:47 +01:00
|
|
|
delete data.required;
|
2014-04-16 01:48:10 +02:00
|
|
|
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 });
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Custom selectors
|
2014-04-16 01:48:10 +02:00
|
|
|
$.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" ); }
|
2013-03-19 00:06:47 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// constructor for validator
|
|
|
|
$.validator = function( options, form ) {
|
|
|
|
this.settings = $.extend( true, {}, $.validator.defaults, options );
|
|
|
|
this.currentForm = form;
|
|
|
|
this.init();
|
|
|
|
};
|
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
// http://jqueryvalidation.org/jQuery.validator.format/
|
2013-03-19 00:06:47 +01:00
|
|
|
$.validator.format = function( source, params ) {
|
|
|
|
if ( arguments.length === 1 ) {
|
|
|
|
return function() {
|
2014-04-16 01:48:10 +02:00
|
|
|
var args = $.makeArray( arguments );
|
|
|
|
args.unshift( source );
|
2013-03-19 00:06:47 +01:00
|
|
|
return $.validator.format.apply( this, args );
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if ( arguments.length > 2 && params.constructor !== Array ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
params = $.makeArray( arguments ).slice( 1 );
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
|
|
|
if ( params.constructor !== Array ) {
|
|
|
|
params = [ params ];
|
|
|
|
}
|
2014-04-16 01:48:10 +02:00
|
|
|
$.each( params, function( i, n ) {
|
|
|
|
source = source.replace( new RegExp( "\\{" + i + "\\}", "g" ), function() {
|
2013-03-19 00:06:47 +01:00
|
|
|
return n;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return source;
|
|
|
|
};
|
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
$.extend( $.validator, {
|
2013-03-19 00:06:47 +01:00
|
|
|
|
|
|
|
defaults: {
|
|
|
|
messages: {},
|
|
|
|
groups: {},
|
|
|
|
rules: {},
|
|
|
|
errorClass: "error",
|
|
|
|
validClass: "valid",
|
|
|
|
errorElement: "label",
|
|
|
|
focusInvalid: true,
|
2014-04-16 01:48:10 +02:00
|
|
|
errorContainer: $( [] ),
|
|
|
|
errorLabelContainer: $( [] ),
|
2013-03-19 00:06:47 +01:00
|
|
|
onsubmit: true,
|
|
|
|
ignore: ":hidden",
|
|
|
|
ignoreTitle: false,
|
2014-04-16 01:48:10 +02:00
|
|
|
onfocusin: function( element ) {
|
2013-03-19 00:06:47 +01:00
|
|
|
this.lastActive = element;
|
|
|
|
|
|
|
|
// hide error label and remove error class on focus if enabled
|
|
|
|
if ( this.settings.focusCleanup && !this.blockFocusCleanup ) {
|
|
|
|
if ( this.settings.unhighlight ) {
|
|
|
|
this.settings.unhighlight.call( this, element, this.settings.errorClass, this.settings.validClass );
|
|
|
|
}
|
2014-04-16 01:48:10 +02:00
|
|
|
this.addWrapper( this.errorsFor( element ) ).hide();
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
|
|
|
},
|
2014-04-16 01:48:10 +02:00
|
|
|
onfocusout: function( element ) {
|
|
|
|
if ( !this.checkable( element ) && ( element.name in this.submitted || !this.optional( element ) ) ) {
|
|
|
|
this.element( element );
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
onkeyup: function( element, event ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
if ( event.which === 9 && this.elementValue( element ) === "" ) {
|
2013-03-19 00:06:47 +01:00
|
|
|
return;
|
|
|
|
} else if ( element.name in this.submitted || element === this.lastElement ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
this.element( element );
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
|
|
|
},
|
2014-04-16 01:48:10 +02:00
|
|
|
onclick: function( element ) {
|
2013-03-19 00:06:47 +01:00
|
|
|
// click on selects, radiobuttons and checkboxes
|
|
|
|
if ( element.name in this.submitted ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
this.element( element );
|
|
|
|
|
2013-03-19 00:06:47 +01:00
|
|
|
// or option elements, check parent select in that case
|
2014-04-16 01:48:10 +02:00
|
|
|
} else if ( element.parentNode.name in this.submitted ) {
|
|
|
|
this.element( element.parentNode );
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
highlight: function( element, errorClass, validClass ) {
|
|
|
|
if ( element.type === "radio" ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
this.findByName( element.name ).addClass( errorClass ).removeClass( validClass );
|
2013-03-19 00:06:47 +01:00
|
|
|
} else {
|
2014-04-16 01:48:10 +02:00
|
|
|
$( element ).addClass( errorClass ).removeClass( validClass );
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
unhighlight: function( element, errorClass, validClass ) {
|
|
|
|
if ( element.type === "radio" ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
this.findByName( element.name ).removeClass( errorClass ).addClass( validClass );
|
2013-03-19 00:06:47 +01:00
|
|
|
} else {
|
2014-04-16 01:48:10 +02:00
|
|
|
$( element ).removeClass( errorClass ).addClass( validClass );
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
// http://jqueryvalidation.org/jQuery.validator.setDefaults/
|
2013-03-19 00:06:47 +01:00
|
|
|
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.",
|
2014-04-16 01:48:10 +02:00
|
|
|
dateISO: "Please enter a valid date ( ISO ).",
|
2013-03-19 00:06:47 +01:00
|
|
|
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.",
|
2014-04-16 01:48:10 +02:00
|
|
|
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}." )
|
2013-03-19 00:06:47 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
autoCreateRanges: false,
|
|
|
|
|
|
|
|
prototype: {
|
|
|
|
|
|
|
|
init: function() {
|
2014-04-16 01:48:10 +02:00
|
|
|
this.labelContainer = $( this.settings.errorLabelContainer );
|
|
|
|
this.errorContext = this.labelContainer.length && this.labelContainer || $( this.currentForm );
|
|
|
|
this.containers = $( this.settings.errorContainer ).add( this.settings.errorLabelContainer );
|
2013-03-19 00:06:47 +01:00
|
|
|
this.submitted = {};
|
|
|
|
this.valueCache = {};
|
|
|
|
this.pendingRequest = 0;
|
|
|
|
this.pending = {};
|
|
|
|
this.invalid = {};
|
|
|
|
this.reset();
|
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
var groups = ( this.groups = {} ),
|
|
|
|
rules;
|
|
|
|
$.each( this.settings.groups, function( key, value ) {
|
2013-03-19 00:06:47 +01:00
|
|
|
if ( typeof value === "string" ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
value = value.split( /\s/ );
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
2014-04-16 01:48:10 +02:00
|
|
|
$.each( value, function( index, name ) {
|
|
|
|
groups[ name ] = key;
|
2013-03-19 00:06:47 +01:00
|
|
|
});
|
|
|
|
});
|
2014-04-16 01:48:10 +02:00
|
|
|
rules = this.settings.rules;
|
|
|
|
$.each( rules, function( key, value ) {
|
|
|
|
rules[ key ] = $.validator.normalizeRule( value );
|
2013-03-19 00:06:47 +01:00
|
|
|
});
|
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
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 );
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
|
|
|
}
|
2014-04-16 01:48:10 +02:00
|
|
|
$( this.currentForm )
|
|
|
|
.validateDelegate( ":text, [type='password'], [type='file'], select, textarea, " +
|
2013-03-19 00:06:47 +01:00
|
|
|
"[type='number'], [type='search'] ,[type='tel'], [type='url'], " +
|
|
|
|
"[type='email'], [type='datetime'], [type='date'], [type='month'], " +
|
|
|
|
"[type='week'], [type='time'], [type='datetime-local'], " +
|
2014-04-16 01:48:10 +02:00
|
|
|
"[type='range'], [type='color'], [type='radio'], [type='checkbox']",
|
2013-03-19 00:06:47 +01:00
|
|
|
"focusin focusout keyup", delegate)
|
2014-04-16 01:48:10 +02:00
|
|
|
// Support: Chrome, oldIE
|
|
|
|
// "select" is provided as event.target when clicking a option
|
|
|
|
.validateDelegate("select, option", "click", delegate);
|
2013-03-19 00:06:47 +01:00
|
|
|
|
|
|
|
if ( this.settings.invalidHandler ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
$( this.currentForm ).bind( "invalid-form.validate", this.settings.invalidHandler );
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
2014-04-16 01:48:10 +02:00
|
|
|
|
|
|
|
// 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" );
|
2013-03-19 00:06:47 +01:00
|
|
|
},
|
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
// http://jqueryvalidation.org/Validator.form/
|
2013-03-19 00:06:47 +01:00
|
|
|
form: function() {
|
|
|
|
this.checkForm();
|
2014-04-16 01:48:10 +02:00
|
|
|
$.extend( this.submitted, this.errorMap );
|
|
|
|
this.invalid = $.extend({}, this.errorMap );
|
2013-03-19 00:06:47 +01:00
|
|
|
if ( !this.valid() ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
$( this.currentForm ).triggerHandler( "invalid-form", [ this ]);
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
|
|
|
this.showErrors();
|
|
|
|
return this.valid();
|
|
|
|
},
|
|
|
|
|
|
|
|
checkForm: function() {
|
|
|
|
this.prepareForm();
|
2014-04-16 01:48:10 +02:00
|
|
|
for ( var i = 0, elements = ( this.currentElements = this.elements() ); elements[ i ]; i++ ) {
|
|
|
|
this.check( elements[ i ] );
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
|
|
|
return this.valid();
|
|
|
|
},
|
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
// http://jqueryvalidation.org/Validator.element/
|
2013-03-19 00:06:47 +01:00
|
|
|
element: function( element ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
var cleanElement = this.clean( element ),
|
|
|
|
checkElement = this.validationTargetFor( cleanElement ),
|
|
|
|
result = true;
|
|
|
|
|
|
|
|
this.lastElement = checkElement;
|
|
|
|
|
|
|
|
if ( checkElement === undefined ) {
|
|
|
|
delete this.invalid[ cleanElement.name ];
|
2013-03-19 00:06:47 +01:00
|
|
|
} else {
|
2014-04-16 01:48:10 +02:00
|
|
|
this.prepareElement( checkElement );
|
|
|
|
this.currentElements = $( checkElement );
|
|
|
|
|
|
|
|
result = this.check( checkElement ) !== false;
|
|
|
|
if ( result ) {
|
|
|
|
delete this.invalid[ checkElement.name ];
|
|
|
|
} else {
|
|
|
|
this.invalid[ checkElement.name ] = true;
|
|
|
|
}
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
2014-04-16 01:48:10 +02:00
|
|
|
// Add aria-invalid status for screen readers
|
|
|
|
$( element ).attr( "aria-invalid", !result );
|
|
|
|
|
2013-03-19 00:06:47 +01:00
|
|
|
if ( !this.numberOfInvalids() ) {
|
|
|
|
// Hide error containers on last error
|
|
|
|
this.toHide = this.toHide.add( this.containers );
|
|
|
|
}
|
|
|
|
this.showErrors();
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
// http://jqueryvalidation.org/Validator.showErrors/
|
2013-03-19 00:06:47 +01:00
|
|
|
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({
|
2014-04-16 01:48:10 +02:00
|
|
|
message: errors[ name ],
|
|
|
|
element: this.findByName( name )[ 0 ]
|
2013-03-19 00:06:47 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
// remove items from success list
|
|
|
|
this.successList = $.grep( this.successList, function( element ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
return !( element.name in errors );
|
2013-03-19 00:06:47 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
if ( this.settings.showErrors ) {
|
|
|
|
this.settings.showErrors.call( this, this.errorMap, this.errorList );
|
|
|
|
} else {
|
|
|
|
this.defaultShowErrors();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
// http://jqueryvalidation.org/Validator.resetForm/
|
2013-03-19 00:06:47 +01:00
|
|
|
resetForm: function() {
|
|
|
|
if ( $.fn.resetForm ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
$( this.currentForm ).resetForm();
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
|
|
|
this.submitted = {};
|
|
|
|
this.lastElement = null;
|
|
|
|
this.prepareForm();
|
|
|
|
this.hideErrors();
|
2014-04-16 01:48:10 +02:00
|
|
|
this.elements()
|
|
|
|
.removeClass( this.settings.errorClass )
|
|
|
|
.removeData( "previousValue" )
|
|
|
|
.removeAttr( "aria-invalid" );
|
2013-03-19 00:06:47 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
numberOfInvalids: function() {
|
2014-04-16 01:48:10 +02:00
|
|
|
return this.objectLength( this.invalid );
|
2013-03-19 00:06:47 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
objectLength: function( obj ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
/* jshint unused: false */
|
|
|
|
var count = 0,
|
|
|
|
i;
|
|
|
|
for ( i in obj ) {
|
2013-03-19 00:06:47 +01:00
|
|
|
count++;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
},
|
|
|
|
|
|
|
|
hideErrors: function() {
|
|
|
|
this.addWrapper( this.toHide ).hide();
|
|
|
|
},
|
|
|
|
|
|
|
|
valid: function() {
|
|
|
|
return this.size() === 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
size: function() {
|
|
|
|
return this.errorList.length;
|
|
|
|
},
|
|
|
|
|
|
|
|
focusInvalid: function() {
|
|
|
|
if ( this.settings.focusInvalid ) {
|
|
|
|
try {
|
2014-04-16 01:48:10 +02:00
|
|
|
$( this.findLastActive() || this.errorList.length && this.errorList[ 0 ].element || [])
|
|
|
|
.filter( ":visible" )
|
2013-03-19 00:06:47 +01:00
|
|
|
.focus()
|
|
|
|
// manually trigger focusin event; without it, focusin handler isn't called, findLastActive won't have anything to find
|
2014-04-16 01:48:10 +02:00
|
|
|
.trigger( "focusin" );
|
|
|
|
} catch( e ) {
|
2013-03-19 00:06:47 +01:00
|
|
|
// ignore IE throwing errors when focusing hidden elements
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
findLastActive: function() {
|
|
|
|
var lastActive = this.lastActive;
|
2014-04-16 01:48:10 +02:00
|
|
|
return lastActive && $.grep( this.errorList, function( n ) {
|
2013-03-19 00:06:47 +01:00
|
|
|
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)
|
2014-04-16 01:48:10 +02:00
|
|
|
return $( this.currentForm )
|
|
|
|
.find( "input, select, textarea" )
|
|
|
|
.not( ":submit, :reset, :image, [disabled]" )
|
2013-03-19 00:06:47 +01:00
|
|
|
.not( this.settings.ignore )
|
2014-04-16 01:48:10 +02:00
|
|
|
.filter( function() {
|
2013-03-19 00:06:47 +01:00
|
|
|
if ( !this.name && validator.settings.debug && window.console ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
console.error( "%o has no name assigned", this );
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// select only the first element for each name, and only those with rules specified
|
2014-04-16 01:48:10 +02:00
|
|
|
if ( this.name in rulesCache || !validator.objectLength( $( this ).rules() ) ) {
|
2013-03-19 00:06:47 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
rulesCache[ this.name ] = true;
|
2013-03-19 00:06:47 +01:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
clean: function( selector ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
return $( selector )[ 0 ];
|
2013-03-19 00:06:47 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
errors: function() {
|
2014-04-16 01:48:10 +02:00
|
|
|
var errorClass = this.settings.errorClass.split( " " ).join( "." );
|
|
|
|
return $( this.settings.errorElement + "." + errorClass, this.errorContext );
|
2013-03-19 00:06:47 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
reset: function() {
|
|
|
|
this.successList = [];
|
|
|
|
this.errorList = [];
|
|
|
|
this.errorMap = {};
|
2014-04-16 01:48:10 +02:00
|
|
|
this.toShow = $( [] );
|
|
|
|
this.toHide = $( [] );
|
|
|
|
this.currentElements = $( [] );
|
2013-03-19 00:06:47 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
prepareForm: function() {
|
|
|
|
this.reset();
|
|
|
|
this.toHide = this.errors().add( this.containers );
|
|
|
|
},
|
|
|
|
|
|
|
|
prepareElement: function( element ) {
|
|
|
|
this.reset();
|
2014-04-16 01:48:10 +02:00
|
|
|
this.toHide = this.errorsFor( element );
|
2013-03-19 00:06:47 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
elementValue: function( element ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
var val,
|
|
|
|
$element = $( element ),
|
|
|
|
type = element.type;
|
2013-03-19 00:06:47 +01:00
|
|
|
|
|
|
|
if ( type === "radio" || type === "checkbox" ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
return $( "input[name='" + element.name + "']:checked" ).val();
|
|
|
|
} else if ( type === "number" && typeof element.validity !== "undefined" ) {
|
|
|
|
return element.validity.badInput ? false : $element.val();
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
val = $element.val();
|
2013-03-19 00:06:47 +01:00
|
|
|
if ( typeof val === "string" ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
return val.replace(/\r/g, "" );
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
|
|
|
return val;
|
|
|
|
},
|
|
|
|
|
|
|
|
check: function( element ) {
|
|
|
|
element = this.validationTargetFor( this.clean( element ) );
|
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
var rules = $( element ).rules(),
|
|
|
|
rulesCount = $.map( rules, function( n, i ) {
|
|
|
|
return i;
|
|
|
|
}).length,
|
|
|
|
dependencyMismatch = false,
|
|
|
|
val = this.elementValue( element ),
|
|
|
|
result, method, rule;
|
2013-03-19 00:06:47 +01:00
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
for ( method in rules ) {
|
|
|
|
rule = { method: method, parameters: rules[ method ] };
|
2013-03-19 00:06:47 +01:00
|
|
|
try {
|
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
result = $.validator.methods[ method ].call( this, val, element, rule.parameters );
|
2013-03-19 00:06:47 +01:00
|
|
|
|
|
|
|
// if a method indicates that the field is optional and therefore valid,
|
|
|
|
// don't mark it as valid when there are no other rules
|
2014-04-16 01:48:10 +02:00
|
|
|
if ( result === "dependency-mismatch" && rulesCount === 1 ) {
|
2013-03-19 00:06:47 +01:00
|
|
|
dependencyMismatch = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
dependencyMismatch = false;
|
|
|
|
|
|
|
|
if ( result === "pending" ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
this.toHide = this.toHide.not( this.errorsFor( element ) );
|
2013-03-19 00:06:47 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !result ) {
|
|
|
|
this.formatAndAdd( element, rule );
|
|
|
|
return false;
|
|
|
|
}
|
2014-04-16 01:48:10 +02:00
|
|
|
} catch( e ) {
|
2013-03-19 00:06:47 +01:00
|
|
|
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;
|
|
|
|
}
|
2014-04-16 01:48:10 +02:00
|
|
|
if ( this.objectLength( rules ) ) {
|
|
|
|
this.successList.push( element );
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
// return the custom message for the given element and validation method
|
|
|
|
// specified in the element's HTML5 data attribute
|
2014-04-16 01:48:10 +02:00
|
|
|
// return the generic message if present and no method specific message is present
|
2013-03-19 00:06:47 +01:00
|
|
|
customDataMessage: function( element, method ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
return $( element ).data( "msg" + method.charAt( 0 ).toUpperCase() +
|
|
|
|
method.substring( 1 ).toLowerCase() ) || $( element ).data( "msg" );
|
2013-03-19 00:06:47 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
// return the custom message for the given element name and validation method
|
|
|
|
customMessage: function( name, method ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
var m = this.settings.messages[ name ];
|
|
|
|
return m && ( m.constructor === String ? m : m[ method ]);
|
2013-03-19 00:06:47 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
// return the first defined argument, allowing empty strings
|
|
|
|
findDefined: function() {
|
2014-04-16 01:48:10 +02:00
|
|
|
for ( var i = 0; i < arguments.length; i++) {
|
|
|
|
if ( arguments[ i ] !== undefined ) {
|
|
|
|
return arguments[ i ];
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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,
|
2014-04-16 01:48:10 +02:00
|
|
|
$.validator.messages[ method ],
|
2013-03-19 00:06:47 +01:00
|
|
|
"<strong>Warning: No message defined for " + element.name + "</strong>"
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
formatAndAdd: function( element, rule ) {
|
|
|
|
var message = this.defaultMessage( element, rule.method ),
|
|
|
|
theregex = /\$?\{(\d+)\}/g;
|
|
|
|
if ( typeof message === "function" ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
message = message.call( this, rule.parameters, element );
|
|
|
|
} else if ( theregex.test( message ) ) {
|
|
|
|
message = $.validator.format( message.replace( theregex, "{$1}" ), rule.parameters );
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
|
|
|
this.errorList.push({
|
|
|
|
message: message,
|
2014-04-16 01:48:10 +02:00
|
|
|
element: element,
|
|
|
|
method: rule.method
|
2013-03-19 00:06:47 +01:00
|
|
|
});
|
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
this.errorMap[ element.name ] = message;
|
|
|
|
this.submitted[ element.name ] = message;
|
2013-03-19 00:06:47 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
addWrapper: function( toToggle ) {
|
|
|
|
if ( this.settings.wrapper ) {
|
|
|
|
toToggle = toToggle.add( toToggle.parent( this.settings.wrapper ) );
|
|
|
|
}
|
|
|
|
return toToggle;
|
|
|
|
},
|
|
|
|
|
|
|
|
defaultShowErrors: function() {
|
2014-04-16 01:48:10 +02:00
|
|
|
var i, elements, error;
|
|
|
|
for ( i = 0; this.errorList[ i ]; i++ ) {
|
|
|
|
error = this.errorList[ i ];
|
2013-03-19 00:06:47 +01:00
|
|
|
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 ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
for ( i = 0; this.successList[ i ]; i++ ) {
|
|
|
|
this.showLabel( this.successList[ i ] );
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( this.settings.unhighlight ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
for ( i = 0, elements = this.validElements(); elements[ i ]; i++ ) {
|
|
|
|
this.settings.unhighlight.call( this, elements[ i ], this.settings.errorClass, this.settings.validClass );
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
this.toHide = this.toHide.not( this.toShow );
|
|
|
|
this.hideErrors();
|
|
|
|
this.addWrapper( this.toShow ).show();
|
|
|
|
},
|
|
|
|
|
|
|
|
validElements: function() {
|
2014-04-16 01:48:10 +02:00
|
|
|
return this.currentElements.not( this.invalidElements() );
|
2013-03-19 00:06:47 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
invalidElements: function() {
|
2014-04-16 01:48:10 +02:00
|
|
|
return $( this.errorList ).map(function() {
|
2013-03-19 00:06:47 +01:00
|
|
|
return this.element;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
showLabel: function( element, message ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
var place, group,
|
|
|
|
error = this.errorsFor( element ),
|
|
|
|
elementID = this.idOrName( element );
|
|
|
|
if ( error.length ) {
|
2013-03-19 00:06:47 +01:00
|
|
|
// refresh error/success class
|
2014-04-16 01:48:10 +02:00
|
|
|
error.removeClass( this.settings.validClass ).addClass( this.settings.errorClass );
|
2013-03-19 00:06:47 +01:00
|
|
|
// replace message on existing label
|
2014-04-16 01:48:10 +02:00
|
|
|
error.html( message );
|
2013-03-19 00:06:47 +01:00
|
|
|
} else {
|
2014-04-16 01:48:10 +02: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;
|
2013-03-19 00:06:47 +01:00
|
|
|
if ( this.settings.wrapper ) {
|
|
|
|
// make sure the element is visible, even in IE
|
|
|
|
// actually showing the wrapped element is handled elsewhere
|
2014-04-16 01:48:10 +02:00
|
|
|
place = error.hide().show().wrap( "<" + this.settings.wrapper + "/>" ).parent();
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
2014-04-16 01:48:10 +02:00
|
|
|
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
|
|
|
|
$( element ).attr( "aria-describedby", error.attr( "id" ) );
|
|
|
|
|
|
|
|
// 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" ) );
|
|
|
|
}
|
|
|
|
});
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( !message && this.settings.success ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
error.text( "" );
|
2013-03-19 00:06:47 +01:00
|
|
|
if ( typeof this.settings.success === "string" ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
error.addClass( this.settings.success );
|
2013-03-19 00:06:47 +01:00
|
|
|
} else {
|
2014-04-16 01:48:10 +02:00
|
|
|
this.settings.success( error, element );
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
|
|
|
}
|
2014-04-16 01:48:10 +02:00
|
|
|
this.toShow = this.toShow.add( error );
|
2013-03-19 00:06:47 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
errorsFor: function( element ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
var name = this.idOrName( element ),
|
|
|
|
describer = $( element ).attr( "aria-describedby" );
|
|
|
|
if ( describer ) {
|
|
|
|
// aria-describedby should directly reference the error element
|
|
|
|
return $( "#" + describer, this.errorContext );
|
|
|
|
} else {
|
|
|
|
// If no describer is used then errors are either associated labels, or children of non-error labels
|
|
|
|
return this
|
|
|
|
.errors()
|
|
|
|
.filter( "label[for='" + name + "'], label[for='" + name + "'] *" );
|
|
|
|
}
|
2013-03-19 00:06:47 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
idOrName: function( element ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
return this.groups[ element.name ] || ( this.checkable( element ) ? element.name : element.id || element.name );
|
2013-03-19 00:06:47 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
validationTargetFor: function( element ) {
|
|
|
|
// if radio/checkbox, validate first element in group instead
|
2014-04-16 01:48:10 +02:00
|
|
|
if ( this.checkable( element ) ) {
|
|
|
|
element = this.findByName( element.name ).not( this.settings.ignore )[ 0 ];
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
|
|
|
return element;
|
|
|
|
},
|
|
|
|
|
|
|
|
checkable: function( element ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
return ( /radio|checkbox/i ).test( element.type );
|
2013-03-19 00:06:47 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
findByName: function( name ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
return $( this.currentForm ).find( "[name='" + name + "']" );
|
2013-03-19 00:06:47 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
getLength: function( value, element ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
switch ( element.nodeName.toLowerCase() ) {
|
2013-03-19 00:06:47 +01:00
|
|
|
case "select":
|
2014-04-16 01:48:10 +02:00
|
|
|
return $( "option:selected", element ).length;
|
2013-03-19 00:06:47 +01:00
|
|
|
case "input":
|
2014-04-16 01:48:10 +02:00
|
|
|
if ( this.checkable( element ) ) {
|
|
|
|
return this.findByName( element.name ).filter( ":checked" ).length;
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return value.length;
|
|
|
|
},
|
|
|
|
|
|
|
|
depend: function( param, element ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
return this.dependTypes[typeof param] ? this.dependTypes[typeof param]( param, element ) : true;
|
2013-03-19 00:06:47 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
dependTypes: {
|
2014-04-16 01:48:10 +02:00
|
|
|
"boolean": function( param ) {
|
2013-03-19 00:06:47 +01:00
|
|
|
return param;
|
|
|
|
},
|
|
|
|
"string": function( param, element ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
return !!$( param, element.form ).length;
|
2013-03-19 00:06:47 +01:00
|
|
|
},
|
|
|
|
"function": function( param, element ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
return param( element );
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
optional: function( element ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
var val = this.elementValue( element );
|
|
|
|
return !$.validator.methods.required.call( this, val, element ) && "dependency-mismatch";
|
2013-03-19 00:06:47 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
startRequest: function( element ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
if ( !this.pending[ element.name ] ) {
|
2013-03-19 00:06:47 +01:00
|
|
|
this.pendingRequest++;
|
2014-04-16 01:48:10 +02:00
|
|
|
this.pending[ element.name ] = true;
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
stopRequest: function( element, valid ) {
|
|
|
|
this.pendingRequest--;
|
|
|
|
// sometimes synchronization fails, make sure pendingRequest is never < 0
|
|
|
|
if ( this.pendingRequest < 0 ) {
|
|
|
|
this.pendingRequest = 0;
|
|
|
|
}
|
2014-04-16 01:48:10 +02:00
|
|
|
delete this.pending[ element.name ];
|
2013-03-19 00:06:47 +01:00
|
|
|
if ( valid && this.pendingRequest === 0 && this.formSubmitted && this.form() ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
$( this.currentForm ).submit();
|
2013-03-19 00:06:47 +01:00
|
|
|
this.formSubmitted = false;
|
2014-04-16 01:48:10 +02:00
|
|
|
} else if (!valid && this.pendingRequest === 0 && this.formSubmitted ) {
|
|
|
|
$( this.currentForm ).triggerHandler( "invalid-form", [ this ]);
|
2013-03-19 00:06:47 +01:00
|
|
|
this.formSubmitted = false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
previousValue: function( element ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
return $.data( element, "previousValue" ) || $.data( element, "previousValue", {
|
2013-03-19 00:06:47 +01:00
|
|
|
old: null,
|
|
|
|
valid: true,
|
|
|
|
message: this.defaultMessage( element, "remote" )
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
classRuleSettings: {
|
2014-04-16 01:48:10 +02:00
|
|
|
required: { required: true },
|
|
|
|
email: { email: true },
|
|
|
|
url: { url: true },
|
|
|
|
date: { date: true },
|
|
|
|
dateISO: { dateISO: true },
|
|
|
|
number: { number: true },
|
|
|
|
digits: { digits: true },
|
|
|
|
creditcard: { creditcard: true }
|
2013-03-19 00:06:47 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
addClassRules: function( className, rules ) {
|
|
|
|
if ( className.constructor === String ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
this.classRuleSettings[ className ] = rules;
|
2013-03-19 00:06:47 +01:00
|
|
|
} else {
|
2014-04-16 01:48:10 +02:00
|
|
|
$.extend( this.classRuleSettings, className );
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
classRules: function( element ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
var rules = {},
|
|
|
|
classes = $( element ).attr( "class" );
|
|
|
|
|
2013-03-19 00:06:47 +01:00
|
|
|
if ( classes ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
$.each( classes.split( " " ), function() {
|
2013-03-19 00:06:47 +01:00
|
|
|
if ( this in $.validator.classRuleSettings ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
$.extend( rules, $.validator.classRuleSettings[ this ]);
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return rules;
|
|
|
|
},
|
|
|
|
|
|
|
|
attributeRules: function( element ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
var rules = {},
|
|
|
|
$element = $( element ),
|
|
|
|
type = element.getAttribute( "type" ),
|
|
|
|
method, value;
|
2013-03-19 00:06:47 +01:00
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
for ( method in $.validator.methods ) {
|
2013-03-19 00:06:47 +01:00
|
|
|
|
|
|
|
// support for <input required> in both html5 and older browsers
|
|
|
|
if ( method === "required" ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
value = element.getAttribute( method );
|
2013-03-19 00:06:47 +01:00
|
|
|
// 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 {
|
2014-04-16 01:48:10 +02:00
|
|
|
value = $element.attr( method );
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
|
|
|
|
2014-04-16 01:48:10 +02: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
|
|
|
|
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;
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
// maxlength may be returned as -1, 2147483647 ( IE ) and 524288 ( safari ) for text inputs
|
|
|
|
if ( rules.maxlength && /-1|2147483647|524288/.test( rules.maxlength ) ) {
|
2013-03-19 00:06:47 +01:00
|
|
|
delete rules.maxlength;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rules;
|
|
|
|
},
|
|
|
|
|
|
|
|
dataRules: function( element ) {
|
|
|
|
var method, value,
|
2014-04-16 01:48:10 +02:00
|
|
|
rules = {}, $element = $( element );
|
|
|
|
for ( method in $.validator.methods ) {
|
|
|
|
value = $element.data( "rule" + method.charAt( 0 ).toUpperCase() + method.substring( 1 ).toLowerCase() );
|
2013-03-19 00:06:47 +01:00
|
|
|
if ( value !== undefined ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
rules[ method ] = value;
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return rules;
|
|
|
|
},
|
|
|
|
|
|
|
|
staticRules: function( element ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
var rules = {},
|
|
|
|
validator = $.data( element.form, "validator" );
|
|
|
|
|
2013-03-19 00:06:47 +01:00
|
|
|
if ( validator.settings.rules ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
rules = $.validator.normalizeRule( validator.settings.rules[ element.name ] ) || {};
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
|
|
|
return rules;
|
|
|
|
},
|
|
|
|
|
|
|
|
normalizeRules: function( rules, element ) {
|
|
|
|
// handle dependency check
|
2014-04-16 01:48:10 +02:00
|
|
|
$.each( rules, function( prop, val ) {
|
2013-03-19 00:06:47 +01:00
|
|
|
// ignore rule when param is explicitly false, eg. required:false
|
|
|
|
if ( val === false ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
delete rules[ prop ];
|
2013-03-19 00:06:47 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( val.param || val.depends ) {
|
|
|
|
var keepRule = true;
|
2014-04-16 01:48:10 +02:00
|
|
|
switch ( typeof val.depends ) {
|
2013-03-19 00:06:47 +01:00
|
|
|
case "string":
|
2014-04-16 01:48:10 +02:00
|
|
|
keepRule = !!$( val.depends, element.form ).length;
|
2013-03-19 00:06:47 +01:00
|
|
|
break;
|
|
|
|
case "function":
|
2014-04-16 01:48:10 +02:00
|
|
|
keepRule = val.depends.call( element, element );
|
2013-03-19 00:06:47 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ( keepRule ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
rules[ prop ] = val.param !== undefined ? val.param : true;
|
2013-03-19 00:06:47 +01:00
|
|
|
} else {
|
2014-04-16 01:48:10 +02:00
|
|
|
delete rules[ prop ];
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// evaluate parameters
|
2014-04-16 01:48:10 +02:00
|
|
|
$.each( rules, function( rule, parameter ) {
|
|
|
|
rules[ rule ] = $.isFunction( parameter ) ? parameter( element ) : parameter;
|
2013-03-19 00:06:47 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// clean number parameters
|
2014-04-16 01:48:10 +02:00
|
|
|
$.each([ "minlength", "maxlength" ], function() {
|
|
|
|
if ( rules[ this ] ) {
|
|
|
|
rules[ this ] = Number( rules[ this ] );
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
|
|
|
});
|
2014-04-16 01:48:10 +02:00
|
|
|
$.each([ "rangelength", "range" ], function() {
|
2013-03-19 00:06:47 +01:00
|
|
|
var parts;
|
2014-04-16 01:48:10 +02:00
|
|
|
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 ] ) ];
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if ( $.validator.autoCreateRanges ) {
|
|
|
|
// auto-create ranges
|
|
|
|
if ( rules.min && rules.max ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
rules.range = [ rules.min, rules.max ];
|
2013-03-19 00:06:47 +01:00
|
|
|
delete rules.min;
|
|
|
|
delete rules.max;
|
|
|
|
}
|
|
|
|
if ( rules.minlength && rules.maxlength ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
rules.rangelength = [ rules.minlength, rules.maxlength ];
|
2013-03-19 00:06:47 +01:00
|
|
|
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 = {};
|
2014-04-16 01:48:10 +02:00
|
|
|
$.each( data.split( /\s/ ), function() {
|
|
|
|
transformed[ this ] = true;
|
2013-03-19 00:06:47 +01:00
|
|
|
});
|
|
|
|
data = transformed;
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
},
|
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
// http://jqueryvalidation.org/jQuery.validator.addMethod/
|
2013-03-19 00:06:47 +01:00
|
|
|
addMethod: function( name, method, message ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
$.validator.methods[ name ] = method;
|
|
|
|
$.validator.messages[ name ] = message !== undefined ? message : $.validator.messages[ name ];
|
2013-03-19 00:06:47 +01:00
|
|
|
if ( method.length < 3 ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
$.validator.addClassRules( name, $.validator.normalizeRule( name ) );
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
methods: {
|
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
// http://jqueryvalidation.org/required-method/
|
2013-03-19 00:06:47 +01:00
|
|
|
required: function( value, element, param ) {
|
|
|
|
// check if dependency is met
|
2014-04-16 01:48:10 +02:00
|
|
|
if ( !this.depend( param, element ) ) {
|
2013-03-19 00:06:47 +01:00
|
|
|
return "dependency-mismatch";
|
|
|
|
}
|
|
|
|
if ( element.nodeName.toLowerCase() === "select" ) {
|
|
|
|
// could be an array for select-multiple or a string, both are fine this way
|
2014-04-16 01:48:10 +02:00
|
|
|
var val = $( element ).val();
|
2013-03-19 00:06:47 +01:00
|
|
|
return val && val.length > 0;
|
|
|
|
}
|
2014-04-16 01:48:10 +02:00
|
|
|
if ( this.checkable( element ) ) {
|
|
|
|
return this.getLength( value, element ) > 0;
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
2014-04-16 01:48:10 +02:00
|
|
|
return $.trim( value ).length > 0;
|
2013-03-19 00:06:47 +01:00
|
|
|
},
|
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
// http://jqueryvalidation.org/email-method/
|
2013-03-19 00:06:47 +01:00
|
|
|
email: function( value, element ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
// 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 );
|
2013-03-19 00:06:47 +01:00
|
|
|
},
|
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
// http://jqueryvalidation.org/url-method/
|
2013-03-19 00:06:47 +01:00
|
|
|
url: function( value, element ) {
|
|
|
|
// contributed by Scott Gonzalez: http://projects.scottsplayground.com/iri/
|
2014-04-16 01:48:10 +02:00
|
|
|
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 );
|
2013-03-19 00:06:47 +01:00
|
|
|
},
|
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
// http://jqueryvalidation.org/date-method/
|
2013-03-19 00:06:47 +01:00
|
|
|
date: function( value, element ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
return this.optional( element ) || !/Invalid|NaN/.test( new Date( value ).toString() );
|
2013-03-19 00:06:47 +01:00
|
|
|
},
|
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
// http://jqueryvalidation.org/dateISO-method/
|
2013-03-19 00:06:47 +01:00
|
|
|
dateISO: function( value, element ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
return this.optional( element ) || /^\d{4}[\/\-]\d{1,2}[\/\-]\d{1,2}$/.test( value );
|
2013-03-19 00:06:47 +01:00
|
|
|
},
|
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
// http://jqueryvalidation.org/number-method/
|
2013-03-19 00:06:47 +01:00
|
|
|
number: function( value, element ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
return this.optional( element ) || /^-?(?:\d+|\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/.test( value );
|
2013-03-19 00:06:47 +01:00
|
|
|
},
|
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
// http://jqueryvalidation.org/digits-method/
|
2013-03-19 00:06:47 +01:00
|
|
|
digits: function( value, element ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
return this.optional( element ) || /^\d+$/.test( value );
|
2013-03-19 00:06:47 +01:00
|
|
|
},
|
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
// http://jqueryvalidation.org/creditcard-method/
|
|
|
|
// based on http://en.wikipedia.org/wiki/Luhn/
|
2013-03-19 00:06:47 +01:00
|
|
|
creditcard: function( value, element ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
if ( this.optional( element ) ) {
|
2013-03-19 00:06:47 +01:00
|
|
|
return "dependency-mismatch";
|
|
|
|
}
|
|
|
|
// accept only spaces, digits and dashes
|
2014-04-16 01:48:10 +02:00
|
|
|
if ( /[^0-9 \-]+/.test( value ) ) {
|
2013-03-19 00:06:47 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var nCheck = 0,
|
|
|
|
nDigit = 0,
|
2014-04-16 01:48:10 +02:00
|
|
|
bEven = false,
|
|
|
|
n, cDigit;
|
2013-03-19 00:06:47 +01:00
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
value = value.replace( /\D/g, "" );
|
2013-03-19 00:06:47 +01:00
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
// 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 );
|
2013-03-19 00:06:47 +01:00
|
|
|
if ( bEven ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
if ( ( nDigit *= 2 ) > 9 ) {
|
2013-03-19 00:06:47 +01:00
|
|
|
nDigit -= 9;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nCheck += nDigit;
|
|
|
|
bEven = !bEven;
|
|
|
|
}
|
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
return ( nCheck % 10 ) === 0;
|
2013-03-19 00:06:47 +01:00
|
|
|
},
|
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
// http://jqueryvalidation.org/minlength-method/
|
2013-03-19 00:06:47 +01:00
|
|
|
minlength: function( value, element, param ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
var length = $.isArray( value ) ? value.length : this.getLength( $.trim( value ), element );
|
|
|
|
return this.optional( element ) || length >= param;
|
2013-03-19 00:06:47 +01:00
|
|
|
},
|
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
// http://jqueryvalidation.org/maxlength-method/
|
2013-03-19 00:06:47 +01:00
|
|
|
maxlength: function( value, element, param ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
var length = $.isArray( value ) ? value.length : this.getLength( $.trim( value ), element );
|
|
|
|
return this.optional( element ) || length <= param;
|
2013-03-19 00:06:47 +01:00
|
|
|
},
|
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
// http://jqueryvalidation.org/rangelength-method/
|
2013-03-19 00:06:47 +01:00
|
|
|
rangelength: function( value, element, param ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
var length = $.isArray( value ) ? value.length : this.getLength( $.trim( value ), element );
|
|
|
|
return this.optional( element ) || ( length >= param[ 0 ] && length <= param[ 1 ] );
|
2013-03-19 00:06:47 +01:00
|
|
|
},
|
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
// http://jqueryvalidation.org/min-method/
|
2013-03-19 00:06:47 +01:00
|
|
|
min: function( value, element, param ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
return this.optional( element ) || value >= param;
|
2013-03-19 00:06:47 +01:00
|
|
|
},
|
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
// http://jqueryvalidation.org/max-method/
|
2013-03-19 00:06:47 +01:00
|
|
|
max: function( value, element, param ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
return this.optional( element ) || value <= param;
|
2013-03-19 00:06:47 +01:00
|
|
|
},
|
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
// http://jqueryvalidation.org/range-method/
|
2013-03-19 00:06:47 +01:00
|
|
|
range: function( value, element, param ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
return this.optional( element ) || ( value >= param[ 0 ] && value <= param[ 1 ] );
|
2013-03-19 00:06:47 +01:00
|
|
|
},
|
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
// http://jqueryvalidation.org/equalTo-method/
|
2013-03-19 00:06:47 +01:00
|
|
|
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
|
2014-04-16 01:48:10 +02:00
|
|
|
var target = $( param );
|
2013-03-19 00:06:47 +01:00
|
|
|
if ( this.settings.onfocusout ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
target.unbind( ".validate-equalTo" ).bind( "blur.validate-equalTo", function() {
|
|
|
|
$( element ).valid();
|
2013-03-19 00:06:47 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return value === target.val();
|
|
|
|
},
|
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
// http://jqueryvalidation.org/remote-method/
|
2013-03-19 00:06:47 +01:00
|
|
|
remote: function( value, element, param ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
if ( this.optional( element ) ) {
|
2013-03-19 00:06:47 +01:00
|
|
|
return "dependency-mismatch";
|
|
|
|
}
|
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
var previous = this.previousValue( element ),
|
|
|
|
validator, data;
|
|
|
|
|
|
|
|
if (!this.settings.messages[ element.name ] ) {
|
|
|
|
this.settings.messages[ element.name ] = {};
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
2014-04-16 01:48:10 +02:00
|
|
|
previous.originalMessage = this.settings.messages[ element.name ].remote;
|
|
|
|
this.settings.messages[ element.name ].remote = previous.message;
|
2013-03-19 00:06:47 +01:00
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
param = typeof param === "string" && { url: param } || param;
|
2013-03-19 00:06:47 +01:00
|
|
|
|
|
|
|
if ( previous.old === value ) {
|
|
|
|
return previous.valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
previous.old = value;
|
2014-04-16 01:48:10 +02:00
|
|
|
validator = this;
|
|
|
|
this.startRequest( element );
|
|
|
|
data = {};
|
|
|
|
data[ element.name ] = value;
|
|
|
|
$.ajax( $.extend( true, {
|
2013-03-19 00:06:47 +01:00
|
|
|
url: param,
|
|
|
|
mode: "abort",
|
|
|
|
port: "validate" + element.name,
|
|
|
|
dataType: "json",
|
|
|
|
data: data,
|
2014-04-16 01:48:10 +02:00
|
|
|
context: validator.currentForm,
|
2013-03-19 00:06:47 +01:00
|
|
|
success: function( response ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
var valid = response === true || response === "true",
|
|
|
|
errors, message, submitted;
|
|
|
|
|
|
|
|
validator.settings.messages[ element.name ].remote = previous.originalMessage;
|
2013-03-19 00:06:47 +01:00
|
|
|
if ( valid ) {
|
2014-04-16 01:48:10 +02:00
|
|
|
submitted = validator.formSubmitted;
|
|
|
|
validator.prepareElement( element );
|
2013-03-19 00:06:47 +01:00
|
|
|
validator.formSubmitted = submitted;
|
2014-04-16 01:48:10 +02:00
|
|
|
validator.successList.push( element );
|
|
|
|
delete validator.invalid[ element.name ];
|
2013-03-19 00:06:47 +01:00
|
|
|
validator.showErrors();
|
|
|
|
} else {
|
2014-04-16 01:48:10 +02:00
|
|
|
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 );
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
|
|
|
previous.valid = valid;
|
2014-04-16 01:48:10 +02:00
|
|
|
validator.stopRequest( element, valid );
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
2014-04-16 01:48:10 +02:00
|
|
|
}, param ) );
|
2013-03-19 00:06:47 +01:00
|
|
|
return "pending";
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
$.format = function deprecated() {
|
|
|
|
throw "$.format has been deprecated. Please use $.validator.format instead.";
|
|
|
|
};
|
2013-03-19 00:06:47 +01:00
|
|
|
|
|
|
|
// 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()
|
2014-04-16 01:48:10 +02:00
|
|
|
|
|
|
|
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();
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
2014-04-16 01:48:10 +02:00
|
|
|
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();
|
2013-03-19 00:06:47 +01:00
|
|
|
}
|
2014-04-16 01:48:10 +02:00
|
|
|
pendingRequests[port] = ajax.apply(this, arguments);
|
|
|
|
return pendingRequests[port];
|
|
|
|
}
|
|
|
|
return ajax.apply(this, arguments);
|
|
|
|
};
|
|
|
|
}
|
2013-03-19 00:06:47 +01:00
|
|
|
|
|
|
|
// 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
|
2014-04-16 01:48:10 +02:00
|
|
|
|
|
|
|
$.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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}));
|