提交 | 用户 | 时间
|
58d006
|
1 |
/*! |
A |
2 |
* jQuery Validation Plugin v1.14.0 |
|
3 |
* |
|
4 |
* http://jqueryvalidation.org/ |
|
5 |
* |
|
6 |
* Copyright (c) 2015 Jörn Zaefferer |
|
7 |
* Released under the MIT license |
|
8 |
*/ |
|
9 |
(function( factory ) { |
|
10 |
if ( typeof define === "function" && define.amd ) { |
|
11 |
define( ["jquery"], factory ); |
|
12 |
} else { |
|
13 |
factory( jQuery ); |
|
14 |
} |
|
15 |
}(function( $ ) { |
|
16 |
|
|
17 |
$.extend($.fn, { |
|
18 |
// http://jqueryvalidation.org/validate/ |
|
19 |
validate: function( options ) { |
|
20 |
|
|
21 |
// if nothing is selected, return nothing; can't chain anyway |
|
22 |
if ( !this.length ) { |
|
23 |
if ( options && options.debug && window.console ) { |
|
24 |
console.warn( "Nothing selected, can't validate, returning nothing." ); |
|
25 |
} |
|
26 |
return; |
|
27 |
} |
|
28 |
|
|
29 |
// check if a validator for this form was already created |
|
30 |
var validator = $.data( this[ 0 ], "validator" ); |
|
31 |
if ( validator ) { |
|
32 |
return validator; |
|
33 |
} |
|
34 |
|
|
35 |
// Add novalidate tag if HTML5. |
|
36 |
this.attr( "novalidate", "novalidate" ); |
|
37 |
|
|
38 |
validator = new $.validator( options, this[ 0 ] ); |
|
39 |
$.data( this[ 0 ], "validator", validator ); |
|
40 |
|
|
41 |
if ( validator.settings.onsubmit ) { |
|
42 |
|
|
43 |
this.on( "click.validate", ":submit", function( event ) { |
|
44 |
if ( validator.settings.submitHandler ) { |
|
45 |
validator.submitButton = event.target; |
|
46 |
} |
|
47 |
|
|
48 |
// allow suppressing validation by adding a cancel class to the submit button |
|
49 |
if ( $( this ).hasClass( "cancel" ) ) { |
|
50 |
validator.cancelSubmit = true; |
|
51 |
} |
|
52 |
|
|
53 |
// allow suppressing validation by adding the html5 formnovalidate attribute to the submit button |
|
54 |
if ( $( this ).attr( "formnovalidate" ) !== undefined ) { |
|
55 |
validator.cancelSubmit = true; |
|
56 |
} |
|
57 |
}); |
|
58 |
|
|
59 |
// validate the form on submit |
|
60 |
this.on( "submit.validate", function( event ) { |
|
61 |
if ( validator.settings.debug ) { |
|
62 |
// prevent form submit to be able to see console output |
|
63 |
event.preventDefault(); |
|
64 |
} |
|
65 |
function handle() { |
|
66 |
var hidden, result; |
|
67 |
if ( validator.settings.submitHandler ) { |
|
68 |
if ( validator.submitButton ) { |
|
69 |
// insert a hidden input as a replacement for the missing submit button |
|
70 |
hidden = $( "<input type='hidden'/>" ) |
|
71 |
.attr( "name", validator.submitButton.name ) |
|
72 |
.val( $( validator.submitButton ).val() ) |
|
73 |
.appendTo( validator.currentForm ); |
|
74 |
} |
|
75 |
result = validator.settings.submitHandler.call( validator, validator.currentForm, event ); |
|
76 |
if ( validator.submitButton ) { |
|
77 |
// and clean up afterwards; thanks to no-block-scope, hidden can be referenced |
|
78 |
hidden.remove(); |
|
79 |
} |
|
80 |
if ( result !== undefined ) { |
|
81 |
return result; |
|
82 |
} |
|
83 |
return false; |
|
84 |
} |
|
85 |
return true; |
|
86 |
} |
|
87 |
|
|
88 |
// prevent submit for invalid forms or custom submit handlers |
|
89 |
if ( validator.cancelSubmit ) { |
|
90 |
validator.cancelSubmit = false; |
|
91 |
return handle(); |
|
92 |
} |
|
93 |
if ( validator.form() ) { |
|
94 |
if ( validator.pendingRequest ) { |
|
95 |
validator.formSubmitted = true; |
|
96 |
return false; |
|
97 |
} |
|
98 |
return handle(); |
|
99 |
} else { |
|
100 |
validator.focusInvalid(); |
|
101 |
return false; |
|
102 |
} |
|
103 |
}); |
|
104 |
} |
|
105 |
|
|
106 |
return validator; |
|
107 |
}, |
|
108 |
// http://jqueryvalidation.org/valid/ |
|
109 |
valid: function() { |
|
110 |
var valid, validator, errorList; |
|
111 |
|
|
112 |
if ( $( this[ 0 ] ).is( "form" ) ) { |
|
113 |
valid = this.validate().form(); |
|
114 |
} else { |
|
115 |
errorList = []; |
|
116 |
valid = true; |
|
117 |
validator = $( this[ 0 ].form ).validate(); |
|
118 |
this.each( function() { |
|
119 |
valid = validator.element( this ) && valid; |
|
120 |
errorList = errorList.concat( validator.errorList ); |
|
121 |
}); |
|
122 |
validator.errorList = errorList; |
|
123 |
} |
|
124 |
return valid; |
|
125 |
}, |
|
126 |
|
|
127 |
// http://jqueryvalidation.org/rules/ |
|
128 |
rules: function( command, argument ) { |
|
129 |
var element = this[ 0 ], |
|
130 |
settings, staticRules, existingRules, data, param, filtered; |
|
131 |
|
|
132 |
if ( command ) { |
|
133 |
settings = $.data( element.form, "validator" ).settings; |
|
134 |
staticRules = settings.rules; |
|
135 |
existingRules = $.validator.staticRules( element ); |
|
136 |
switch ( command ) { |
|
137 |
case "add": |
|
138 |
$.extend( existingRules, $.validator.normalizeRule( argument ) ); |
|
139 |
// remove messages from rules, but allow them to be set separately |
|
140 |
delete existingRules.messages; |
|
141 |
staticRules[ element.name ] = existingRules; |
|
142 |
if ( argument.messages ) { |
|
143 |
settings.messages[ element.name ] = $.extend( settings.messages[ element.name ], argument.messages ); |
|
144 |
} |
|
145 |
break; |
|
146 |
case "remove": |
|
147 |
if ( !argument ) { |
|
148 |
delete staticRules[ element.name ]; |
|
149 |
return existingRules; |
|
150 |
} |
|
151 |
filtered = {}; |
|
152 |
$.each( argument.split( /\s/ ), function( index, method ) { |
|
153 |
filtered[ method ] = existingRules[ method ]; |
|
154 |
delete existingRules[ method ]; |
|
155 |
if ( method === "required" ) { |
|
156 |
$( element ).removeAttr( "aria-required" ); |
|
157 |
} |
|
158 |
}); |
|
159 |
return filtered; |
|
160 |
} |
|
161 |
} |
|
162 |
|
|
163 |
data = $.validator.normalizeRules( |
|
164 |
$.extend( |
|
165 |
{}, |
|
166 |
$.validator.classRules( element ), |
|
167 |
$.validator.attributeRules( element ), |
|
168 |
$.validator.dataRules( element ), |
|
169 |
$.validator.staticRules( element ) |
|
170 |
), element ); |
|
171 |
|
|
172 |
// make sure required is at front |
|
173 |
if ( data.required ) { |
|
174 |
param = data.required; |
|
175 |
delete data.required; |
|
176 |
data = $.extend( { required: param }, data ); |
|
177 |
$( element ).attr( "aria-required", "true" ); |
|
178 |
} |
|
179 |
|
|
180 |
// make sure remote is at back |
|
181 |
if ( data.remote ) { |
|
182 |
param = data.remote; |
|
183 |
delete data.remote; |
|
184 |
data = $.extend( data, { remote: param }); |
|
185 |
} |
|
186 |
|
|
187 |
return data; |
|
188 |
} |
|
189 |
}); |
|
190 |
|
|
191 |
// Custom selectors |
|
192 |
$.extend( $.expr[ ":" ], { |
|
193 |
// http://jqueryvalidation.org/blank-selector/ |
|
194 |
blank: function( a ) { |
|
195 |
return !$.trim( "" + $( a ).val() ); |
|
196 |
}, |
|
197 |
// http://jqueryvalidation.org/filled-selector/ |
|
198 |
filled: function( a ) { |
|
199 |
return !!$.trim( "" + $( a ).val() ); |
|
200 |
}, |
|
201 |
// http://jqueryvalidation.org/unchecked-selector/ |
|
202 |
unchecked: function( a ) { |
|
203 |
return !$( a ).prop( "checked" ); |
|
204 |
} |
|
205 |
}); |
|
206 |
|
|
207 |
// constructor for validator |
|
208 |
$.validator = function( options, form ) { |
|
209 |
this.settings = $.extend( true, {}, $.validator.defaults, options ); |
|
210 |
this.currentForm = form; |
|
211 |
this.init(); |
|
212 |
}; |
|
213 |
|
|
214 |
// http://jqueryvalidation.org/jQuery.validator.format/ |
|
215 |
$.validator.format = function( source, params ) { |
|
216 |
if ( arguments.length === 1 ) { |
|
217 |
return function() { |
|
218 |
var args = $.makeArray( arguments ); |
|
219 |
args.unshift( source ); |
|
220 |
return $.validator.format.apply( this, args ); |
|
221 |
}; |
|
222 |
} |
|
223 |
if ( arguments.length > 2 && params.constructor !== Array ) { |
|
224 |
params = $.makeArray( arguments ).slice( 1 ); |
|
225 |
} |
|
226 |
if ( params.constructor !== Array ) { |
|
227 |
params = [ params ]; |
|
228 |
} |
|
229 |
$.each( params, function( i, n ) { |
|
230 |
source = source.replace( new RegExp( "\\{" + i + "\\}", "g" ), function() { |
|
231 |
return n; |
|
232 |
}); |
|
233 |
}); |
|
234 |
return source; |
|
235 |
}; |
|
236 |
|
|
237 |
$.extend( $.validator, { |
|
238 |
|
|
239 |
defaults: { |
|
240 |
messages: {}, |
|
241 |
groups: {}, |
|
242 |
rules: {}, |
|
243 |
errorClass: "error", |
|
244 |
validClass: "valid", |
|
245 |
errorElement: "label", |
|
246 |
focusCleanup: false, |
|
247 |
focusInvalid: true, |
|
248 |
errorContainer: $( [] ), |
|
249 |
errorLabelContainer: $( [] ), |
|
250 |
onsubmit: true, |
|
251 |
ignore: ":hidden", |
|
252 |
ignoreTitle: false, |
|
253 |
onfocusin: function( element ) { |
|
254 |
this.lastActive = element; |
|
255 |
|
|
256 |
// Hide error label and remove error class on focus if enabled |
|
257 |
if ( this.settings.focusCleanup ) { |
|
258 |
if ( this.settings.unhighlight ) { |
|
259 |
this.settings.unhighlight.call( this, element, this.settings.errorClass, this.settings.validClass ); |
|
260 |
} |
|
261 |
this.hideThese( this.errorsFor( element ) ); |
|
262 |
} |
|
263 |
}, |
|
264 |
onfocusout: function( element ) { |
|
265 |
if ( !this.checkable( element ) && ( element.name in this.submitted || !this.optional( element ) ) ) { |
|
266 |
this.element( element ); |
|
267 |
} |
|
268 |
}, |
|
269 |
onkeyup: function( element, event ) { |
|
270 |
// Avoid revalidate the field when pressing one of the following keys |
|
271 |
// Shift => 16 |
|
272 |
// Ctrl => 17 |
|
273 |
// Alt => 18 |
|
274 |
// Caps lock => 20 |
|
275 |
// End => 35 |
|
276 |
// Home => 36 |
|
277 |
// Left arrow => 37 |
|
278 |
// Up arrow => 38 |
|
279 |
// Right arrow => 39 |
|
280 |
// Down arrow => 40 |
|
281 |
// Insert => 45 |
|
282 |
// Num lock => 144 |
|
283 |
// AltGr key => 225 |
|
284 |
var excludedKeys = [ |
|
285 |
16, 17, 18, 20, 35, 36, 37, |
|
286 |
38, 39, 40, 45, 144, 225 |
|
287 |
]; |
|
288 |
|
|
289 |
if ( event.which === 9 && this.elementValue( element ) === "" || $.inArray( event.keyCode, excludedKeys ) !== -1 ) { |
|
290 |
return; |
|
291 |
} else if ( element.name in this.submitted || element === this.lastElement ) { |
|
292 |
this.element( element ); |
|
293 |
} |
|
294 |
}, |
|
295 |
onclick: function( element ) { |
|
296 |
// click on selects, radiobuttons and checkboxes |
|
297 |
if ( element.name in this.submitted ) { |
|
298 |
this.element( element ); |
|
299 |
|
|
300 |
// or option elements, check parent select in that case |
|
301 |
} else if ( element.parentNode.name in this.submitted ) { |
|
302 |
this.element( element.parentNode ); |
|
303 |
} |
|
304 |
}, |
|
305 |
highlight: function( element, errorClass, validClass ) { |
|
306 |
if ( element.type === "radio" ) { |
|
307 |
this.findByName( element.name ).addClass( errorClass ).removeClass( validClass ); |
|
308 |
} else { |
|
309 |
$( element ).addClass( errorClass ).removeClass( validClass ); |
|
310 |
} |
|
311 |
}, |
|
312 |
unhighlight: function( element, errorClass, validClass ) { |
|
313 |
if ( element.type === "radio" ) { |
|
314 |
this.findByName( element.name ).removeClass( errorClass ).addClass( validClass ); |
|
315 |
} else { |
|
316 |
$( element ).removeClass( errorClass ).addClass( validClass ); |
|
317 |
} |
|
318 |
} |
|
319 |
}, |
|
320 |
|
|
321 |
// http://jqueryvalidation.org/jQuery.validator.setDefaults/ |
|
322 |
setDefaults: function( settings ) { |
|
323 |
$.extend( $.validator.defaults, settings ); |
|
324 |
}, |
|
325 |
|
|
326 |
messages: { |
|
327 |
required: "This field is required.", |
|
328 |
remote: "Please fix this field.", |
|
329 |
email: "Please enter a valid email address.", |
|
330 |
url: "Please enter a valid URL.", |
|
331 |
date: "Please enter a valid date.", |
|
332 |
dateISO: "Please enter a valid date ( ISO ).", |
|
333 |
number: "Please enter a valid number.", |
|
334 |
digits: "Please enter only digits.", |
|
335 |
creditcard: "Please enter a valid credit card number.", |
|
336 |
equalTo: "Please enter the same value again.", |
|
337 |
maxlength: $.validator.format( "Please enter no more than {0} characters." ), |
|
338 |
minlength: $.validator.format( "Please enter at least {0} characters." ), |
|
339 |
rangelength: $.validator.format( "Please enter a value between {0} and {1} characters long." ), |
|
340 |
range: $.validator.format( "Please enter a value between {0} and {1}." ), |
|
341 |
max: $.validator.format( "Please enter a value less than or equal to {0}." ), |
|
342 |
min: $.validator.format( "Please enter a value greater than or equal to {0}." ) |
|
343 |
}, |
|
344 |
|
|
345 |
autoCreateRanges: false, |
|
346 |
|
|
347 |
prototype: { |
|
348 |
|
|
349 |
init: function() { |
|
350 |
this.labelContainer = $( this.settings.errorLabelContainer ); |
|
351 |
this.errorContext = this.labelContainer.length && this.labelContainer || $( this.currentForm ); |
|
352 |
this.containers = $( this.settings.errorContainer ).add( this.settings.errorLabelContainer ); |
|
353 |
this.submitted = {}; |
|
354 |
this.valueCache = {}; |
|
355 |
this.pendingRequest = 0; |
|
356 |
this.pending = {}; |
|
357 |
this.invalid = {}; |
|
358 |
this.reset(); |
|
359 |
|
|
360 |
var groups = ( this.groups = {} ), |
|
361 |
rules; |
|
362 |
$.each( this.settings.groups, function( key, value ) { |
|
363 |
if ( typeof value === "string" ) { |
|
364 |
value = value.split( /\s/ ); |
|
365 |
} |
|
366 |
$.each( value, function( index, name ) { |
|
367 |
groups[ name ] = key; |
|
368 |
}); |
|
369 |
}); |
|
370 |
rules = this.settings.rules; |
|
371 |
$.each( rules, function( key, value ) { |
|
372 |
rules[ key ] = $.validator.normalizeRule( value ); |
|
373 |
}); |
|
374 |
|
|
375 |
function delegate( event ) { |
|
376 |
var validator = $.data( this.form, "validator" ), |
|
377 |
eventType = "on" + event.type.replace( /^validate/, "" ), |
|
378 |
settings = validator.settings; |
|
379 |
if ( settings[ eventType ] && !$( this ).is( settings.ignore ) ) { |
|
380 |
settings[ eventType ].call( validator, this, event ); |
|
381 |
} |
|
382 |
} |
|
383 |
|
|
384 |
$( this.currentForm ) |
|
385 |
.on( "focusin.validate focusout.validate keyup.validate", |
|
386 |
":text, [type='password'], [type='file'], select, textarea, [type='number'], [type='search'], " + |
|
387 |
"[type='tel'], [type='url'], [type='email'], [type='datetime'], [type='date'], [type='month'], " + |
|
388 |
"[type='week'], [type='time'], [type='datetime-local'], [type='range'], [type='color'], " + |
|
389 |
"[type='radio'], [type='checkbox']", delegate) |
|
390 |
// Support: Chrome, oldIE |
|
391 |
// "select" is provided as event.target when clicking a option |
|
392 |
.on("click.validate", "select, option, [type='radio'], [type='checkbox']", delegate); |
|
393 |
|
|
394 |
if ( this.settings.invalidHandler ) { |
|
395 |
$( this.currentForm ).on( "invalid-form.validate", this.settings.invalidHandler ); |
|
396 |
} |
|
397 |
|
|
398 |
// Add aria-required to any Static/Data/Class required fields before first validation |
|
399 |
// Screen readers require this attribute to be present before the initial submission http://www.w3.org/TR/WCAG-TECHS/ARIA2.html |
|
400 |
$( this.currentForm ).find( "[required], [data-rule-required], .required" ).attr( "aria-required", "true" ); |
|
401 |
}, |
|
402 |
|
|
403 |
// http://jqueryvalidation.org/Validator.form/ |
|
404 |
form: function() { |
|
405 |
this.checkForm(); |
|
406 |
$.extend( this.submitted, this.errorMap ); |
|
407 |
this.invalid = $.extend({}, this.errorMap ); |
|
408 |
if ( !this.valid() ) { |
|
409 |
$( this.currentForm ).triggerHandler( "invalid-form", [ this ]); |
|
410 |
} |
|
411 |
this.showErrors(); |
|
412 |
return this.valid(); |
|
413 |
}, |
|
414 |
|
|
415 |
checkForm: function() { |
|
416 |
this.prepareForm(); |
|
417 |
for ( var i = 0, elements = ( this.currentElements = this.elements() ); elements[ i ]; i++ ) { |
|
418 |
this.check( elements[ i ] ); |
|
419 |
} |
|
420 |
return this.valid(); |
|
421 |
}, |
|
422 |
|
|
423 |
// http://jqueryvalidation.org/Validator.element/ |
|
424 |
element: function( element ) { |
|
425 |
var cleanElement = this.clean( element ), |
|
426 |
checkElement = this.validationTargetFor( cleanElement ), |
|
427 |
result = true; |
|
428 |
|
|
429 |
this.lastElement = checkElement; |
|
430 |
|
|
431 |
if ( checkElement === undefined ) { |
|
432 |
delete this.invalid[ cleanElement.name ]; |
|
433 |
} else { |
|
434 |
this.prepareElement( checkElement ); |
|
435 |
this.currentElements = $( checkElement ); |
|
436 |
|
|
437 |
result = this.check( checkElement ) !== false; |
|
438 |
if ( result ) { |
|
439 |
delete this.invalid[ checkElement.name ]; |
|
440 |
} else { |
|
441 |
this.invalid[ checkElement.name ] = true; |
|
442 |
} |
|
443 |
} |
|
444 |
// Add aria-invalid status for screen readers |
|
445 |
$( element ).attr( "aria-invalid", !result ); |
|
446 |
|
|
447 |
if ( !this.numberOfInvalids() ) { |
|
448 |
// Hide error containers on last error |
|
449 |
this.toHide = this.toHide.add( this.containers ); |
|
450 |
} |
|
451 |
this.showErrors(); |
|
452 |
return result; |
|
453 |
}, |
|
454 |
|
|
455 |
// http://jqueryvalidation.org/Validator.showErrors/ |
|
456 |
showErrors: function( errors ) { |
|
457 |
if ( errors ) { |
|
458 |
// add items to error list and map |
|
459 |
$.extend( this.errorMap, errors ); |
|
460 |
this.errorList = []; |
|
461 |
for ( var name in errors ) { |
|
462 |
this.errorList.push({ |
|
463 |
message: errors[ name ], |
|
464 |
element: this.findByName( name )[ 0 ] |
|
465 |
}); |
|
466 |
} |
|
467 |
// remove items from success list |
|
468 |
this.successList = $.grep( this.successList, function( element ) { |
|
469 |
return !( element.name in errors ); |
|
470 |
}); |
|
471 |
} |
|
472 |
if ( this.settings.showErrors ) { |
|
473 |
this.settings.showErrors.call( this, this.errorMap, this.errorList ); |
|
474 |
} else { |
|
475 |
this.defaultShowErrors(); |
|
476 |
} |
|
477 |
}, |
|
478 |
|
|
479 |
// http://jqueryvalidation.org/Validator.resetForm/ |
|
480 |
resetForm: function() { |
|
481 |
if ( $.fn.resetForm ) { |
|
482 |
$( this.currentForm ).resetForm(); |
|
483 |
} |
|
484 |
this.submitted = {}; |
|
485 |
this.lastElement = null; |
|
486 |
this.prepareForm(); |
|
487 |
this.hideErrors(); |
|
488 |
var i, elements = this.elements() |
|
489 |
.removeData( "previousValue" ) |
|
490 |
.removeAttr( "aria-invalid" ); |
|
491 |
|
|
492 |
if ( this.settings.unhighlight ) { |
|
493 |
for ( i = 0; elements[ i ]; i++ ) { |
|
494 |
this.settings.unhighlight.call( this, elements[ i ], |
|
495 |
this.settings.errorClass, "" ); |
|
496 |
} |
|
497 |
} else { |
|
498 |
elements.removeClass( this.settings.errorClass ); |
|
499 |
} |
|
500 |
}, |
|
501 |
|
|
502 |
numberOfInvalids: function() { |
|
503 |
return this.objectLength( this.invalid ); |
|
504 |
}, |
|
505 |
|
|
506 |
objectLength: function( obj ) { |
|
507 |
/* jshint unused: false */ |
|
508 |
var count = 0, |
|
509 |
i; |
|
510 |
for ( i in obj ) { |
|
511 |
count++; |
|
512 |
} |
|
513 |
return count; |
|
514 |
}, |
|
515 |
|
|
516 |
hideErrors: function() { |
|
517 |
this.hideThese( this.toHide ); |
|
518 |
}, |
|
519 |
|
|
520 |
hideThese: function( errors ) { |
|
521 |
errors.not( this.containers ).text( "" ); |
|
522 |
this.addWrapper( errors ).hide(); |
|
523 |
}, |
|
524 |
|
|
525 |
valid: function() { |
|
526 |
return this.size() === 0; |
|
527 |
}, |
|
528 |
|
|
529 |
size: function() { |
|
530 |
return this.errorList.length; |
|
531 |
}, |
|
532 |
|
|
533 |
focusInvalid: function() { |
|
534 |
if ( this.settings.focusInvalid ) { |
|
535 |
try { |
|
536 |
$( this.findLastActive() || this.errorList.length && this.errorList[ 0 ].element || []) |
|
537 |
.filter( ":visible" ) |
|
538 |
.focus() |
|
539 |
// manually trigger focusin event; without it, focusin handler isn't called, findLastActive won't have anything to find |
|
540 |
.trigger( "focusin" ); |
|
541 |
} catch ( e ) { |
|
542 |
// ignore IE throwing errors when focusing hidden elements |
|
543 |
} |
|
544 |
} |
|
545 |
}, |
|
546 |
|
|
547 |
findLastActive: function() { |
|
548 |
var lastActive = this.lastActive; |
|
549 |
return lastActive && $.grep( this.errorList, function( n ) { |
|
550 |
return n.element.name === lastActive.name; |
|
551 |
}).length === 1 && lastActive; |
|
552 |
}, |
|
553 |
|
|
554 |
elements: function() { |
|
555 |
var validator = this, |
|
556 |
rulesCache = {}; |
|
557 |
|
|
558 |
// select all valid inputs inside the form (no submit or reset buttons) |
|
559 |
return $( this.currentForm ) |
|
560 |
.find( "input, select, textarea" ) |
|
561 |
.not( ":submit, :reset, :image, :disabled" ) |
|
562 |
.not( this.settings.ignore ) |
|
563 |
.filter( function() { |
|
564 |
if ( !this.name && validator.settings.debug && window.console ) { |
|
565 |
console.error( "%o has no name assigned", this ); |
|
566 |
} |
|
567 |
|
|
568 |
// select only the first element for each name, and only those with rules specified |
|
569 |
if ( this.name in rulesCache || !validator.objectLength( $( this ).rules() ) ) { |
|
570 |
return false; |
|
571 |
} |
|
572 |
|
|
573 |
rulesCache[ this.name ] = true; |
|
574 |
return true; |
|
575 |
}); |
|
576 |
}, |
|
577 |
|
|
578 |
clean: function( selector ) { |
|
579 |
return $( selector )[ 0 ]; |
|
580 |
}, |
|
581 |
|
|
582 |
errors: function() { |
|
583 |
var errorClass = this.settings.errorClass.split( " " ).join( "." ); |
|
584 |
return $( this.settings.errorElement + "." + errorClass, this.errorContext ); |
|
585 |
}, |
|
586 |
|
|
587 |
reset: function() { |
|
588 |
this.successList = []; |
|
589 |
this.errorList = []; |
|
590 |
this.errorMap = {}; |
|
591 |
this.toShow = $( [] ); |
|
592 |
this.toHide = $( [] ); |
|
593 |
this.currentElements = $( [] ); |
|
594 |
}, |
|
595 |
|
|
596 |
prepareForm: function() { |
|
597 |
this.reset(); |
|
598 |
this.toHide = this.errors().add( this.containers ); |
|
599 |
}, |
|
600 |
|
|
601 |
prepareElement: function( element ) { |
|
602 |
this.reset(); |
|
603 |
this.toHide = this.errorsFor( element ); |
|
604 |
}, |
|
605 |
|
|
606 |
elementValue: function( element ) { |
|
607 |
var val, |
|
608 |
$element = $( element ), |
|
609 |
type = element.type; |
|
610 |
|
|
611 |
if ( type === "radio" || type === "checkbox" ) { |
|
612 |
return this.findByName( element.name ).filter(":checked").val(); |
|
613 |
} else if ( type === "number" && typeof element.validity !== "undefined" ) { |
|
614 |
return element.validity.badInput ? false : $element.val(); |
|
615 |
} |
|
616 |
|
|
617 |
val = $element.val(); |
|
618 |
if ( typeof val === "string" ) { |
|
619 |
return val.replace(/\r/g, "" ); |
|
620 |
} |
|
621 |
return val; |
|
622 |
}, |
|
623 |
|
|
624 |
check: function( element ) { |
|
625 |
element = this.validationTargetFor( this.clean( element ) ); |
|
626 |
|
|
627 |
var rules = $( element ).rules(), |
|
628 |
rulesCount = $.map( rules, function( n, i ) { |
|
629 |
return i; |
|
630 |
}).length, |
|
631 |
dependencyMismatch = false, |
|
632 |
val = this.elementValue( element ), |
|
633 |
result, method, rule; |
|
634 |
|
|
635 |
for ( method in rules ) { |
|
636 |
rule = { method: method, parameters: rules[ method ] }; |
|
637 |
try { |
|
638 |
|
|
639 |
result = $.validator.methods[ method ].call( this, val, element, rule.parameters ); |
|
640 |
|
|
641 |
// if a method indicates that the field is optional and therefore valid, |
|
642 |
// don't mark it as valid when there are no other rules |
|
643 |
if ( result === "dependency-mismatch" && rulesCount === 1 ) { |
|
644 |
dependencyMismatch = true; |
|
645 |
continue; |
|
646 |
} |
|
647 |
dependencyMismatch = false; |
|
648 |
|
|
649 |
if ( result === "pending" ) { |
|
650 |
this.toHide = this.toHide.not( this.errorsFor( element ) ); |
|
651 |
return; |
|
652 |
} |
|
653 |
|
|
654 |
if ( !result ) { |
|
655 |
this.formatAndAdd( element, rule ); |
|
656 |
return false; |
|
657 |
} |
|
658 |
} catch ( e ) { |
|
659 |
if ( this.settings.debug && window.console ) { |
|
660 |
console.log( "Exception occurred when checking element " + element.id + ", check the '" + rule.method + "' method.", e ); |
|
661 |
} |
|
662 |
if ( e instanceof TypeError ) { |
|
663 |
e.message += ". Exception occurred when checking element " + element.id + ", check the '" + rule.method + "' method."; |
|
664 |
} |
|
665 |
|
|
666 |
throw e; |
|
667 |
} |
|
668 |
} |
|
669 |
if ( dependencyMismatch ) { |
|
670 |
return; |
|
671 |
} |
|
672 |
if ( this.objectLength( rules ) ) { |
|
673 |
this.successList.push( element ); |
|
674 |
} |
|
675 |
return true; |
|
676 |
}, |
|
677 |
|
|
678 |
// return the custom message for the given element and validation method |
|
679 |
// specified in the element's HTML5 data attribute |
|
680 |
// return the generic message if present and no method specific message is present |
|
681 |
customDataMessage: function( element, method ) { |
|
682 |
return $( element ).data( "msg" + method.charAt( 0 ).toUpperCase() + |
|
683 |
method.substring( 1 ).toLowerCase() ) || $( element ).data( "msg" ); |
|
684 |
}, |
|
685 |
|
|
686 |
// return the custom message for the given element name and validation method |
|
687 |
customMessage: function( name, method ) { |
|
688 |
var m = this.settings.messages[ name ]; |
|
689 |
return m && ( m.constructor === String ? m : m[ method ]); |
|
690 |
}, |
|
691 |
|
|
692 |
// return the first defined argument, allowing empty strings |
|
693 |
findDefined: function() { |
|
694 |
for ( var i = 0; i < arguments.length; i++) { |
|
695 |
if ( arguments[ i ] !== undefined ) { |
|
696 |
return arguments[ i ]; |
|
697 |
} |
|
698 |
} |
|
699 |
return undefined; |
|
700 |
}, |
|
701 |
|
|
702 |
defaultMessage: function( element, method ) { |
|
703 |
return this.findDefined( |
|
704 |
this.customMessage( element.name, method ), |
|
705 |
this.customDataMessage( element, method ), |
|
706 |
// title is never undefined, so handle empty string as undefined |
|
707 |
!this.settings.ignoreTitle && element.title || undefined, |
|
708 |
$.validator.messages[ method ], |
|
709 |
"<strong>Warning: No message defined for " + element.name + "</strong>" |
|
710 |
); |
|
711 |
}, |
|
712 |
|
|
713 |
formatAndAdd: function( element, rule ) { |
|
714 |
var message = this.defaultMessage( element, rule.method ), |
|
715 |
theregex = /\$?\{(\d+)\}/g; |
|
716 |
if ( typeof message === "function" ) { |
|
717 |
message = message.call( this, rule.parameters, element ); |
|
718 |
} else if ( theregex.test( message ) ) { |
|
719 |
message = $.validator.format( message.replace( theregex, "{$1}" ), rule.parameters ); |
|
720 |
} |
|
721 |
this.errorList.push({ |
|
722 |
message: message, |
|
723 |
element: element, |
|
724 |
method: rule.method |
|
725 |
}); |
|
726 |
|
|
727 |
this.errorMap[ element.name ] = message; |
|
728 |
this.submitted[ element.name ] = message; |
|
729 |
}, |
|
730 |
|
|
731 |
addWrapper: function( toToggle ) { |
|
732 |
if ( this.settings.wrapper ) { |
|
733 |
toToggle = toToggle.add( toToggle.parent( this.settings.wrapper ) ); |
|
734 |
} |
|
735 |
return toToggle; |
|
736 |
}, |
|
737 |
|
|
738 |
defaultShowErrors: function() { |
|
739 |
var i, elements, error; |
|
740 |
for ( i = 0; this.errorList[ i ]; i++ ) { |
|
741 |
error = this.errorList[ i ]; |
|
742 |
if ( this.settings.highlight ) { |
|
743 |
this.settings.highlight.call( this, error.element, this.settings.errorClass, this.settings.validClass ); |
|
744 |
} |
|
745 |
this.showLabel( error.element, error.message ); |
|
746 |
} |
|
747 |
if ( this.errorList.length ) { |
|
748 |
this.toShow = this.toShow.add( this.containers ); |
|
749 |
} |
|
750 |
if ( this.settings.success ) { |
|
751 |
for ( i = 0; this.successList[ i ]; i++ ) { |
|
752 |
this.showLabel( this.successList[ i ] ); |
|
753 |
} |
|
754 |
} |
|
755 |
if ( this.settings.unhighlight ) { |
|
756 |
for ( i = 0, elements = this.validElements(); elements[ i ]; i++ ) { |
|
757 |
this.settings.unhighlight.call( this, elements[ i ], this.settings.errorClass, this.settings.validClass ); |
|
758 |
} |
|
759 |
} |
|
760 |
this.toHide = this.toHide.not( this.toShow ); |
|
761 |
this.hideErrors(); |
|
762 |
this.addWrapper( this.toShow ).show(); |
|
763 |
}, |
|
764 |
|
|
765 |
validElements: function() { |
|
766 |
return this.currentElements.not( this.invalidElements() ); |
|
767 |
}, |
|
768 |
|
|
769 |
invalidElements: function() { |
|
770 |
return $( this.errorList ).map(function() { |
|
771 |
return this.element; |
|
772 |
}); |
|
773 |
}, |
|
774 |
|
|
775 |
showLabel: function( element, message ) { |
|
776 |
var place, group, errorID, |
|
777 |
error = this.errorsFor( element ), |
|
778 |
elementID = this.idOrName( element ), |
|
779 |
describedBy = $( element ).attr( "aria-describedby" ); |
|
780 |
if ( error.length ) { |
|
781 |
// refresh error/success class |
|
782 |
error.removeClass( this.settings.validClass ).addClass( this.settings.errorClass ); |
|
783 |
// replace message on existing label |
|
784 |
error.html( message ); |
|
785 |
} else { |
|
786 |
// create error element |
|
787 |
error = $( "<" + this.settings.errorElement + ">" ) |
|
788 |
.attr( "id", elementID + "-error" ) |
|
789 |
.addClass( this.settings.errorClass ) |
|
790 |
.html( message || "" ); |
|
791 |
|
|
792 |
// Maintain reference to the element to be placed into the DOM |
|
793 |
place = error; |
|
794 |
if ( this.settings.wrapper ) { |
|
795 |
// make sure the element is visible, even in IE |
|
796 |
// actually showing the wrapped element is handled elsewhere |
|
797 |
place = error.hide().show().wrap( "<" + this.settings.wrapper + "/>" ).parent(); |
|
798 |
} |
|
799 |
if ( this.labelContainer.length ) { |
|
800 |
this.labelContainer.append( place ); |
|
801 |
} else if ( this.settings.errorPlacement ) { |
|
802 |
this.settings.errorPlacement( place, $( element ) ); |
|
803 |
} else { |
|
804 |
place.insertAfter( element ); |
|
805 |
} |
|
806 |
|
|
807 |
// Link error back to the element |
|
808 |
if ( error.is( "label" ) ) { |
|
809 |
// If the error is a label, then associate using 'for' |
|
810 |
error.attr( "for", elementID ); |
|
811 |
} else if ( error.parents( "label[for='" + elementID + "']" ).length === 0 ) { |
|
812 |
// If the element is not a child of an associated label, then it's necessary |
|
813 |
// to explicitly apply aria-describedby |
|
814 |
|
|
815 |
errorID = error.attr( "id" ).replace( /(:|\.|\[|\]|\$)/g, "\\$1"); |
|
816 |
// Respect existing non-error aria-describedby |
|
817 |
if ( !describedBy ) { |
|
818 |
describedBy = errorID; |
|
819 |
} else if ( !describedBy.match( new RegExp( "\\b" + errorID + "\\b" ) ) ) { |
|
820 |
// Add to end of list if not already present |
|
821 |
describedBy += " " + errorID; |
|
822 |
} |
|
823 |
$( element ).attr( "aria-describedby", describedBy ); |
|
824 |
|
|
825 |
// If this element is grouped, then assign to all elements in the same group |
|
826 |
group = this.groups[ element.name ]; |
|
827 |
if ( group ) { |
|
828 |
$.each( this.groups, function( name, testgroup ) { |
|
829 |
if ( testgroup === group ) { |
|
830 |
$( "[name='" + name + "']", this.currentForm ) |
|
831 |
.attr( "aria-describedby", error.attr( "id" ) ); |
|
832 |
} |
|
833 |
}); |
|
834 |
} |
|
835 |
} |
|
836 |
} |
|
837 |
if ( !message && this.settings.success ) { |
|
838 |
error.text( "" ); |
|
839 |
if ( typeof this.settings.success === "string" ) { |
|
840 |
error.addClass( this.settings.success ); |
|
841 |
} else { |
|
842 |
this.settings.success( error, element ); |
|
843 |
} |
|
844 |
} |
|
845 |
this.toShow = this.toShow.add( error ); |
|
846 |
}, |
|
847 |
|
|
848 |
errorsFor: function( element ) { |
|
849 |
var name = this.idOrName( element ), |
|
850 |
describer = $( element ).attr( "aria-describedby" ), |
|
851 |
selector = "label[for='" + name + "'], label[for='" + name + "'] *"; |
|
852 |
|
|
853 |
// aria-describedby should directly reference the error element |
|
854 |
if ( describer ) { |
|
855 |
selector = selector + ", #" + describer.replace( /\s+/g, ", #" ); |
|
856 |
} |
|
857 |
return this |
|
858 |
.errors() |
|
859 |
.filter( selector ); |
|
860 |
}, |
|
861 |
|
|
862 |
idOrName: function( element ) { |
|
863 |
return this.groups[ element.name ] || ( this.checkable( element ) ? element.name : element.id || element.name ); |
|
864 |
}, |
|
865 |
|
|
866 |
validationTargetFor: function( element ) { |
|
867 |
|
|
868 |
// If radio/checkbox, validate first element in group instead |
|
869 |
if ( this.checkable( element ) ) { |
|
870 |
element = this.findByName( element.name ); |
|
871 |
} |
|
872 |
|
|
873 |
// Always apply ignore filter |
|
874 |
return $( element ).not( this.settings.ignore )[ 0 ]; |
|
875 |
}, |
|
876 |
|
|
877 |
checkable: function( element ) { |
|
878 |
return ( /radio|checkbox/i ).test( element.type ); |
|
879 |
}, |
|
880 |
|
|
881 |
findByName: function( name ) { |
|
882 |
return $( this.currentForm ).find( "[name='" + name + "']" ); |
|
883 |
}, |
|
884 |
|
|
885 |
getLength: function( value, element ) { |
|
886 |
switch ( element.nodeName.toLowerCase() ) { |
|
887 |
case "select": |
|
888 |
return $( "option:selected", element ).length; |
|
889 |
case "input": |
|
890 |
if ( this.checkable( element ) ) { |
|
891 |
return this.findByName( element.name ).filter( ":checked" ).length; |
|
892 |
} |
|
893 |
} |
|
894 |
return value.length; |
|
895 |
}, |
|
896 |
|
|
897 |
depend: function( param, element ) { |
|
898 |
return this.dependTypes[typeof param] ? this.dependTypes[typeof param]( param, element ) : true; |
|
899 |
}, |
|
900 |
|
|
901 |
dependTypes: { |
|
902 |
"boolean": function( param ) { |
|
903 |
return param; |
|
904 |
}, |
|
905 |
"string": function( param, element ) { |
|
906 |
return !!$( param, element.form ).length; |
|
907 |
}, |
|
908 |
"function": function( param, element ) { |
|
909 |
return param( element ); |
|
910 |
} |
|
911 |
}, |
|
912 |
|
|
913 |
optional: function( element ) { |
|
914 |
var val = this.elementValue( element ); |
|
915 |
return !$.validator.methods.required.call( this, val, element ) && "dependency-mismatch"; |
|
916 |
}, |
|
917 |
|
|
918 |
startRequest: function( element ) { |
|
919 |
if ( !this.pending[ element.name ] ) { |
|
920 |
this.pendingRequest++; |
|
921 |
this.pending[ element.name ] = true; |
|
922 |
} |
|
923 |
}, |
|
924 |
|
|
925 |
stopRequest: function( element, valid ) { |
|
926 |
this.pendingRequest--; |
|
927 |
// sometimes synchronization fails, make sure pendingRequest is never < 0 |
|
928 |
if ( this.pendingRequest < 0 ) { |
|
929 |
this.pendingRequest = 0; |
|
930 |
} |
|
931 |
delete this.pending[ element.name ]; |
|
932 |
if ( valid && this.pendingRequest === 0 && this.formSubmitted && this.form() ) { |
|
933 |
$( this.currentForm ).submit(); |
|
934 |
this.formSubmitted = false; |
|
935 |
} else if (!valid && this.pendingRequest === 0 && this.formSubmitted ) { |
|
936 |
$( this.currentForm ).triggerHandler( "invalid-form", [ this ]); |
|
937 |
this.formSubmitted = false; |
|
938 |
} |
|
939 |
}, |
|
940 |
|
|
941 |
previousValue: function( element ) { |
|
942 |
return $.data( element, "previousValue" ) || $.data( element, "previousValue", { |
|
943 |
old: null, |
|
944 |
valid: true, |
|
945 |
message: this.defaultMessage( element, "remote" ) |
|
946 |
}); |
|
947 |
}, |
|
948 |
|
|
949 |
// cleans up all forms and elements, removes validator-specific events |
|
950 |
destroy: function() { |
|
951 |
this.resetForm(); |
|
952 |
|
|
953 |
$( this.currentForm ) |
|
954 |
.off( ".validate" ) |
|
955 |
.removeData( "validator" ); |
|
956 |
} |
|
957 |
|
|
958 |
}, |
|
959 |
|
|
960 |
classRuleSettings: { |
|
961 |
required: { required: true }, |
|
962 |
email: { email: true }, |
|
963 |
url: { url: true }, |
|
964 |
date: { date: true }, |
|
965 |
dateISO: { dateISO: true }, |
|
966 |
number: { number: true }, |
|
967 |
digits: { digits: true }, |
|
968 |
creditcard: { creditcard: true } |
|
969 |
}, |
|
970 |
|
|
971 |
addClassRules: function( className, rules ) { |
|
972 |
if ( className.constructor === String ) { |
|
973 |
this.classRuleSettings[ className ] = rules; |
|
974 |
} else { |
|
975 |
$.extend( this.classRuleSettings, className ); |
|
976 |
} |
|
977 |
}, |
|
978 |
|
|
979 |
classRules: function( element ) { |
|
980 |
var rules = {}, |
|
981 |
classes = $( element ).attr( "class" ); |
|
982 |
|
|
983 |
if ( classes ) { |
|
984 |
$.each( classes.split( " " ), function() { |
|
985 |
if ( this in $.validator.classRuleSettings ) { |
|
986 |
$.extend( rules, $.validator.classRuleSettings[ this ]); |
|
987 |
} |
|
988 |
}); |
|
989 |
} |
|
990 |
return rules; |
|
991 |
}, |
|
992 |
|
|
993 |
normalizeAttributeRule: function( rules, type, method, value ) { |
|
994 |
|
|
995 |
// convert the value to a number for number inputs, and for text for backwards compability |
|
996 |
// allows type="date" and others to be compared as strings |
|
997 |
if ( /min|max/.test( method ) && ( type === null || /number|range|text/.test( type ) ) ) { |
|
998 |
value = Number( value ); |
|
999 |
|
|
1000 |
// Support Opera Mini, which returns NaN for undefined minlength |
|
1001 |
if ( isNaN( value ) ) { |
|
1002 |
value = undefined; |
|
1003 |
} |
|
1004 |
} |
|
1005 |
|
|
1006 |
if ( value || value === 0 ) { |
|
1007 |
rules[ method ] = value; |
|
1008 |
} else if ( type === method && type !== "range" ) { |
|
1009 |
|
|
1010 |
// exception: the jquery validate 'range' method |
|
1011 |
// does not test for the html5 'range' type |
|
1012 |
rules[ method ] = true; |
|
1013 |
} |
|
1014 |
}, |
|
1015 |
|
|
1016 |
attributeRules: function( element ) { |
|
1017 |
var rules = {}, |
|
1018 |
$element = $( element ), |
|
1019 |
type = element.getAttribute( "type" ), |
|
1020 |
method, value; |
|
1021 |
|
|
1022 |
for ( method in $.validator.methods ) { |
|
1023 |
|
|
1024 |
// support for <input required> in both html5 and older browsers |
|
1025 |
if ( method === "required" ) { |
|
1026 |
value = element.getAttribute( method ); |
|
1027 |
|
|
1028 |
// Some browsers return an empty string for the required attribute |
|
1029 |
// and non-HTML5 browsers might have required="" markup |
|
1030 |
if ( value === "" ) { |
|
1031 |
value = true; |
|
1032 |
} |
|
1033 |
|
|
1034 |
// force non-HTML5 browsers to return bool |
|
1035 |
value = !!value; |
|
1036 |
} else { |
|
1037 |
value = $element.attr( method ); |
|
1038 |
} |
|
1039 |
|
|
1040 |
this.normalizeAttributeRule( rules, type, method, value ); |
|
1041 |
} |
|
1042 |
|
|
1043 |
// maxlength may be returned as -1, 2147483647 ( IE ) and 524288 ( safari ) for text inputs |
|
1044 |
if ( rules.maxlength && /-1|2147483647|524288/.test( rules.maxlength ) ) { |
|
1045 |
delete rules.maxlength; |
|
1046 |
} |
|
1047 |
|
|
1048 |
return rules; |
|
1049 |
}, |
|
1050 |
|
|
1051 |
dataRules: function( element ) { |
|
1052 |
var rules = {}, |
|
1053 |
$element = $( element ), |
|
1054 |
type = element.getAttribute( "type" ), |
|
1055 |
method, value; |
|
1056 |
|
|
1057 |
for ( method in $.validator.methods ) { |
|
1058 |
value = $element.data( "rule" + method.charAt( 0 ).toUpperCase() + method.substring( 1 ).toLowerCase() ); |
|
1059 |
this.normalizeAttributeRule( rules, type, method, value ); |
|
1060 |
} |
|
1061 |
return rules; |
|
1062 |
}, |
|
1063 |
|
|
1064 |
staticRules: function( element ) { |
|
1065 |
var rules = {}, |
|
1066 |
validator = $.data( element.form, "validator" ); |
|
1067 |
|
|
1068 |
if ( validator.settings.rules ) { |
|
1069 |
rules = $.validator.normalizeRule( validator.settings.rules[ element.name ] ) || {}; |
|
1070 |
} |
|
1071 |
return rules; |
|
1072 |
}, |
|
1073 |
|
|
1074 |
normalizeRules: function( rules, element ) { |
|
1075 |
// handle dependency check |
|
1076 |
$.each( rules, function( prop, val ) { |
|
1077 |
// ignore rule when param is explicitly false, eg. required:false |
|
1078 |
if ( val === false ) { |
|
1079 |
delete rules[ prop ]; |
|
1080 |
return; |
|
1081 |
} |
|
1082 |
if ( val.param || val.depends ) { |
|
1083 |
var keepRule = true; |
|
1084 |
switch ( typeof val.depends ) { |
|
1085 |
case "string": |
|
1086 |
keepRule = !!$( val.depends, element.form ).length; |
|
1087 |
break; |
|
1088 |
case "function": |
|
1089 |
keepRule = val.depends.call( element, element ); |
|
1090 |
break; |
|
1091 |
} |
|
1092 |
if ( keepRule ) { |
|
1093 |
rules[ prop ] = val.param !== undefined ? val.param : true; |
|
1094 |
} else { |
|
1095 |
delete rules[ prop ]; |
|
1096 |
} |
|
1097 |
} |
|
1098 |
}); |
|
1099 |
|
|
1100 |
// evaluate parameters |
|
1101 |
$.each( rules, function( rule, parameter ) { |
|
1102 |
rules[ rule ] = $.isFunction( parameter ) ? parameter( element ) : parameter; |
|
1103 |
}); |
|
1104 |
|
|
1105 |
// clean number parameters |
|
1106 |
$.each([ "minlength", "maxlength" ], function() { |
|
1107 |
if ( rules[ this ] ) { |
|
1108 |
rules[ this ] = Number( rules[ this ] ); |
|
1109 |
} |
|
1110 |
}); |
|
1111 |
$.each([ "rangelength", "range" ], function() { |
|
1112 |
var parts; |
|
1113 |
if ( rules[ this ] ) { |
|
1114 |
if ( $.isArray( rules[ this ] ) ) { |
|
1115 |
rules[ this ] = [ Number( rules[ this ][ 0 ]), Number( rules[ this ][ 1 ] ) ]; |
|
1116 |
} else if ( typeof rules[ this ] === "string" ) { |
|
1117 |
parts = rules[ this ].replace(/[\[\]]/g, "" ).split( /[\s,]+/ ); |
|
1118 |
rules[ this ] = [ Number( parts[ 0 ]), Number( parts[ 1 ] ) ]; |
|
1119 |
} |
|
1120 |
} |
|
1121 |
}); |
|
1122 |
|
|
1123 |
if ( $.validator.autoCreateRanges ) { |
|
1124 |
// auto-create ranges |
|
1125 |
if ( rules.min != null && rules.max != null ) { |
|
1126 |
rules.range = [ rules.min, rules.max ]; |
|
1127 |
delete rules.min; |
|
1128 |
delete rules.max; |
|
1129 |
} |
|
1130 |
if ( rules.minlength != null && rules.maxlength != null ) { |
|
1131 |
rules.rangelength = [ rules.minlength, rules.maxlength ]; |
|
1132 |
delete rules.minlength; |
|
1133 |
delete rules.maxlength; |
|
1134 |
} |
|
1135 |
} |
|
1136 |
|
|
1137 |
return rules; |
|
1138 |
}, |
|
1139 |
|
|
1140 |
// Converts a simple string to a {string: true} rule, e.g., "required" to {required:true} |
|
1141 |
normalizeRule: function( data ) { |
|
1142 |
if ( typeof data === "string" ) { |
|
1143 |
var transformed = {}; |
|
1144 |
$.each( data.split( /\s/ ), function() { |
|
1145 |
transformed[ this ] = true; |
|
1146 |
}); |
|
1147 |
data = transformed; |
|
1148 |
} |
|
1149 |
return data; |
|
1150 |
}, |
|
1151 |
|
|
1152 |
// http://jqueryvalidation.org/jQuery.validator.addMethod/ |
|
1153 |
addMethod: function( name, method, message ) { |
|
1154 |
$.validator.methods[ name ] = method; |
|
1155 |
$.validator.messages[ name ] = message !== undefined ? message : $.validator.messages[ name ]; |
|
1156 |
if ( method.length < 3 ) { |
|
1157 |
$.validator.addClassRules( name, $.validator.normalizeRule( name ) ); |
|
1158 |
} |
|
1159 |
}, |
|
1160 |
|
|
1161 |
methods: { |
|
1162 |
|
|
1163 |
// http://jqueryvalidation.org/required-method/ |
|
1164 |
required: function( value, element, param ) { |
|
1165 |
// check if dependency is met |
|
1166 |
if ( !this.depend( param, element ) ) { |
|
1167 |
return "dependency-mismatch"; |
|
1168 |
} |
|
1169 |
if ( element.nodeName.toLowerCase() === "select" ) { |
|
1170 |
// could be an array for select-multiple or a string, both are fine this way |
|
1171 |
var val = $( element ).val(); |
|
1172 |
return val && val.length > 0; |
|
1173 |
} |
|
1174 |
if ( this.checkable( element ) ) { |
|
1175 |
return this.getLength( value, element ) > 0; |
|
1176 |
} |
|
1177 |
return value.length > 0; |
|
1178 |
}, |
|
1179 |
|
|
1180 |
// http://jqueryvalidation.org/email-method/ |
|
1181 |
email: function( value, element ) { |
|
1182 |
// From https://html.spec.whatwg.org/multipage/forms.html#valid-e-mail-address |
|
1183 |
// Retrieved 2014-01-14 |
|
1184 |
// If you have a problem with this implementation, report a bug against the above spec |
|
1185 |
// Or use custom methods to implement your own email validation |
|
1186 |
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 ); |
|
1187 |
}, |
|
1188 |
|
|
1189 |
// http://jqueryvalidation.org/url-method/ |
|
1190 |
url: function( value, element ) { |
|
1191 |
|
|
1192 |
// Copyright (c) 2010-2013 Diego Perini, MIT licensed |
|
1193 |
// https://gist.github.com/dperini/729294 |
|
1194 |
// see also https://mathiasbynens.be/demo/url-regex |
|
1195 |
// modified to allow protocol-relative URLs |
|
1196 |
return this.optional( element ) || /^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})).?)(?::\d{2,5})?(?:[/?#]\S*)?$/i.test( value ); |
|
1197 |
}, |
|
1198 |
|
|
1199 |
// http://jqueryvalidation.org/date-method/ |
|
1200 |
date: function( value, element ) { |
|
1201 |
return this.optional( element ) || !/Invalid|NaN/.test( new Date( value ).toString() ); |
|
1202 |
}, |
|
1203 |
|
|
1204 |
// http://jqueryvalidation.org/dateISO-method/ |
|
1205 |
dateISO: function( value, element ) { |
|
1206 |
return this.optional( element ) || /^\d{4}[\/\-](0?[1-9]|1[012])[\/\-](0?[1-9]|[12][0-9]|3[01])$/.test( value ); |
|
1207 |
}, |
|
1208 |
|
|
1209 |
// http://jqueryvalidation.org/number-method/ |
|
1210 |
number: function( value, element ) { |
|
1211 |
return this.optional( element ) || /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/.test( value ); |
|
1212 |
}, |
|
1213 |
|
|
1214 |
// http://jqueryvalidation.org/digits-method/ |
|
1215 |
digits: function( value, element ) { |
|
1216 |
return this.optional( element ) || /^\d+$/.test( value ); |
|
1217 |
}, |
|
1218 |
|
|
1219 |
// http://jqueryvalidation.org/creditcard-method/ |
|
1220 |
// based on http://en.wikipedia.org/wiki/Luhn_algorithm |
|
1221 |
creditcard: function( value, element ) { |
|
1222 |
if ( this.optional( element ) ) { |
|
1223 |
return "dependency-mismatch"; |
|
1224 |
} |
|
1225 |
// accept only spaces, digits and dashes |
|
1226 |
if ( /[^0-9 \-]+/.test( value ) ) { |
|
1227 |
return false; |
|
1228 |
} |
|
1229 |
var nCheck = 0, |
|
1230 |
nDigit = 0, |
|
1231 |
bEven = false, |
|
1232 |
n, cDigit; |
|
1233 |
|
|
1234 |
value = value.replace( /\D/g, "" ); |
|
1235 |
|
|
1236 |
// Basing min and max length on |
|
1237 |
// http://developer.ean.com/general_info/Valid_Credit_Card_Types |
|
1238 |
if ( value.length < 13 || value.length > 19 ) { |
|
1239 |
return false; |
|
1240 |
} |
|
1241 |
|
|
1242 |
for ( n = value.length - 1; n >= 0; n--) { |
|
1243 |
cDigit = value.charAt( n ); |
|
1244 |
nDigit = parseInt( cDigit, 10 ); |
|
1245 |
if ( bEven ) { |
|
1246 |
if ( ( nDigit *= 2 ) > 9 ) { |
|
1247 |
nDigit -= 9; |
|
1248 |
} |
|
1249 |
} |
|
1250 |
nCheck += nDigit; |
|
1251 |
bEven = !bEven; |
|
1252 |
} |
|
1253 |
|
|
1254 |
return ( nCheck % 10 ) === 0; |
|
1255 |
}, |
|
1256 |
|
|
1257 |
// http://jqueryvalidation.org/minlength-method/ |
|
1258 |
minlength: function( value, element, param ) { |
|
1259 |
var length = $.isArray( value ) ? value.length : this.getLength( value, element ); |
|
1260 |
return this.optional( element ) || length >= param; |
|
1261 |
}, |
|
1262 |
|
|
1263 |
// http://jqueryvalidation.org/maxlength-method/ |
|
1264 |
maxlength: function( value, element, param ) { |
|
1265 |
var length = $.isArray( value ) ? value.length : this.getLength( value, element ); |
|
1266 |
return this.optional( element ) || length <= param; |
|
1267 |
}, |
|
1268 |
|
|
1269 |
// http://jqueryvalidation.org/rangelength-method/ |
|
1270 |
rangelength: function( value, element, param ) { |
|
1271 |
var length = $.isArray( value ) ? value.length : this.getLength( value, element ); |
|
1272 |
return this.optional( element ) || ( length >= param[ 0 ] && length <= param[ 1 ] ); |
|
1273 |
}, |
|
1274 |
|
|
1275 |
// http://jqueryvalidation.org/min-method/ |
|
1276 |
min: function( value, element, param ) { |
|
1277 |
return this.optional( element ) || value >= param; |
|
1278 |
}, |
|
1279 |
|
|
1280 |
// http://jqueryvalidation.org/max-method/ |
|
1281 |
max: function( value, element, param ) { |
|
1282 |
return this.optional( element ) || value <= param; |
|
1283 |
}, |
|
1284 |
|
|
1285 |
// http://jqueryvalidation.org/range-method/ |
|
1286 |
range: function( value, element, param ) { |
|
1287 |
return this.optional( element ) || ( value >= param[ 0 ] && value <= param[ 1 ] ); |
|
1288 |
}, |
|
1289 |
|
|
1290 |
// http://jqueryvalidation.org/equalTo-method/ |
|
1291 |
equalTo: function( value, element, param ) { |
|
1292 |
// bind to the blur event of the target in order to revalidate whenever the target field is updated |
|
1293 |
// TODO find a way to bind the event just once, avoiding the unbind-rebind overhead |
|
1294 |
var target = $( param ); |
|
1295 |
if ( this.settings.onfocusout ) { |
|
1296 |
target.off( ".validate-equalTo" ).on( "blur.validate-equalTo", function() { |
|
1297 |
$( element ).valid(); |
|
1298 |
}); |
|
1299 |
} |
|
1300 |
return value === target.val(); |
|
1301 |
}, |
|
1302 |
|
|
1303 |
// http://jqueryvalidation.org/remote-method/ |
|
1304 |
remote: function( value, element, param ) { |
|
1305 |
if ( this.optional( element ) ) { |
|
1306 |
return "dependency-mismatch"; |
|
1307 |
} |
|
1308 |
|
|
1309 |
var previous = this.previousValue( element ), |
|
1310 |
validator, data; |
|
1311 |
|
|
1312 |
if (!this.settings.messages[ element.name ] ) { |
|
1313 |
this.settings.messages[ element.name ] = {}; |
|
1314 |
} |
|
1315 |
previous.originalMessage = this.settings.messages[ element.name ].remote; |
|
1316 |
this.settings.messages[ element.name ].remote = previous.message; |
|
1317 |
|
|
1318 |
param = typeof param === "string" && { url: param } || param; |
|
1319 |
|
|
1320 |
if ( previous.old === value ) { |
|
1321 |
return previous.valid; |
|
1322 |
} |
|
1323 |
|
|
1324 |
previous.old = value; |
|
1325 |
validator = this; |
|
1326 |
this.startRequest( element ); |
|
1327 |
data = {}; |
|
1328 |
data[ element.name ] = value; |
|
1329 |
$.ajax( $.extend( true, { |
|
1330 |
mode: "abort", |
|
1331 |
port: "validate" + element.name, |
|
1332 |
dataType: "json", |
|
1333 |
data: data, |
|
1334 |
context: validator.currentForm, |
|
1335 |
success: function( response ) { |
|
1336 |
var valid = response === true || response === "true", |
|
1337 |
errors, message, submitted; |
|
1338 |
|
|
1339 |
validator.settings.messages[ element.name ].remote = previous.originalMessage; |
|
1340 |
if ( valid ) { |
|
1341 |
submitted = validator.formSubmitted; |
|
1342 |
validator.prepareElement( element ); |
|
1343 |
validator.formSubmitted = submitted; |
|
1344 |
validator.successList.push( element ); |
|
1345 |
delete validator.invalid[ element.name ]; |
|
1346 |
validator.showErrors(); |
|
1347 |
} else { |
|
1348 |
errors = {}; |
|
1349 |
message = response || validator.defaultMessage( element, "remote" ); |
|
1350 |
errors[ element.name ] = previous.message = $.isFunction( message ) ? message( value ) : message; |
|
1351 |
validator.invalid[ element.name ] = true; |
|
1352 |
validator.showErrors( errors ); |
|
1353 |
} |
|
1354 |
previous.valid = valid; |
|
1355 |
validator.stopRequest( element, valid ); |
|
1356 |
} |
|
1357 |
}, param ) ); |
|
1358 |
return "pending"; |
|
1359 |
} |
|
1360 |
} |
|
1361 |
|
|
1362 |
}); |
|
1363 |
|
|
1364 |
// ajax mode: abort |
|
1365 |
// usage: $.ajax({ mode: "abort"[, port: "uniqueport"]}); |
|
1366 |
// if mode:"abort" is used, the previous request on that port (port can be undefined) is aborted via XMLHttpRequest.abort() |
|
1367 |
|
|
1368 |
var pendingRequests = {}, |
|
1369 |
ajax; |
|
1370 |
// Use a prefilter if available (1.5+) |
|
1371 |
if ( $.ajaxPrefilter ) { |
|
1372 |
$.ajaxPrefilter(function( settings, _, xhr ) { |
|
1373 |
var port = settings.port; |
|
1374 |
if ( settings.mode === "abort" ) { |
|
1375 |
if ( pendingRequests[port] ) { |
|
1376 |
pendingRequests[port].abort(); |
|
1377 |
} |
|
1378 |
pendingRequests[port] = xhr; |
|
1379 |
} |
|
1380 |
}); |
|
1381 |
} else { |
|
1382 |
// Proxy ajax |
|
1383 |
ajax = $.ajax; |
|
1384 |
$.ajax = function( settings ) { |
|
1385 |
var mode = ( "mode" in settings ? settings : $.ajaxSettings ).mode, |
|
1386 |
port = ( "port" in settings ? settings : $.ajaxSettings ).port; |
|
1387 |
if ( mode === "abort" ) { |
|
1388 |
if ( pendingRequests[port] ) { |
|
1389 |
pendingRequests[port].abort(); |
|
1390 |
} |
|
1391 |
pendingRequests[port] = ajax.apply(this, arguments); |
|
1392 |
return pendingRequests[port]; |
|
1393 |
} |
|
1394 |
return ajax.apply(this, arguments); |
|
1395 |
}; |
|
1396 |
} |
|
1397 |
|
|
1398 |
})); |