[8384] | 1 | /* |
---|
| 2 | * This file has been commented to support Visual Studio Intellisense. |
---|
| 3 | * You should not use this file at runtime inside the browser--it is only |
---|
| 4 | * intended to be used only for design-time IntelliSense. Please use the |
---|
| 5 | * standard jQuery library for all production use. |
---|
| 6 | * |
---|
| 7 | * Comment version: 1.8 |
---|
| 8 | */ |
---|
| 9 | |
---|
| 10 | /* |
---|
| 11 | * Note: While Microsoft is not the author of this file, Microsoft is |
---|
| 12 | * offering you a license subject to the terms of the Microsoft Software |
---|
| 13 | * License Terms for Microsoft ASP.NET Model View Controller 3. |
---|
| 14 | * Microsoft reserves all other rights. The notices below are provided |
---|
| 15 | * for informational purposes only and are not the license terms under |
---|
| 16 | * which Microsoft distributed this file. |
---|
| 17 | * |
---|
| 18 | * jQuery validation plugin 1.8.0 |
---|
| 19 | * |
---|
| 20 | * http://bassistance.de/jquery-plugins/jquery-plugin-validation/ |
---|
| 21 | * http://docs.jquery.com/Plugins/Validation |
---|
| 22 | * |
---|
| 23 | * Copyright (c) 2006 - 2011 Jörn Zaefferer |
---|
| 24 | * |
---|
| 25 | */ |
---|
| 26 | |
---|
| 27 | (function($) { |
---|
| 28 | |
---|
| 29 | $.extend($.fn, { |
---|
| 30 | // http://docs.jquery.com/Plugins/Validation/validate |
---|
| 31 | validate: function( options ) { |
---|
| 32 | /// <summary> |
---|
| 33 | /// Validates the selected form. This method sets up event handlers for submit, focus, |
---|
| 34 | /// keyup, blur and click to trigger validation of the entire form or individual |
---|
| 35 | /// elements. Each one can be disabled, see the onxxx options (onsubmit, onfocusout, |
---|
| 36 | /// onkeyup, onclick). focusInvalid focuses elements when submitting a invalid form. |
---|
| 37 | /// </summary> |
---|
[9582] | 38 | /// <param name="options" type="Object"> |
---|
[8384] | 39 | /// A set of key/value pairs that configure the validate. All options are optional. |
---|
| 40 | /// </param> |
---|
| 41 | |
---|
| 42 | // if nothing is selected, return nothing; can't chain anyway |
---|
| 43 | if (!this.length) { |
---|
| 44 | options && options.debug && window.console && console.warn( "nothing selected, can't validate, returning nothing" ); |
---|
| 45 | return; |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | // check if a validator for this form was already created |
---|
| 49 | var validator = $.data(this[0], 'validator'); |
---|
| 50 | if ( validator ) { |
---|
| 51 | return validator; |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | validator = new $.validator( options, this[0] ); |
---|
| 55 | $.data(this[0], 'validator', validator); |
---|
| 56 | |
---|
| 57 | if ( validator.settings.onsubmit ) { |
---|
| 58 | |
---|
| 59 | // allow suppresing validation by adding a cancel class to the submit button |
---|
| 60 | this.find("input, button").filter(".cancel").click(function() { |
---|
| 61 | validator.cancelSubmit = true; |
---|
| 62 | }); |
---|
| 63 | |
---|
| 64 | // when a submitHandler is used, capture the submitting button |
---|
| 65 | if (validator.settings.submitHandler) { |
---|
| 66 | this.find("input, button").filter(":submit").click(function() { |
---|
| 67 | validator.submitButton = this; |
---|
| 68 | }); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | // validate the form on submit |
---|
| 72 | this.submit( function( event ) { |
---|
| 73 | if ( validator.settings.debug ) |
---|
| 74 | // prevent form submit to be able to see console output |
---|
| 75 | event.preventDefault(); |
---|
| 76 | |
---|
| 77 | function handle() { |
---|
| 78 | if ( validator.settings.submitHandler ) { |
---|
| 79 | if (validator.submitButton) { |
---|
| 80 | // insert a hidden input as a replacement for the missing submit button |
---|
| 81 | var hidden = $("<input type='hidden'/>").attr("name", validator.submitButton.name).val(validator.submitButton.value).appendTo(validator.currentForm); |
---|
| 82 | } |
---|
| 83 | validator.settings.submitHandler.call( validator, validator.currentForm ); |
---|
| 84 | if (validator.submitButton) { |
---|
| 85 | // and clean up afterwards; thanks to no-block-scope, hidden can be referenced |
---|
| 86 | hidden.remove(); |
---|
| 87 | } |
---|
| 88 | return false; |
---|
| 89 | } |
---|
| 90 | return true; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | // prevent submit for invalid forms or custom submit handlers |
---|
| 94 | if ( validator.cancelSubmit ) { |
---|
| 95 | validator.cancelSubmit = false; |
---|
| 96 | return handle(); |
---|
| 97 | } |
---|
| 98 | if ( validator.form() ) { |
---|
| 99 | if ( validator.pendingRequest ) { |
---|
| 100 | validator.formSubmitted = true; |
---|
| 101 | return false; |
---|
| 102 | } |
---|
| 103 | return handle(); |
---|
| 104 | } else { |
---|
| 105 | validator.focusInvalid(); |
---|
| 106 | return false; |
---|
| 107 | } |
---|
| 108 | }); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | return validator; |
---|
| 112 | }, |
---|
| 113 | // http://docs.jquery.com/Plugins/Validation/valid |
---|
| 114 | valid: function() { |
---|
| 115 | /// <summary> |
---|
| 116 | /// Checks if the selected form is valid or if all selected elements are valid. |
---|
| 117 | /// validate() needs to be called on the form before checking it using this method. |
---|
| 118 | /// </summary> |
---|
| 119 | /// <returns type="Boolean" /> |
---|
| 120 | |
---|
| 121 | if ( $(this[0]).is('form')) { |
---|
| 122 | return this.validate().form(); |
---|
| 123 | } else { |
---|
| 124 | var valid = true; |
---|
| 125 | var validator = $(this[0].form).validate(); |
---|
| 126 | this.each(function() { |
---|
| 127 | valid &= validator.element(this); |
---|
| 128 | }); |
---|
| 129 | return valid; |
---|
| 130 | } |
---|
| 131 | }, |
---|
| 132 | // attributes: space seperated list of attributes to retrieve and remove |
---|
| 133 | removeAttrs: function(attributes) { |
---|
| 134 | /// <summary> |
---|
| 135 | /// Remove the specified attributes from the first matched element and return them. |
---|
| 136 | /// </summary> |
---|
| 137 | /// <param name="attributes" type="String"> |
---|
| 138 | /// A space-seperated list of attribute names to remove. |
---|
| 139 | /// </param> |
---|
| 140 | |
---|
| 141 | var result = {}, |
---|
| 142 | $element = this; |
---|
| 143 | $.each(attributes.split(/\s/), function(index, value) { |
---|
| 144 | result[value] = $element.attr(value); |
---|
| 145 | $element.removeAttr(value); |
---|
| 146 | }); |
---|
| 147 | return result; |
---|
| 148 | }, |
---|
| 149 | // http://docs.jquery.com/Plugins/Validation/rules |
---|
| 150 | rules: function(command, argument) { |
---|
| 151 | /// <summary> |
---|
| 152 | /// Return the validations rules for the first selected element. |
---|
| 153 | /// </summary> |
---|
| 154 | /// <param name="command" type="String"> |
---|
| 155 | /// Can be either "add" or "remove". |
---|
| 156 | /// </param> |
---|
| 157 | /// <param name="argument" type=""> |
---|
| 158 | /// A list of rules to add or remove. |
---|
| 159 | /// </param> |
---|
| 160 | |
---|
| 161 | var element = this[0]; |
---|
| 162 | |
---|
| 163 | if (command) { |
---|
| 164 | var settings = $.data(element.form, 'validator').settings; |
---|
| 165 | var staticRules = settings.rules; |
---|
| 166 | var existingRules = $.validator.staticRules(element); |
---|
| 167 | switch(command) { |
---|
| 168 | case "add": |
---|
| 169 | $.extend(existingRules, $.validator.normalizeRule(argument)); |
---|
| 170 | staticRules[element.name] = existingRules; |
---|
| 171 | if (argument.messages) |
---|
| 172 | settings.messages[element.name] = $.extend( settings.messages[element.name], argument.messages ); |
---|
| 173 | break; |
---|
| 174 | case "remove": |
---|
| 175 | if (!argument) { |
---|
| 176 | delete staticRules[element.name]; |
---|
| 177 | return existingRules; |
---|
| 178 | } |
---|
| 179 | var filtered = {}; |
---|
| 180 | $.each(argument.split(/\s/), function(index, method) { |
---|
| 181 | filtered[method] = existingRules[method]; |
---|
| 182 | delete existingRules[method]; |
---|
| 183 | }); |
---|
| 184 | return filtered; |
---|
| 185 | } |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | var data = $.validator.normalizeRules( |
---|
| 189 | $.extend( |
---|
| 190 | {}, |
---|
| 191 | $.validator.metadataRules(element), |
---|
| 192 | $.validator.classRules(element), |
---|
| 193 | $.validator.attributeRules(element), |
---|
| 194 | $.validator.staticRules(element) |
---|
| 195 | ), element); |
---|
| 196 | |
---|
| 197 | // make sure required is at front |
---|
| 198 | if (data.required) { |
---|
| 199 | var param = data.required; |
---|
| 200 | delete data.required; |
---|
| 201 | data = $.extend({required: param}, data); |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | return data; |
---|
| 205 | } |
---|
| 206 | }); |
---|
| 207 | |
---|
| 208 | // Custom selectors |
---|
| 209 | $.extend($.expr[":"], { |
---|
| 210 | // http://docs.jquery.com/Plugins/Validation/blank |
---|
| 211 | blank: function(a) {return !$.trim("" + a.value);}, |
---|
| 212 | // http://docs.jquery.com/Plugins/Validation/filled |
---|
| 213 | filled: function(a) {return !!$.trim("" + a.value);}, |
---|
| 214 | // http://docs.jquery.com/Plugins/Validation/unchecked |
---|
| 215 | unchecked: function(a) {return !a.checked;} |
---|
| 216 | }); |
---|
| 217 | |
---|
| 218 | // constructor for validator |
---|
| 219 | $.validator = function( options, form ) { |
---|
| 220 | this.settings = $.extend( true, {}, $.validator.defaults, options ); |
---|
| 221 | this.currentForm = form; |
---|
| 222 | this.init(); |
---|
| 223 | }; |
---|
| 224 | |
---|
| 225 | $.validator.format = function(source, params) { |
---|
| 226 | /// <summary> |
---|
| 227 | /// Replaces {n} placeholders with arguments. |
---|
| 228 | /// One or more arguments can be passed, in addition to the string template itself, to insert |
---|
| 229 | /// into the string. |
---|
| 230 | /// </summary> |
---|
| 231 | /// <param name="source" type="String"> |
---|
| 232 | /// The string to format. |
---|
| 233 | /// </param> |
---|
| 234 | /// <param name="params" type="String"> |
---|
| 235 | /// The first argument to insert, or an array of Strings to insert |
---|
| 236 | /// </param> |
---|
| 237 | /// <returns type="String" /> |
---|
| 238 | |
---|
| 239 | if ( arguments.length == 1 ) |
---|
| 240 | return function() { |
---|
| 241 | var args = $.makeArray(arguments); |
---|
| 242 | args.unshift(source); |
---|
| 243 | return $.validator.format.apply( this, args ); |
---|
| 244 | }; |
---|
| 245 | if ( arguments.length > 2 && params.constructor != Array ) { |
---|
| 246 | params = $.makeArray(arguments).slice(1); |
---|
| 247 | } |
---|
| 248 | if ( params.constructor != Array ) { |
---|
| 249 | params = [ params ]; |
---|
| 250 | } |
---|
| 251 | $.each(params, function(i, n) { |
---|
| 252 | source = source.replace(new RegExp("\\{" + i + "\\}", "g"), n); |
---|
| 253 | }); |
---|
| 254 | return source; |
---|
| 255 | }; |
---|
| 256 | |
---|
| 257 | $.extend($.validator, { |
---|
| 258 | |
---|
| 259 | defaults: { |
---|
| 260 | messages: {}, |
---|
| 261 | groups: {}, |
---|
| 262 | rules: {}, |
---|
| 263 | errorClass: "error", |
---|
| 264 | validClass: "valid", |
---|
| 265 | errorElement: "label", |
---|
| 266 | focusInvalid: true, |
---|
| 267 | errorContainer: $( [] ), |
---|
| 268 | errorLabelContainer: $( [] ), |
---|
| 269 | onsubmit: true, |
---|
| 270 | ignore: [], |
---|
| 271 | ignoreTitle: false, |
---|
| 272 | onfocusin: function(element) { |
---|
| 273 | this.lastActive = element; |
---|
| 274 | |
---|
| 275 | // hide error label and remove error class on focus if enabled |
---|
| 276 | if ( this.settings.focusCleanup && !this.blockFocusCleanup ) { |
---|
| 277 | this.settings.unhighlight && this.settings.unhighlight.call( this, element, this.settings.errorClass, this.settings.validClass ); |
---|
| 278 | this.addWrapper(this.errorsFor(element)).hide(); |
---|
| 279 | } |
---|
| 280 | }, |
---|
| 281 | onfocusout: function(element) { |
---|
| 282 | if ( !this.checkable(element) && (element.name in this.submitted || !this.optional(element)) ) { |
---|
| 283 | this.element(element); |
---|
| 284 | } |
---|
| 285 | }, |
---|
| 286 | onkeyup: function(element) { |
---|
| 287 | if ( element.name in this.submitted || element == this.lastElement ) { |
---|
| 288 | this.element(element); |
---|
| 289 | } |
---|
| 290 | }, |
---|
| 291 | onclick: function(element) { |
---|
| 292 | // click on selects, radiobuttons and checkboxes |
---|
| 293 | if ( element.name in this.submitted ) |
---|
| 294 | this.element(element); |
---|
| 295 | // or option elements, check parent select in that case |
---|
| 296 | else if (element.parentNode.name in this.submitted) |
---|
| 297 | this.element(element.parentNode); |
---|
| 298 | }, |
---|
| 299 | highlight: function( element, errorClass, validClass ) { |
---|
| 300 | $(element).addClass(errorClass).removeClass(validClass); |
---|
| 301 | }, |
---|
| 302 | unhighlight: function( element, errorClass, validClass ) { |
---|
| 303 | $(element).removeClass(errorClass).addClass(validClass); |
---|
| 304 | } |
---|
| 305 | }, |
---|
| 306 | |
---|
| 307 | // http://docs.jquery.com/Plugins/Validation/Validator/setDefaults |
---|
| 308 | setDefaults: function(settings) { |
---|
| 309 | /// <summary> |
---|
| 310 | /// Modify default settings for validation. |
---|
| 311 | /// Accepts everything that Plugins/Validation/validate accepts. |
---|
| 312 | /// </summary> |
---|
| 313 | /// <param name="settings" type="Options"> |
---|
| 314 | /// Options to set as default. |
---|
| 315 | /// </param> |
---|
| 316 | |
---|
| 317 | $.extend( $.validator.defaults, settings ); |
---|
| 318 | }, |
---|
| 319 | |
---|
| 320 | messages: { |
---|
| 321 | required: "This field is required.", |
---|
| 322 | remote: "Please fix this field.", |
---|
| 323 | email: "Please enter a valid email address.", |
---|
| 324 | url: "Please enter a valid URL.", |
---|
| 325 | date: "Please enter a valid date.", |
---|
| 326 | dateISO: "Please enter a valid date (ISO).", |
---|
| 327 | number: "Please enter a valid number.", |
---|
| 328 | digits: "Please enter only digits.", |
---|
| 329 | creditcard: "Please enter a valid credit card number.", |
---|
| 330 | equalTo: "Please enter the same value again.", |
---|
| 331 | accept: "Please enter a value with a valid extension.", |
---|
| 332 | maxlength: $.validator.format("Please enter no more than {0} characters."), |
---|
| 333 | minlength: $.validator.format("Please enter at least {0} characters."), |
---|
| 334 | rangelength: $.validator.format("Please enter a value between {0} and {1} characters long."), |
---|
| 335 | range: $.validator.format("Please enter a value between {0} and {1}."), |
---|
| 336 | max: $.validator.format("Please enter a value less than or equal to {0}."), |
---|
| 337 | min: $.validator.format("Please enter a value greater than or equal to {0}.") |
---|
| 338 | }, |
---|
| 339 | |
---|
| 340 | autoCreateRanges: false, |
---|
| 341 | |
---|
| 342 | prototype: { |
---|
| 343 | |
---|
| 344 | init: function() { |
---|
| 345 | this.labelContainer = $(this.settings.errorLabelContainer); |
---|
| 346 | this.errorContext = this.labelContainer.length && this.labelContainer || $(this.currentForm); |
---|
| 347 | this.containers = $(this.settings.errorContainer).add( this.settings.errorLabelContainer ); |
---|
| 348 | this.submitted = {}; |
---|
| 349 | this.valueCache = {}; |
---|
| 350 | this.pendingRequest = 0; |
---|
| 351 | this.pending = {}; |
---|
| 352 | this.invalid = {}; |
---|
| 353 | this.reset(); |
---|
| 354 | |
---|
| 355 | var groups = (this.groups = {}); |
---|
| 356 | $.each(this.settings.groups, function(key, value) { |
---|
| 357 | $.each(value.split(/\s/), function(index, name) { |
---|
| 358 | groups[name] = key; |
---|
| 359 | }); |
---|
| 360 | }); |
---|
| 361 | var rules = this.settings.rules; |
---|
| 362 | $.each(rules, function(key, value) { |
---|
| 363 | rules[key] = $.validator.normalizeRule(value); |
---|
| 364 | }); |
---|
| 365 | |
---|
| 366 | function delegate(event) { |
---|
| 367 | var validator = $.data(this[0].form, "validator"), |
---|
| 368 | eventType = "on" + event.type.replace(/^validate/, ""); |
---|
| 369 | validator.settings[eventType] && validator.settings[eventType].call(validator, this[0] ); |
---|
| 370 | } |
---|
| 371 | $(this.currentForm) |
---|
| 372 | .validateDelegate(":text, :password, :file, select, textarea", "focusin focusout keyup", delegate) |
---|
| 373 | .validateDelegate(":radio, :checkbox, select, option", "click", delegate); |
---|
| 374 | |
---|
| 375 | if (this.settings.invalidHandler) |
---|
| 376 | $(this.currentForm).bind("invalid-form.validate", this.settings.invalidHandler); |
---|
| 377 | }, |
---|
| 378 | |
---|
| 379 | // http://docs.jquery.com/Plugins/Validation/Validator/form |
---|
| 380 | form: function() { |
---|
| 381 | /// <summary> |
---|
| 382 | /// Validates the form, returns true if it is valid, false otherwise. |
---|
| 383 | /// This behaves as a normal submit event, but returns the result. |
---|
| 384 | /// </summary> |
---|
| 385 | /// <returns type="Boolean" /> |
---|
| 386 | |
---|
| 387 | this.checkForm(); |
---|
| 388 | $.extend(this.submitted, this.errorMap); |
---|
| 389 | this.invalid = $.extend({}, this.errorMap); |
---|
| 390 | if (!this.valid()) |
---|
| 391 | $(this.currentForm).triggerHandler("invalid-form", [this]); |
---|
| 392 | this.showErrors(); |
---|
| 393 | return this.valid(); |
---|
| 394 | }, |
---|
| 395 | |
---|
| 396 | checkForm: function() { |
---|
| 397 | this.prepareForm(); |
---|
| 398 | for ( var i = 0, elements = (this.currentElements = this.elements()); elements[i]; i++ ) { |
---|
| 399 | this.check( elements[i] ); |
---|
| 400 | } |
---|
| 401 | return this.valid(); |
---|
| 402 | }, |
---|
| 403 | |
---|
| 404 | // http://docs.jquery.com/Plugins/Validation/Validator/element |
---|
| 405 | element: function( element ) { |
---|
| 406 | /// <summary> |
---|
| 407 | /// Validates a single element, returns true if it is valid, false otherwise. |
---|
| 408 | /// This behaves as validation on blur or keyup, but returns the result. |
---|
| 409 | /// </summary> |
---|
| 410 | /// <param name="element" type="Selector"> |
---|
| 411 | /// An element to validate, must be inside the validated form. |
---|
| 412 | /// </param> |
---|
| 413 | /// <returns type="Boolean" /> |
---|
| 414 | |
---|
| 415 | element = this.clean( element ); |
---|
| 416 | this.lastElement = element; |
---|
| 417 | this.prepareElement( element ); |
---|
| 418 | this.currentElements = $(element); |
---|
| 419 | var result = this.check( element ); |
---|
| 420 | if ( result ) { |
---|
| 421 | delete this.invalid[element.name]; |
---|
| 422 | } else { |
---|
| 423 | this.invalid[element.name] = true; |
---|
| 424 | } |
---|
| 425 | if ( !this.numberOfInvalids() ) { |
---|
| 426 | // Hide error containers on last error |
---|
| 427 | this.toHide = this.toHide.add( this.containers ); |
---|
| 428 | } |
---|
| 429 | this.showErrors(); |
---|
| 430 | return result; |
---|
| 431 | }, |
---|
| 432 | |
---|
| 433 | // http://docs.jquery.com/Plugins/Validation/Validator/showErrors |
---|
| 434 | showErrors: function(errors) { |
---|
| 435 | /// <summary> |
---|
| 436 | /// Show the specified messages. |
---|
| 437 | /// Keys have to refer to the names of elements, values are displayed for those elements, using the configured error placement. |
---|
| 438 | /// </summary> |
---|
| 439 | /// <param name="errors" type="Object"> |
---|
| 440 | /// One or more key/value pairs of input names and messages. |
---|
| 441 | /// </param> |
---|
| 442 | |
---|
| 443 | if(errors) { |
---|
| 444 | // add items to error list and map |
---|
| 445 | $.extend( this.errorMap, errors ); |
---|
| 446 | this.errorList = []; |
---|
| 447 | for ( var name in errors ) { |
---|
| 448 | this.errorList.push({ |
---|
| 449 | message: errors[name], |
---|
| 450 | element: this.findByName(name)[0] |
---|
| 451 | }); |
---|
| 452 | } |
---|
| 453 | // remove items from success list |
---|
| 454 | this.successList = $.grep( this.successList, function(element) { |
---|
| 455 | return !(element.name in errors); |
---|
| 456 | }); |
---|
| 457 | } |
---|
| 458 | this.settings.showErrors |
---|
| 459 | ? this.settings.showErrors.call( this, this.errorMap, this.errorList ) |
---|
| 460 | : this.defaultShowErrors(); |
---|
| 461 | }, |
---|
| 462 | |
---|
| 463 | // http://docs.jquery.com/Plugins/Validation/Validator/resetForm |
---|
| 464 | resetForm: function() { |
---|
| 465 | /// <summary> |
---|
| 466 | /// Resets the controlled form. |
---|
| 467 | /// Resets input fields to their original value (requires form plugin), removes classes |
---|
| 468 | /// indicating invalid elements and hides error messages. |
---|
| 469 | /// </summary> |
---|
| 470 | |
---|
| 471 | if ( $.fn.resetForm ) |
---|
| 472 | $( this.currentForm ).resetForm(); |
---|
| 473 | this.submitted = {}; |
---|
| 474 | this.prepareForm(); |
---|
| 475 | this.hideErrors(); |
---|
| 476 | this.elements().removeClass( this.settings.errorClass ); |
---|
| 477 | }, |
---|
| 478 | |
---|
| 479 | numberOfInvalids: function() { |
---|
| 480 | /// <summary> |
---|
| 481 | /// Returns the number of invalid fields. |
---|
| 482 | /// This depends on the internal validator state. It covers all fields only after |
---|
| 483 | /// validating the complete form (on submit or via $("form").valid()). After validating |
---|
| 484 | /// a single element, only that element is counted. Most useful in combination with the |
---|
| 485 | /// invalidHandler-option. |
---|
| 486 | /// </summary> |
---|
| 487 | /// <returns type="Number" /> |
---|
| 488 | |
---|
| 489 | return this.objectLength(this.invalid); |
---|
| 490 | }, |
---|
| 491 | |
---|
| 492 | objectLength: function( obj ) { |
---|
| 493 | var count = 0; |
---|
| 494 | for ( var i in obj ) |
---|
| 495 | count++; |
---|
| 496 | return count; |
---|
| 497 | }, |
---|
| 498 | |
---|
| 499 | hideErrors: function() { |
---|
| 500 | this.addWrapper( this.toHide ).hide(); |
---|
| 501 | }, |
---|
| 502 | |
---|
| 503 | valid: function() { |
---|
| 504 | return this.size() == 0; |
---|
| 505 | }, |
---|
| 506 | |
---|
| 507 | size: function() { |
---|
| 508 | return this.errorList.length; |
---|
| 509 | }, |
---|
| 510 | |
---|
| 511 | focusInvalid: function() { |
---|
| 512 | if( this.settings.focusInvalid ) { |
---|
| 513 | try { |
---|
| 514 | $(this.findLastActive() || this.errorList.length && this.errorList[0].element || []) |
---|
| 515 | .filter(":visible") |
---|
| 516 | .focus() |
---|
| 517 | // manually trigger focusin event; without it, focusin handler isn't called, findLastActive won't have anything to find |
---|
| 518 | .trigger("focusin"); |
---|
| 519 | } catch(e) { |
---|
| 520 | // ignore IE throwing errors when focusing hidden elements |
---|
| 521 | } |
---|
| 522 | } |
---|
| 523 | }, |
---|
| 524 | |
---|
| 525 | findLastActive: function() { |
---|
| 526 | var lastActive = this.lastActive; |
---|
| 527 | return lastActive && $.grep(this.errorList, function(n) { |
---|
| 528 | return n.element.name == lastActive.name; |
---|
| 529 | }).length == 1 && lastActive; |
---|
| 530 | }, |
---|
| 531 | |
---|
| 532 | elements: function() { |
---|
| 533 | var validator = this, |
---|
| 534 | rulesCache = {}; |
---|
| 535 | |
---|
| 536 | // select all valid inputs inside the form (no submit or reset buttons) |
---|
| 537 | // workaround $Query([]).add until http://dev.jquery.com/ticket/2114 is solved |
---|
| 538 | return $([]).add(this.currentForm.elements) |
---|
| 539 | .filter(":input") |
---|
| 540 | .not(":submit, :reset, :image, [disabled]") |
---|
| 541 | .not( this.settings.ignore ) |
---|
| 542 | .filter(function() { |
---|
| 543 | !this.name && validator.settings.debug && window.console && console.error( "%o has no name assigned", this); |
---|
| 544 | |
---|
| 545 | // select only the first element for each name, and only those with rules specified |
---|
| 546 | if ( this.name in rulesCache || !validator.objectLength($(this).rules()) ) |
---|
| 547 | return false; |
---|
| 548 | |
---|
| 549 | rulesCache[this.name] = true; |
---|
| 550 | return true; |
---|
| 551 | }); |
---|
| 552 | }, |
---|
| 553 | |
---|
| 554 | clean: function( selector ) { |
---|
| 555 | return $( selector )[0]; |
---|
| 556 | }, |
---|
| 557 | |
---|
| 558 | errors: function() { |
---|
| 559 | return $( this.settings.errorElement + "." + this.settings.errorClass, this.errorContext ); |
---|
| 560 | }, |
---|
| 561 | |
---|
| 562 | reset: function() { |
---|
| 563 | this.successList = []; |
---|
| 564 | this.errorList = []; |
---|
| 565 | this.errorMap = {}; |
---|
| 566 | this.toShow = $([]); |
---|
| 567 | this.toHide = $([]); |
---|
| 568 | this.currentElements = $([]); |
---|
| 569 | }, |
---|
| 570 | |
---|
| 571 | prepareForm: function() { |
---|
| 572 | this.reset(); |
---|
| 573 | this.toHide = this.errors().add( this.containers ); |
---|
| 574 | }, |
---|
| 575 | |
---|
| 576 | prepareElement: function( element ) { |
---|
| 577 | this.reset(); |
---|
| 578 | this.toHide = this.errorsFor(element); |
---|
| 579 | }, |
---|
| 580 | |
---|
| 581 | check: function( element ) { |
---|
| 582 | element = this.clean( element ); |
---|
| 583 | |
---|
| 584 | // if radio/checkbox, validate first element in group instead |
---|
| 585 | if (this.checkable(element)) { |
---|
| 586 | element = this.findByName(element.name).not(this.settings.ignore)[0]; |
---|
| 587 | } |
---|
| 588 | |
---|
| 589 | var rules = $(element).rules(); |
---|
| 590 | var dependencyMismatch = false; |
---|
| 591 | for (var method in rules) { |
---|
| 592 | var rule = { method: method, parameters: rules[method] }; |
---|
| 593 | try { |
---|
| 594 | var result = $.validator.methods[method].call( this, element.value.replace(/\r/g, ""), element, rule.parameters ); |
---|
| 595 | |
---|
| 596 | // if a method indicates that the field is optional and therefore valid, |
---|
| 597 | // don't mark it as valid when there are no other rules |
---|
| 598 | if ( result == "dependency-mismatch" ) { |
---|
| 599 | dependencyMismatch = true; |
---|
| 600 | continue; |
---|
| 601 | } |
---|
| 602 | dependencyMismatch = false; |
---|
| 603 | |
---|
| 604 | if ( result == "pending" ) { |
---|
| 605 | this.toHide = this.toHide.not( this.errorsFor(element) ); |
---|
| 606 | return; |
---|
| 607 | } |
---|
| 608 | |
---|
| 609 | if( !result ) { |
---|
| 610 | this.formatAndAdd( element, rule ); |
---|
| 611 | return false; |
---|
| 612 | } |
---|
| 613 | } catch(e) { |
---|
| 614 | this.settings.debug && window.console && console.log("exception occured when checking element " + element.id |
---|
| 615 | + ", check the '" + rule.method + "' method", e); |
---|
| 616 | throw e; |
---|
| 617 | } |
---|
| 618 | } |
---|
| 619 | if (dependencyMismatch) |
---|
| 620 | return; |
---|
| 621 | if ( this.objectLength(rules) ) |
---|
| 622 | this.successList.push(element); |
---|
| 623 | return true; |
---|
| 624 | }, |
---|
| 625 | |
---|
| 626 | // return the custom message for the given element and validation method |
---|
| 627 | // specified in the element's "messages" metadata |
---|
| 628 | customMetaMessage: function(element, method) { |
---|
| 629 | if (!$.metadata) |
---|
| 630 | return; |
---|
| 631 | |
---|
| 632 | var meta = this.settings.meta |
---|
| 633 | ? $(element).metadata()[this.settings.meta] |
---|
| 634 | : $(element).metadata(); |
---|
| 635 | |
---|
| 636 | return meta && meta.messages && meta.messages[method]; |
---|
| 637 | }, |
---|
| 638 | |
---|
| 639 | // return the custom message for the given element name and validation method |
---|
| 640 | customMessage: function( name, method ) { |
---|
| 641 | var m = this.settings.messages[name]; |
---|
| 642 | return m && (m.constructor == String |
---|
| 643 | ? m |
---|
| 644 | : m[method]); |
---|
| 645 | }, |
---|
| 646 | |
---|
| 647 | // return the first defined argument, allowing empty strings |
---|
| 648 | findDefined: function() { |
---|
| 649 | for(var i = 0; i < arguments.length; i++) { |
---|
| 650 | if (arguments[i] !== undefined) |
---|
| 651 | return arguments[i]; |
---|
| 652 | } |
---|
| 653 | return undefined; |
---|
| 654 | }, |
---|
| 655 | |
---|
| 656 | defaultMessage: function( element, method) { |
---|
| 657 | return this.findDefined( |
---|
| 658 | this.customMessage( element.name, method ), |
---|
| 659 | this.customMetaMessage( element, method ), |
---|
| 660 | // title is never undefined, so handle empty string as undefined |
---|
| 661 | !this.settings.ignoreTitle && element.title || undefined, |
---|
| 662 | $.validator.messages[method], |
---|
| 663 | "<strong>Warning: No message defined for " + element.name + "</strong>" |
---|
| 664 | ); |
---|
| 665 | }, |
---|
| 666 | |
---|
| 667 | formatAndAdd: function( element, rule ) { |
---|
| 668 | var message = this.defaultMessage( element, rule.method ), |
---|
| 669 | theregex = /\$?\{(\d+)\}/g; |
---|
| 670 | if ( typeof message == "function" ) { |
---|
| 671 | message = message.call(this, rule.parameters, element); |
---|
| 672 | } else if (theregex.test(message)) { |
---|
| 673 | message = jQuery.format(message.replace(theregex, '{$1}'), rule.parameters); |
---|
| 674 | } |
---|
| 675 | this.errorList.push({ |
---|
| 676 | message: message, |
---|
| 677 | element: element |
---|
| 678 | }); |
---|
| 679 | |
---|
| 680 | this.errorMap[element.name] = message; |
---|
| 681 | this.submitted[element.name] = message; |
---|
| 682 | }, |
---|
| 683 | |
---|
| 684 | addWrapper: function(toToggle) { |
---|
| 685 | if ( this.settings.wrapper ) |
---|
| 686 | toToggle = toToggle.add( toToggle.parent( this.settings.wrapper ) ); |
---|
| 687 | return toToggle; |
---|
| 688 | }, |
---|
| 689 | |
---|
| 690 | defaultShowErrors: function() { |
---|
| 691 | for ( var i = 0; this.errorList[i]; i++ ) { |
---|
| 692 | var error = this.errorList[i]; |
---|
| 693 | this.settings.highlight && this.settings.highlight.call( this, error.element, this.settings.errorClass, this.settings.validClass ); |
---|
| 694 | this.showLabel( error.element, error.message ); |
---|
| 695 | } |
---|
| 696 | if( this.errorList.length ) { |
---|
| 697 | this.toShow = this.toShow.add( this.containers ); |
---|
| 698 | } |
---|
| 699 | if (this.settings.success) { |
---|
| 700 | for ( var i = 0; this.successList[i]; i++ ) { |
---|
| 701 | this.showLabel( this.successList[i] ); |
---|
| 702 | } |
---|
| 703 | } |
---|
| 704 | if (this.settings.unhighlight) { |
---|
| 705 | for ( var i = 0, elements = this.validElements(); elements[i]; i++ ) { |
---|
| 706 | this.settings.unhighlight.call( this, elements[i], this.settings.errorClass, this.settings.validClass ); |
---|
| 707 | } |
---|
| 708 | } |
---|
| 709 | this.toHide = this.toHide.not( this.toShow ); |
---|
| 710 | this.hideErrors(); |
---|
| 711 | this.addWrapper( this.toShow ).show(); |
---|
| 712 | }, |
---|
| 713 | |
---|
| 714 | validElements: function() { |
---|
| 715 | return this.currentElements.not(this.invalidElements()); |
---|
| 716 | }, |
---|
| 717 | |
---|
| 718 | invalidElements: function() { |
---|
| 719 | return $(this.errorList).map(function() { |
---|
| 720 | return this.element; |
---|
| 721 | }); |
---|
| 722 | }, |
---|
| 723 | |
---|
| 724 | showLabel: function(element, message) { |
---|
| 725 | var label = this.errorsFor( element ); |
---|
| 726 | if ( label.length ) { |
---|
| 727 | // refresh error/success class |
---|
| 728 | label.removeClass().addClass( this.settings.errorClass ); |
---|
| 729 | |
---|
| 730 | // check if we have a generated label, replace the message then |
---|
| 731 | label.attr("generated") && label.html(message); |
---|
| 732 | } else { |
---|
| 733 | // create label |
---|
| 734 | label = $("<" + this.settings.errorElement + "/>") |
---|
| 735 | .attr({"for": this.idOrName(element), generated: true}) |
---|
| 736 | .addClass(this.settings.errorClass) |
---|
| 737 | .html(message || ""); |
---|
| 738 | if ( this.settings.wrapper ) { |
---|
| 739 | // make sure the element is visible, even in IE |
---|
| 740 | // actually showing the wrapped element is handled elsewhere |
---|
| 741 | label = label.hide().show().wrap("<" + this.settings.wrapper + "/>").parent(); |
---|
| 742 | } |
---|
| 743 | if ( !this.labelContainer.append(label).length ) |
---|
| 744 | this.settings.errorPlacement |
---|
| 745 | ? this.settings.errorPlacement(label, $(element) ) |
---|
| 746 | : label.insertAfter(element); |
---|
| 747 | } |
---|
| 748 | if ( !message && this.settings.success ) { |
---|
| 749 | label.text(""); |
---|
| 750 | typeof this.settings.success == "string" |
---|
| 751 | ? label.addClass( this.settings.success ) |
---|
| 752 | : this.settings.success( label ); |
---|
| 753 | } |
---|
| 754 | this.toShow = this.toShow.add(label); |
---|
| 755 | }, |
---|
| 756 | |
---|
| 757 | errorsFor: function(element) { |
---|
| 758 | var name = this.idOrName(element); |
---|
| 759 | return this.errors().filter(function() { |
---|
| 760 | return $(this).attr('for') == name; |
---|
| 761 | }); |
---|
| 762 | }, |
---|
| 763 | |
---|
| 764 | idOrName: function(element) { |
---|
| 765 | return this.groups[element.name] || (this.checkable(element) ? element.name : element.id || element.name); |
---|
| 766 | }, |
---|
| 767 | |
---|
| 768 | checkable: function( element ) { |
---|
| 769 | return /radio|checkbox/i.test(element.type); |
---|
| 770 | }, |
---|
| 771 | |
---|
| 772 | findByName: function( name ) { |
---|
| 773 | // select by name and filter by form for performance over form.find("[name=...]") |
---|
| 774 | var form = this.currentForm; |
---|
| 775 | return $(document.getElementsByName(name)).map(function(index, element) { |
---|
| 776 | return element.form == form && element.name == name && element || null; |
---|
| 777 | }); |
---|
| 778 | }, |
---|
| 779 | |
---|
| 780 | getLength: function(value, element) { |
---|
| 781 | switch( element.nodeName.toLowerCase() ) { |
---|
| 782 | case 'select': |
---|
| 783 | return $("option:selected", element).length; |
---|
| 784 | case 'input': |
---|
| 785 | if( this.checkable( element) ) |
---|
| 786 | return this.findByName(element.name).filter(':checked').length; |
---|
| 787 | } |
---|
| 788 | return value.length; |
---|
| 789 | }, |
---|
| 790 | |
---|
| 791 | depend: function(param, element) { |
---|
| 792 | return this.dependTypes[typeof param] |
---|
| 793 | ? this.dependTypes[typeof param](param, element) |
---|
| 794 | : true; |
---|
| 795 | }, |
---|
| 796 | |
---|
| 797 | dependTypes: { |
---|
| 798 | "boolean": function(param, element) { |
---|
| 799 | return param; |
---|
| 800 | }, |
---|
| 801 | "string": function(param, element) { |
---|
| 802 | return !!$(param, element.form).length; |
---|
| 803 | }, |
---|
| 804 | "function": function(param, element) { |
---|
| 805 | return param(element); |
---|
| 806 | } |
---|
| 807 | }, |
---|
| 808 | |
---|
| 809 | optional: function(element) { |
---|
| 810 | return !$.validator.methods.required.call(this, $.trim(element.value), element) && "dependency-mismatch"; |
---|
| 811 | }, |
---|
| 812 | |
---|
| 813 | startRequest: function(element) { |
---|
| 814 | if (!this.pending[element.name]) { |
---|
| 815 | this.pendingRequest++; |
---|
| 816 | this.pending[element.name] = true; |
---|
| 817 | } |
---|
| 818 | }, |
---|
| 819 | |
---|
| 820 | stopRequest: function(element, valid) { |
---|
| 821 | this.pendingRequest--; |
---|
| 822 | // sometimes synchronization fails, make sure pendingRequest is never < 0 |
---|
| 823 | if (this.pendingRequest < 0) |
---|
| 824 | this.pendingRequest = 0; |
---|
| 825 | delete this.pending[element.name]; |
---|
| 826 | if ( valid && this.pendingRequest == 0 && this.formSubmitted && this.form() ) { |
---|
| 827 | $(this.currentForm).submit(); |
---|
| 828 | this.formSubmitted = false; |
---|
| 829 | } else if (!valid && this.pendingRequest == 0 && this.formSubmitted) { |
---|
| 830 | $(this.currentForm).triggerHandler("invalid-form", [this]); |
---|
| 831 | this.formSubmitted = false; |
---|
| 832 | } |
---|
| 833 | }, |
---|
| 834 | |
---|
| 835 | previousValue: function(element) { |
---|
| 836 | return $.data(element, "previousValue") || $.data(element, "previousValue", { |
---|
| 837 | old: null, |
---|
| 838 | valid: true, |
---|
| 839 | message: this.defaultMessage( element, "remote" ) |
---|
| 840 | }); |
---|
| 841 | } |
---|
| 842 | |
---|
| 843 | }, |
---|
| 844 | |
---|
| 845 | classRuleSettings: { |
---|
| 846 | required: {required: true}, |
---|
| 847 | email: {email: true}, |
---|
| 848 | url: {url: true}, |
---|
| 849 | date: {date: true}, |
---|
| 850 | dateISO: {dateISO: true}, |
---|
| 851 | dateDE: {dateDE: true}, |
---|
| 852 | number: {number: true}, |
---|
| 853 | numberDE: {numberDE: true}, |
---|
| 854 | digits: {digits: true}, |
---|
| 855 | creditcard: {creditcard: true} |
---|
| 856 | }, |
---|
| 857 | |
---|
| 858 | addClassRules: function(className, rules) { |
---|
| 859 | /// <summary> |
---|
| 860 | /// Add a compound class method - useful to refactor common combinations of rules into a single |
---|
| 861 | /// class. |
---|
| 862 | /// </summary> |
---|
| 863 | /// <param name="name" type="String"> |
---|
| 864 | /// The name of the class rule to add |
---|
| 865 | /// </param> |
---|
| 866 | /// <param name="rules" type="Options"> |
---|
| 867 | /// The compound rules |
---|
| 868 | /// </param> |
---|
| 869 | |
---|
| 870 | className.constructor == String ? |
---|
| 871 | this.classRuleSettings[className] = rules : |
---|
| 872 | $.extend(this.classRuleSettings, className); |
---|
| 873 | }, |
---|
| 874 | |
---|
| 875 | classRules: function(element) { |
---|
| 876 | var rules = {}; |
---|
| 877 | var classes = $(element).attr('class'); |
---|
| 878 | classes && $.each(classes.split(' '), function() { |
---|
| 879 | if (this in $.validator.classRuleSettings) { |
---|
| 880 | $.extend(rules, $.validator.classRuleSettings[this]); |
---|
| 881 | } |
---|
| 882 | }); |
---|
| 883 | return rules; |
---|
| 884 | }, |
---|
| 885 | |
---|
| 886 | attributeRules: function(element) { |
---|
| 887 | var rules = {}; |
---|
| 888 | var $element = $(element); |
---|
| 889 | |
---|
| 890 | for (var method in $.validator.methods) { |
---|
| 891 | var value = $element.attr(method); |
---|
| 892 | if (value) { |
---|
| 893 | rules[method] = value; |
---|
| 894 | } |
---|
| 895 | } |
---|
| 896 | |
---|
| 897 | // maxlength may be returned as -1, 2147483647 (IE) and 524288 (safari) for text inputs |
---|
| 898 | if (rules.maxlength && /-1|2147483647|524288/.test(rules.maxlength)) { |
---|
| 899 | delete rules.maxlength; |
---|
| 900 | } |
---|
| 901 | |
---|
| 902 | return rules; |
---|
| 903 | }, |
---|
| 904 | |
---|
| 905 | metadataRules: function(element) { |
---|
| 906 | if (!$.metadata) return {}; |
---|
| 907 | |
---|
| 908 | var meta = $.data(element.form, 'validator').settings.meta; |
---|
| 909 | return meta ? |
---|
| 910 | $(element).metadata()[meta] : |
---|
| 911 | $(element).metadata(); |
---|
| 912 | }, |
---|
| 913 | |
---|
| 914 | staticRules: function(element) { |
---|
| 915 | var rules = {}; |
---|
| 916 | var validator = $.data(element.form, 'validator'); |
---|
| 917 | if (validator.settings.rules) { |
---|
| 918 | rules = $.validator.normalizeRule(validator.settings.rules[element.name]) || {}; |
---|
| 919 | } |
---|
| 920 | return rules; |
---|
| 921 | }, |
---|
| 922 | |
---|
| 923 | normalizeRules: function(rules, element) { |
---|
| 924 | // handle dependency check |
---|
| 925 | $.each(rules, function(prop, val) { |
---|
| 926 | // ignore rule when param is explicitly false, eg. required:false |
---|
| 927 | if (val === false) { |
---|
| 928 | delete rules[prop]; |
---|
| 929 | return; |
---|
| 930 | } |
---|
| 931 | if (val.param || val.depends) { |
---|
| 932 | var keepRule = true; |
---|
| 933 | switch (typeof val.depends) { |
---|
| 934 | case "string": |
---|
| 935 | keepRule = !!$(val.depends, element.form).length; |
---|
| 936 | break; |
---|
| 937 | case "function": |
---|
| 938 | keepRule = val.depends.call(element, element); |
---|
| 939 | break; |
---|
| 940 | } |
---|
| 941 | if (keepRule) { |
---|
| 942 | rules[prop] = val.param !== undefined ? val.param : true; |
---|
| 943 | } else { |
---|
| 944 | delete rules[prop]; |
---|
| 945 | } |
---|
| 946 | } |
---|
| 947 | }); |
---|
| 948 | |
---|
| 949 | // evaluate parameters |
---|
| 950 | $.each(rules, function(rule, parameter) { |
---|
| 951 | rules[rule] = $.isFunction(parameter) ? parameter(element) : parameter; |
---|
| 952 | }); |
---|
| 953 | |
---|
| 954 | // clean number parameters |
---|
| 955 | $.each(['minlength', 'maxlength', 'min', 'max'], function() { |
---|
| 956 | if (rules[this]) { |
---|
| 957 | rules[this] = Number(rules[this]); |
---|
| 958 | } |
---|
| 959 | }); |
---|
| 960 | $.each(['rangelength', 'range'], function() { |
---|
| 961 | if (rules[this]) { |
---|
| 962 | rules[this] = [Number(rules[this][0]), Number(rules[this][1])]; |
---|
| 963 | } |
---|
| 964 | }); |
---|
| 965 | |
---|
| 966 | if ($.validator.autoCreateRanges) { |
---|
| 967 | // auto-create ranges |
---|
| 968 | if (rules.min && rules.max) { |
---|
| 969 | rules.range = [rules.min, rules.max]; |
---|
| 970 | delete rules.min; |
---|
| 971 | delete rules.max; |
---|
| 972 | } |
---|
| 973 | if (rules.minlength && rules.maxlength) { |
---|
| 974 | rules.rangelength = [rules.minlength, rules.maxlength]; |
---|
| 975 | delete rules.minlength; |
---|
| 976 | delete rules.maxlength; |
---|
| 977 | } |
---|
| 978 | } |
---|
| 979 | |
---|
| 980 | // To support custom messages in metadata ignore rule methods titled "messages" |
---|
| 981 | if (rules.messages) { |
---|
| 982 | delete rules.messages; |
---|
| 983 | } |
---|
| 984 | |
---|
| 985 | return rules; |
---|
| 986 | }, |
---|
| 987 | |
---|
| 988 | // Converts a simple string to a {string: true} rule, e.g., "required" to {required:true} |
---|
| 989 | normalizeRule: function(data) { |
---|
| 990 | if( typeof data == "string" ) { |
---|
| 991 | var transformed = {}; |
---|
| 992 | $.each(data.split(/\s/), function() { |
---|
| 993 | transformed[this] = true; |
---|
| 994 | }); |
---|
| 995 | data = transformed; |
---|
| 996 | } |
---|
| 997 | return data; |
---|
| 998 | }, |
---|
| 999 | |
---|
| 1000 | // http://docs.jquery.com/Plugins/Validation/Validator/addMethod |
---|
| 1001 | addMethod: function(name, method, message) { |
---|
| 1002 | /// <summary> |
---|
| 1003 | /// Add a custom validation method. It must consist of a name (must be a legal javascript |
---|
| 1004 | /// identifier), a javascript based function and a default string message. |
---|
| 1005 | /// </summary> |
---|
| 1006 | /// <param name="name" type="String"> |
---|
| 1007 | /// The name of the method, used to identify and referencing it, must be a valid javascript |
---|
| 1008 | /// identifier |
---|
| 1009 | /// </param> |
---|
| 1010 | /// <param name="method" type="Function"> |
---|
| 1011 | /// The actual method implementation, returning true if an element is valid |
---|
| 1012 | /// </param> |
---|
| 1013 | /// <param name="message" type="String" optional="true"> |
---|
| 1014 | /// (Optional) The default message to display for this method. Can be a function created by |
---|
| 1015 | /// jQuery.validator.format(value). When undefined, an already existing message is used |
---|
| 1016 | /// (handy for localization), otherwise the field-specific messages have to be defined. |
---|
| 1017 | /// </param> |
---|
| 1018 | |
---|
| 1019 | $.validator.methods[name] = method; |
---|
| 1020 | $.validator.messages[name] = message != undefined ? message : $.validator.messages[name]; |
---|
| 1021 | if (method.length < 3) { |
---|
| 1022 | $.validator.addClassRules(name, $.validator.normalizeRule(name)); |
---|
| 1023 | } |
---|
| 1024 | }, |
---|
| 1025 | |
---|
| 1026 | methods: { |
---|
| 1027 | |
---|
| 1028 | // http://docs.jquery.com/Plugins/Validation/Methods/required |
---|
| 1029 | required: function(value, element, param) { |
---|
| 1030 | // check if dependency is met |
---|
| 1031 | if ( !this.depend(param, element) ) |
---|
| 1032 | return "dependency-mismatch"; |
---|
| 1033 | switch( element.nodeName.toLowerCase() ) { |
---|
| 1034 | case 'select': |
---|
| 1035 | // could be an array for select-multiple or a string, both are fine this way |
---|
| 1036 | var val = $(element).val(); |
---|
| 1037 | return val && val.length > 0; |
---|
| 1038 | case 'input': |
---|
| 1039 | if ( this.checkable(element) ) |
---|
| 1040 | return this.getLength(value, element) > 0; |
---|
| 1041 | default: |
---|
| 1042 | return $.trim(value).length > 0; |
---|
| 1043 | } |
---|
| 1044 | }, |
---|
| 1045 | |
---|
| 1046 | // http://docs.jquery.com/Plugins/Validation/Methods/remote |
---|
| 1047 | remote: function(value, element, param) { |
---|
| 1048 | if ( this.optional(element) ) |
---|
| 1049 | return "dependency-mismatch"; |
---|
| 1050 | |
---|
| 1051 | var previous = this.previousValue(element); |
---|
| 1052 | if (!this.settings.messages[element.name] ) |
---|
| 1053 | this.settings.messages[element.name] = {}; |
---|
| 1054 | previous.originalMessage = this.settings.messages[element.name].remote; |
---|
| 1055 | this.settings.messages[element.name].remote = previous.message; |
---|
| 1056 | |
---|
| 1057 | param = typeof param == "string" && {url:param} || param; |
---|
| 1058 | |
---|
| 1059 | if ( this.pending[element.name] ) { |
---|
| 1060 | return "pending"; |
---|
| 1061 | } |
---|
| 1062 | if ( previous.old === value ) { |
---|
| 1063 | return previous.valid; |
---|
| 1064 | } |
---|
| 1065 | |
---|
| 1066 | previous.old = value; |
---|
| 1067 | var validator = this; |
---|
| 1068 | this.startRequest(element); |
---|
| 1069 | var data = {}; |
---|
| 1070 | data[element.name] = value; |
---|
| 1071 | $.ajax($.extend(true, { |
---|
| 1072 | url: param, |
---|
| 1073 | mode: "abort", |
---|
| 1074 | port: "validate" + element.name, |
---|
| 1075 | dataType: "json", |
---|
| 1076 | data: data, |
---|
| 1077 | success: function(response) { |
---|
| 1078 | validator.settings.messages[element.name].remote = previous.originalMessage; |
---|
| 1079 | var valid = response === true; |
---|
| 1080 | if ( valid ) { |
---|
| 1081 | var submitted = validator.formSubmitted; |
---|
| 1082 | validator.prepareElement(element); |
---|
| 1083 | validator.formSubmitted = submitted; |
---|
| 1084 | validator.successList.push(element); |
---|
| 1085 | validator.showErrors(); |
---|
| 1086 | } else { |
---|
| 1087 | var errors = {}; |
---|
| 1088 | var message = response || validator.defaultMessage(element, "remote"); |
---|
| 1089 | errors[element.name] = previous.message = $.isFunction(message) ? message(value) : message; |
---|
| 1090 | validator.showErrors(errors); |
---|
| 1091 | } |
---|
| 1092 | previous.valid = valid; |
---|
| 1093 | validator.stopRequest(element, valid); |
---|
| 1094 | } |
---|
| 1095 | }, param)); |
---|
| 1096 | return "pending"; |
---|
| 1097 | }, |
---|
| 1098 | |
---|
| 1099 | // http://docs.jquery.com/Plugins/Validation/Methods/minlength |
---|
| 1100 | minlength: function(value, element, param) { |
---|
| 1101 | return this.optional(element) || this.getLength($.trim(value), element) >= param; |
---|
| 1102 | }, |
---|
| 1103 | |
---|
| 1104 | // http://docs.jquery.com/Plugins/Validation/Methods/maxlength |
---|
| 1105 | maxlength: function(value, element, param) { |
---|
| 1106 | return this.optional(element) || this.getLength($.trim(value), element) <= param; |
---|
| 1107 | }, |
---|
| 1108 | |
---|
| 1109 | // http://docs.jquery.com/Plugins/Validation/Methods/rangelength |
---|
| 1110 | rangelength: function(value, element, param) { |
---|
| 1111 | var length = this.getLength($.trim(value), element); |
---|
| 1112 | return this.optional(element) || ( length >= param[0] && length <= param[1] ); |
---|
| 1113 | }, |
---|
| 1114 | |
---|
| 1115 | // http://docs.jquery.com/Plugins/Validation/Methods/min |
---|
| 1116 | min: function( value, element, param ) { |
---|
| 1117 | return this.optional(element) || value >= param; |
---|
| 1118 | }, |
---|
| 1119 | |
---|
| 1120 | // http://docs.jquery.com/Plugins/Validation/Methods/max |
---|
| 1121 | max: function( value, element, param ) { |
---|
| 1122 | return this.optional(element) || value <= param; |
---|
| 1123 | }, |
---|
| 1124 | |
---|
| 1125 | // http://docs.jquery.com/Plugins/Validation/Methods/range |
---|
| 1126 | range: function( value, element, param ) { |
---|
| 1127 | return this.optional(element) || ( value >= param[0] && value <= param[1] ); |
---|
| 1128 | }, |
---|
| 1129 | |
---|
| 1130 | // http://docs.jquery.com/Plugins/Validation/Methods/email |
---|
| 1131 | email: function(value, element) { |
---|
| 1132 | // contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/ |
---|
| 1133 | return this.optional(element) || /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([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])))\.?$/i.test(value); |
---|
| 1134 | }, |
---|
| 1135 | |
---|
| 1136 | // http://docs.jquery.com/Plugins/Validation/Methods/url |
---|
| 1137 | url: function(value, element) { |
---|
| 1138 | // contributed by Scott Gonzalez: http://projects.scottsplayground.com/iri/ |
---|
| 1139 | return this.optional(element) || /^(https?|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); |
---|
| 1140 | }, |
---|
| 1141 | |
---|
| 1142 | // http://docs.jquery.com/Plugins/Validation/Methods/date |
---|
| 1143 | date: function(value, element) { |
---|
| 1144 | return this.optional(element) || !/Invalid|NaN/.test(new Date(value)); |
---|
| 1145 | }, |
---|
| 1146 | |
---|
| 1147 | // http://docs.jquery.com/Plugins/Validation/Methods/dateISO |
---|
| 1148 | dateISO: function(value, element) { |
---|
| 1149 | return this.optional(element) || /^\d{4}[\/-]\d{1,2}[\/-]\d{1,2}$/.test(value); |
---|
| 1150 | }, |
---|
| 1151 | |
---|
| 1152 | // http://docs.jquery.com/Plugins/Validation/Methods/number |
---|
| 1153 | number: function(value, element) { |
---|
| 1154 | return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(value); |
---|
| 1155 | }, |
---|
| 1156 | |
---|
| 1157 | // http://docs.jquery.com/Plugins/Validation/Methods/digits |
---|
| 1158 | digits: function(value, element) { |
---|
| 1159 | return this.optional(element) || /^\d+$/.test(value); |
---|
| 1160 | }, |
---|
| 1161 | |
---|
| 1162 | // http://docs.jquery.com/Plugins/Validation/Methods/creditcard |
---|
| 1163 | // based on http://en.wikipedia.org/wiki/Luhn |
---|
| 1164 | creditcard: function(value, element) { |
---|
| 1165 | if ( this.optional(element) ) |
---|
| 1166 | return "dependency-mismatch"; |
---|
| 1167 | // accept only digits and dashes |
---|
| 1168 | if (/[^0-9-]+/.test(value)) |
---|
| 1169 | return false; |
---|
| 1170 | var nCheck = 0, |
---|
| 1171 | nDigit = 0, |
---|
| 1172 | bEven = false; |
---|
| 1173 | |
---|
| 1174 | value = value.replace(/\D/g, ""); |
---|
| 1175 | |
---|
| 1176 | for (var n = value.length - 1; n >= 0; n--) { |
---|
| 1177 | var cDigit = value.charAt(n); |
---|
| 1178 | var nDigit = parseInt(cDigit, 10); |
---|
| 1179 | if (bEven) { |
---|
| 1180 | if ((nDigit *= 2) > 9) |
---|
| 1181 | nDigit -= 9; |
---|
| 1182 | } |
---|
| 1183 | nCheck += nDigit; |
---|
| 1184 | bEven = !bEven; |
---|
| 1185 | } |
---|
| 1186 | |
---|
| 1187 | return (nCheck % 10) == 0; |
---|
| 1188 | }, |
---|
| 1189 | |
---|
| 1190 | // http://docs.jquery.com/Plugins/Validation/Methods/accept |
---|
| 1191 | accept: function(value, element, param) { |
---|
| 1192 | param = typeof param == "string" ? param.replace(/,/g, '|') : "png|jpe?g|gif"; |
---|
| 1193 | return this.optional(element) || value.match(new RegExp(".(" + param + ")$", "i")); |
---|
| 1194 | }, |
---|
| 1195 | |
---|
| 1196 | // http://docs.jquery.com/Plugins/Validation/Methods/equalTo |
---|
| 1197 | equalTo: function(value, element, param) { |
---|
| 1198 | // bind to the blur event of the target in order to revalidate whenever the target field is updated |
---|
| 1199 | // TODO find a way to bind the event just once, avoiding the unbind-rebind overhead |
---|
| 1200 | var target = $(param).unbind(".validate-equalTo").bind("blur.validate-equalTo", function() { |
---|
| 1201 | $(element).valid(); |
---|
| 1202 | }); |
---|
| 1203 | return value == target.val(); |
---|
| 1204 | } |
---|
| 1205 | |
---|
| 1206 | } |
---|
| 1207 | |
---|
| 1208 | }); |
---|
| 1209 | |
---|
| 1210 | // deprecated, use $.validator.format instead |
---|
| 1211 | $.format = $.validator.format; |
---|
| 1212 | |
---|
| 1213 | })(jQuery); |
---|
| 1214 | |
---|
| 1215 | // ajax mode: abort |
---|
| 1216 | // usage: $.ajax({ mode: "abort"[, port: "uniqueport"]}); |
---|
| 1217 | // if mode:"abort" is used, the previous request on that port (port can be undefined) is aborted via XMLHttpRequest.abort() |
---|
| 1218 | ;(function($) { |
---|
| 1219 | var pendingRequests = {}; |
---|
| 1220 | // Use a prefilter if available (1.5+) |
---|
| 1221 | if ( $.ajaxPrefilter ) { |
---|
| 1222 | $.ajaxPrefilter(function(settings, _, xhr) { |
---|
| 1223 | var port = settings.port; |
---|
| 1224 | if (settings.mode == "abort") { |
---|
| 1225 | if ( pendingRequests[port] ) { |
---|
| 1226 | pendingRequests[port].abort(); |
---|
| 1227 | } pendingRequests[port] = xhr; |
---|
| 1228 | } |
---|
| 1229 | }); |
---|
| 1230 | } else { |
---|
| 1231 | // Proxy ajax |
---|
| 1232 | var ajax = $.ajax; |
---|
| 1233 | $.ajax = function(settings) { |
---|
| 1234 | var mode = ( "mode" in settings ? settings : $.ajaxSettings ).mode, |
---|
| 1235 | port = ( "port" in settings ? settings : $.ajaxSettings ).port; |
---|
| 1236 | if (mode == "abort") { |
---|
| 1237 | if ( pendingRequests[port] ) { |
---|
| 1238 | pendingRequests[port].abort(); |
---|
| 1239 | } |
---|
| 1240 | |
---|
| 1241 | return (pendingRequests[port] = ajax.apply(this, arguments)); |
---|
| 1242 | } |
---|
| 1243 | return ajax.apply(this, arguments); |
---|
| 1244 | }; |
---|
| 1245 | } |
---|
| 1246 | })(jQuery); |
---|
| 1247 | |
---|
| 1248 | // provides cross-browser focusin and focusout events |
---|
| 1249 | // IE has native support, in other browsers, use event caputuring (neither bubbles) |
---|
| 1250 | |
---|
| 1251 | // provides delegate(type: String, delegate: Selector, handler: Callback) plugin for easier event delegation |
---|
| 1252 | // handler is only called when $(event.target).is(delegate), in the scope of the jquery-object for event.target |
---|
| 1253 | ;(function($) { |
---|
| 1254 | // only implement if not provided by jQuery core (since 1.4) |
---|
| 1255 | // TODO verify if jQuery 1.4's implementation is compatible with older jQuery special-event APIs |
---|
| 1256 | if (!jQuery.event.special.focusin && !jQuery.event.special.focusout && document.addEventListener) { |
---|
| 1257 | $.each({ |
---|
| 1258 | focus: 'focusin', |
---|
| 1259 | blur: 'focusout' |
---|
| 1260 | }, function( original, fix ){ |
---|
| 1261 | $.event.special[fix] = { |
---|
| 1262 | setup:function() { |
---|
| 1263 | this.addEventListener( original, handler, true ); |
---|
| 1264 | }, |
---|
| 1265 | teardown:function() { |
---|
| 1266 | this.removeEventListener( original, handler, true ); |
---|
| 1267 | }, |
---|
| 1268 | handler: function(e) { |
---|
| 1269 | arguments[0] = $.event.fix(e); |
---|
| 1270 | arguments[0].type = fix; |
---|
| 1271 | return $.event.handle.apply(this, arguments); |
---|
| 1272 | } |
---|
| 1273 | }; |
---|
| 1274 | function handler(e) { |
---|
| 1275 | e = $.event.fix(e); |
---|
| 1276 | e.type = fix; |
---|
| 1277 | return $.event.handle.call(this, e); |
---|
| 1278 | } |
---|
| 1279 | }); |
---|
| 1280 | }; |
---|
| 1281 | $.extend($.fn, { |
---|
| 1282 | validateDelegate: function(delegate, type, handler) { |
---|
| 1283 | return this.bind(type, function(event) { |
---|
| 1284 | var target = $(event.target); |
---|
| 1285 | if (target.is(delegate)) { |
---|
| 1286 | return handler.apply(target, arguments); |
---|
| 1287 | } |
---|
| 1288 | }); |
---|
| 1289 | } |
---|
| 1290 | }); |
---|
| 1291 | })(jQuery); |
---|