github.com/apremalal/vamps-core@v1.0.1-0.20161221121535-d430b56ec174/server/webapps/app/base/plugins/jquery-validation/js/additional-methods.js (about) 1 /*! 2 * jQuery Validation Plugin v1.13.0 3 * 4 * http://jqueryvalidation.org/ 5 * 6 * Copyright (c) 2014 Jörn Zaefferer 7 * Released under the MIT license 8 */ 9 (function( factory ) { 10 if ( typeof define === "function" && define.amd ) { 11 define( ["jquery", "./jquery.validate.js"], factory ); 12 } else { 13 factory( jQuery ); 14 } 15 }(function( $ ) { 16 17 (function() { 18 19 function stripHtml(value) { 20 // remove html tags and space chars 21 return value.replace(/<.[^<>]*?>/g, " ").replace(/ | /gi, " ") 22 // remove punctuation 23 .replace(/[.(),;:!?%#$'\"_+=\/\-“”’]*/g, ""); 24 } 25 26 $.validator.addMethod("maxWords", function(value, element, params) { 27 return this.optional(element) || stripHtml(value).match(/\b\w+\b/g).length <= params; 28 }, $.validator.format("Please enter {0} words or less.")); 29 30 $.validator.addMethod("minWords", function(value, element, params) { 31 return this.optional(element) || stripHtml(value).match(/\b\w+\b/g).length >= params; 32 }, $.validator.format("Please enter at least {0} words.")); 33 34 $.validator.addMethod("rangeWords", function(value, element, params) { 35 var valueStripped = stripHtml(value), 36 regex = /\b\w+\b/g; 37 return this.optional(element) || valueStripped.match(regex).length >= params[0] && valueStripped.match(regex).length <= params[1]; 38 }, $.validator.format("Please enter between {0} and {1} words.")); 39 40 }()); 41 42 // Accept a value from a file input based on a required mimetype 43 $.validator.addMethod("accept", function(value, element, param) { 44 // Split mime on commas in case we have multiple types we can accept 45 var typeParam = typeof param === "string" ? param.replace(/\s/g, "").replace(/,/g, "|") : "image/*", 46 optionalValue = this.optional(element), 47 i, file; 48 49 // Element is optional 50 if (optionalValue) { 51 return optionalValue; 52 } 53 54 if ($(element).attr("type") === "file") { 55 // If we are using a wildcard, make it regex friendly 56 typeParam = typeParam.replace(/\*/g, ".*"); 57 58 // Check if the element has a FileList before checking each file 59 if (element.files && element.files.length) { 60 for (i = 0; i < element.files.length; i++) { 61 file = element.files[i]; 62 63 // Grab the mimetype from the loaded file, verify it matches 64 if (!file.type.match(new RegExp( ".?(" + typeParam + ")$", "i"))) { 65 return false; 66 } 67 } 68 } 69 } 70 71 // Either return true because we've validated each file, or because the 72 // browser does not support element.files and the FileList feature 73 return true; 74 }, $.validator.format("Please enter a value with a valid mimetype.")); 75 76 $.validator.addMethod("alphanumeric", function(value, element) { 77 return this.optional(element) || /^\w+$/i.test(value); 78 }, "Letters, numbers, and underscores only please"); 79 80 /* 81 * Dutch bank account numbers (not 'giro' numbers) have 9 digits 82 * and pass the '11 check'. 83 * We accept the notation with spaces, as that is common. 84 * acceptable: 123456789 or 12 34 56 789 85 */ 86 $.validator.addMethod("bankaccountNL", function(value, element) { 87 if (this.optional(element)) { 88 return true; 89 } 90 if (!(/^[0-9]{9}|([0-9]{2} ){3}[0-9]{3}$/.test(value))) { 91 return false; 92 } 93 // now '11 check' 94 var account = value.replace(/ /g, ""), // remove spaces 95 sum = 0, 96 len = account.length, 97 pos, factor, digit; 98 for ( pos = 0; pos < len; pos++ ) { 99 factor = len - pos; 100 digit = account.substring(pos, pos + 1); 101 sum = sum + factor * digit; 102 } 103 return sum % 11 === 0; 104 }, "Please specify a valid bank account number"); 105 106 $.validator.addMethod("bankorgiroaccountNL", function(value, element) { 107 return this.optional(element) || 108 ($.validator.methods.bankaccountNL.call(this, value, element)) || 109 ($.validator.methods.giroaccountNL.call(this, value, element)); 110 }, "Please specify a valid bank or giro account number"); 111 112 /** 113 * BIC is the business identifier code (ISO 9362). This BIC check is not a guarantee for authenticity. 114 * 115 * BIC pattern: BBBBCCLLbbb (8 or 11 characters long; bbb is optional) 116 * 117 * BIC definition in detail: 118 * - First 4 characters - bank code (only letters) 119 * - Next 2 characters - ISO 3166-1 alpha-2 country code (only letters) 120 * - Next 2 characters - location code (letters and digits) 121 * a. shall not start with '0' or '1' 122 * b. second character must be a letter ('O' is not allowed) or one of the following digits ('0' for test (therefore not allowed), '1' for passive participant and '2' for active participant) 123 * - Last 3 characters - branch code, optional (shall not start with 'X' except in case of 'XXX' for primary office) (letters and digits) 124 */ 125 $.validator.addMethod("bic", function(value, element) { 126 return this.optional( element ) || /^([A-Z]{6}[A-Z2-9][A-NP-Z1-2])(X{3}|[A-WY-Z0-9][A-Z0-9]{2})?$/.test( value ); 127 }, "Please specify a valid BIC code"); 128 129 /* 130 * Código de identificación fiscal ( CIF ) is the tax identification code for Spanish legal entities 131 * Further rules can be found in Spanish on http://es.wikipedia.org/wiki/C%C3%B3digo_de_identificaci%C3%B3n_fiscal 132 */ 133 $.validator.addMethod( "cifES", function( value ) { 134 "use strict"; 135 136 var num = [], 137 controlDigit, sum, i, count, tmp, secondDigit; 138 139 value = value.toUpperCase(); 140 141 // Quick format test 142 if ( !value.match( "((^[A-Z]{1}[0-9]{7}[A-Z0-9]{1}$|^[T]{1}[A-Z0-9]{8}$)|^[0-9]{8}[A-Z]{1}$)" ) ) { 143 return false; 144 } 145 146 for ( i = 0; i < 9; i++ ) { 147 num[ i ] = parseInt( value.charAt( i ), 10 ); 148 } 149 150 // Algorithm for checking CIF codes 151 sum = num[ 2 ] + num[ 4 ] + num[ 6 ]; 152 for ( count = 1; count < 8; count += 2 ) { 153 tmp = ( 2 * num[ count ] ).toString(); 154 secondDigit = tmp.charAt( 1 ); 155 156 sum += parseInt( tmp.charAt( 0 ), 10 ) + ( secondDigit === "" ? 0 : parseInt( secondDigit, 10 ) ); 157 } 158 159 /* The first (position 1) is a letter following the following criteria: 160 * A. Corporations 161 * B. LLCs 162 * C. General partnerships 163 * D. Companies limited partnerships 164 * E. Communities of goods 165 * F. Cooperative Societies 166 * G. Associations 167 * H. Communities of homeowners in horizontal property regime 168 * J. Civil Societies 169 * K. Old format 170 * L. Old format 171 * M. Old format 172 * N. Nonresident entities 173 * P. Local authorities 174 * Q. Autonomous bodies, state or not, and the like, and congregations and religious institutions 175 * R. Congregations and religious institutions (since 2008 ORDER EHA/451/2008) 176 * S. Organs of State Administration and regions 177 * V. Agrarian Transformation 178 * W. Permanent establishments of non-resident in Spain 179 */ 180 if ( /^[ABCDEFGHJNPQRSUVW]{1}/.test( value ) ) { 181 sum += ""; 182 controlDigit = 10 - parseInt( sum.charAt( sum.length - 1 ), 10 ); 183 value += controlDigit; 184 return ( num[ 8 ].toString() === String.fromCharCode( 64 + controlDigit ) || num[ 8 ].toString() === value.charAt( value.length - 1 ) ); 185 } 186 187 return false; 188 189 }, "Please specify a valid CIF number." ); 190 191 /* NOTICE: Modified version of Castle.Components.Validator.CreditCardValidator 192 * Redistributed under the the Apache License 2.0 at http://www.apache.org/licenses/LICENSE-2.0 193 * Valid Types: mastercard, visa, amex, dinersclub, enroute, discover, jcb, unknown, all (overrides all other settings) 194 */ 195 $.validator.addMethod("creditcardtypes", function(value, element, param) { 196 if (/[^0-9\-]+/.test(value)) { 197 return false; 198 } 199 200 value = value.replace(/\D/g, ""); 201 202 var validTypes = 0x0000; 203 204 if (param.mastercard) { 205 validTypes |= 0x0001; 206 } 207 if (param.visa) { 208 validTypes |= 0x0002; 209 } 210 if (param.amex) { 211 validTypes |= 0x0004; 212 } 213 if (param.dinersclub) { 214 validTypes |= 0x0008; 215 } 216 if (param.enroute) { 217 validTypes |= 0x0010; 218 } 219 if (param.discover) { 220 validTypes |= 0x0020; 221 } 222 if (param.jcb) { 223 validTypes |= 0x0040; 224 } 225 if (param.unknown) { 226 validTypes |= 0x0080; 227 } 228 if (param.all) { 229 validTypes = 0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080; 230 } 231 if (validTypes & 0x0001 && /^(5[12345])/.test(value)) { //mastercard 232 return value.length === 16; 233 } 234 if (validTypes & 0x0002 && /^(4)/.test(value)) { //visa 235 return value.length === 16; 236 } 237 if (validTypes & 0x0004 && /^(3[47])/.test(value)) { //amex 238 return value.length === 15; 239 } 240 if (validTypes & 0x0008 && /^(3(0[012345]|[68]))/.test(value)) { //dinersclub 241 return value.length === 14; 242 } 243 if (validTypes & 0x0010 && /^(2(014|149))/.test(value)) { //enroute 244 return value.length === 15; 245 } 246 if (validTypes & 0x0020 && /^(6011)/.test(value)) { //discover 247 return value.length === 16; 248 } 249 if (validTypes & 0x0040 && /^(3)/.test(value)) { //jcb 250 return value.length === 16; 251 } 252 if (validTypes & 0x0040 && /^(2131|1800)/.test(value)) { //jcb 253 return value.length === 15; 254 } 255 if (validTypes & 0x0080) { //unknown 256 return true; 257 } 258 return false; 259 }, "Please enter a valid credit card number."); 260 261 /** 262 * Validates currencies with any given symbols by @jameslouiz 263 * Symbols can be optional or required. Symbols required by default 264 * 265 * Usage examples: 266 * currency: ["£", false] - Use false for soft currency validation 267 * currency: ["$", false] 268 * currency: ["RM", false] - also works with text based symbols such as "RM" - Malaysia Ringgit etc 269 * 270 * <input class="currencyInput" name="currencyInput"> 271 * 272 * Soft symbol checking 273 * currencyInput: { 274 * currency: ["$", false] 275 * } 276 * 277 * Strict symbol checking (default) 278 * currencyInput: { 279 * currency: "$" 280 * //OR 281 * currency: ["$", true] 282 * } 283 * 284 * Multiple Symbols 285 * currencyInput: { 286 * currency: "$,£,¢" 287 * } 288 */ 289 $.validator.addMethod("currency", function(value, element, param) { 290 var isParamString = typeof param === "string", 291 symbol = isParamString ? param : param[0], 292 soft = isParamString ? true : param[1], 293 regex; 294 295 symbol = symbol.replace(/,/g, ""); 296 symbol = soft ? symbol + "]" : symbol + "]?"; 297 regex = "^[" + symbol + "([1-9]{1}[0-9]{0,2}(\\,[0-9]{3})*(\\.[0-9]{0,2})?|[1-9]{1}[0-9]{0,}(\\.[0-9]{0,2})?|0(\\.[0-9]{0,2})?|(\\.[0-9]{1,2})?)$"; 298 regex = new RegExp(regex); 299 return this.optional(element) || regex.test(value); 300 301 }, "Please specify a valid currency"); 302 303 $.validator.addMethod("dateFA", function(value, element) { 304 return this.optional(element) || /^[1-4]\d{3}\/((0?[1-6]\/((3[0-1])|([1-2][0-9])|(0?[1-9])))|((1[0-2]|(0?[7-9]))\/(30|([1-2][0-9])|(0?[1-9]))))$/.test(value); 305 }, "Please enter a correct date"); 306 307 /** 308 * Return true, if the value is a valid date, also making this formal check dd/mm/yyyy. 309 * 310 * @example $.validator.methods.date("01/01/1900") 311 * @result true 312 * 313 * @example $.validator.methods.date("01/13/1990") 314 * @result false 315 * 316 * @example $.validator.methods.date("01.01.1900") 317 * @result false 318 * 319 * @example <input name="pippo" class="{dateITA:true}" /> 320 * @desc Declares an optional input element whose value must be a valid date. 321 * 322 * @name $.validator.methods.dateITA 323 * @type Boolean 324 * @cat Plugins/Validate/Methods 325 */ 326 $.validator.addMethod("dateITA", function(value, element) { 327 var check = false, 328 re = /^\d{1,2}\/\d{1,2}\/\d{4}$/, 329 adata, gg, mm, aaaa, xdata; 330 if ( re.test(value)) { 331 adata = value.split("/"); 332 gg = parseInt(adata[0], 10); 333 mm = parseInt(adata[1], 10); 334 aaaa = parseInt(adata[2], 10); 335 xdata = new Date(aaaa, mm - 1, gg, 12, 0, 0, 0); 336 if ( ( xdata.getUTCFullYear() === aaaa ) && ( xdata.getUTCMonth () === mm - 1 ) && ( xdata.getUTCDate() === gg ) ) { 337 check = true; 338 } else { 339 check = false; 340 } 341 } else { 342 check = false; 343 } 344 return this.optional(element) || check; 345 }, "Please enter a correct date"); 346 347 $.validator.addMethod("dateNL", function(value, element) { 348 return this.optional(element) || /^(0?[1-9]|[12]\d|3[01])[\.\/\-](0?[1-9]|1[012])[\.\/\-]([12]\d)?(\d\d)$/.test(value); 349 }, "Please enter a correct date"); 350 351 // Older "accept" file extension method. Old docs: http://docs.jquery.com/Plugins/Validation/Methods/accept 352 $.validator.addMethod("extension", function(value, element, param) { 353 param = typeof param === "string" ? param.replace(/,/g, "|") : "png|jpe?g|gif"; 354 return this.optional(element) || value.match(new RegExp(".(" + param + ")$", "i")); 355 }, $.validator.format("Please enter a value with a valid extension.")); 356 357 /** 358 * Dutch giro account numbers (not bank numbers) have max 7 digits 359 */ 360 $.validator.addMethod("giroaccountNL", function(value, element) { 361 return this.optional(element) || /^[0-9]{1,7}$/.test(value); 362 }, "Please specify a valid giro account number"); 363 364 /** 365 * IBAN is the international bank account number. 366 * It has a country - specific format, that is checked here too 367 */ 368 $.validator.addMethod("iban", function(value, element) { 369 // some quick simple tests to prevent needless work 370 if (this.optional(element)) { 371 return true; 372 } 373 374 // remove spaces and to upper case 375 var iban = value.replace(/ /g, "").toUpperCase(), 376 ibancheckdigits = "", 377 leadingZeroes = true, 378 cRest = "", 379 cOperator = "", 380 countrycode, ibancheck, charAt, cChar, bbanpattern, bbancountrypatterns, ibanregexp, i, p; 381 382 if (!(/^([a-zA-Z0-9]{4} ){2,8}[a-zA-Z0-9]{1,4}|[a-zA-Z0-9]{12,34}$/.test(iban))) { 383 return false; 384 } 385 386 // check the country code and find the country specific format 387 countrycode = iban.substring(0, 2); 388 bbancountrypatterns = { 389 "AL": "\\d{8}[\\dA-Z]{16}", 390 "AD": "\\d{8}[\\dA-Z]{12}", 391 "AT": "\\d{16}", 392 "AZ": "[\\dA-Z]{4}\\d{20}", 393 "BE": "\\d{12}", 394 "BH": "[A-Z]{4}[\\dA-Z]{14}", 395 "BA": "\\d{16}", 396 "BR": "\\d{23}[A-Z][\\dA-Z]", 397 "BG": "[A-Z]{4}\\d{6}[\\dA-Z]{8}", 398 "CR": "\\d{17}", 399 "HR": "\\d{17}", 400 "CY": "\\d{8}[\\dA-Z]{16}", 401 "CZ": "\\d{20}", 402 "DK": "\\d{14}", 403 "DO": "[A-Z]{4}\\d{20}", 404 "EE": "\\d{16}", 405 "FO": "\\d{14}", 406 "FI": "\\d{14}", 407 "FR": "\\d{10}[\\dA-Z]{11}\\d{2}", 408 "GE": "[\\dA-Z]{2}\\d{16}", 409 "DE": "\\d{18}", 410 "GI": "[A-Z]{4}[\\dA-Z]{15}", 411 "GR": "\\d{7}[\\dA-Z]{16}", 412 "GL": "\\d{14}", 413 "GT": "[\\dA-Z]{4}[\\dA-Z]{20}", 414 "HU": "\\d{24}", 415 "IS": "\\d{22}", 416 "IE": "[\\dA-Z]{4}\\d{14}", 417 "IL": "\\d{19}", 418 "IT": "[A-Z]\\d{10}[\\dA-Z]{12}", 419 "KZ": "\\d{3}[\\dA-Z]{13}", 420 "KW": "[A-Z]{4}[\\dA-Z]{22}", 421 "LV": "[A-Z]{4}[\\dA-Z]{13}", 422 "LB": "\\d{4}[\\dA-Z]{20}", 423 "LI": "\\d{5}[\\dA-Z]{12}", 424 "LT": "\\d{16}", 425 "LU": "\\d{3}[\\dA-Z]{13}", 426 "MK": "\\d{3}[\\dA-Z]{10}\\d{2}", 427 "MT": "[A-Z]{4}\\d{5}[\\dA-Z]{18}", 428 "MR": "\\d{23}", 429 "MU": "[A-Z]{4}\\d{19}[A-Z]{3}", 430 "MC": "\\d{10}[\\dA-Z]{11}\\d{2}", 431 "MD": "[\\dA-Z]{2}\\d{18}", 432 "ME": "\\d{18}", 433 "NL": "[A-Z]{4}\\d{10}", 434 "NO": "\\d{11}", 435 "PK": "[\\dA-Z]{4}\\d{16}", 436 "PS": "[\\dA-Z]{4}\\d{21}", 437 "PL": "\\d{24}", 438 "PT": "\\d{21}", 439 "RO": "[A-Z]{4}[\\dA-Z]{16}", 440 "SM": "[A-Z]\\d{10}[\\dA-Z]{12}", 441 "SA": "\\d{2}[\\dA-Z]{18}", 442 "RS": "\\d{18}", 443 "SK": "\\d{20}", 444 "SI": "\\d{15}", 445 "ES": "\\d{20}", 446 "SE": "\\d{20}", 447 "CH": "\\d{5}[\\dA-Z]{12}", 448 "TN": "\\d{20}", 449 "TR": "\\d{5}[\\dA-Z]{17}", 450 "AE": "\\d{3}\\d{16}", 451 "GB": "[A-Z]{4}\\d{14}", 452 "VG": "[\\dA-Z]{4}\\d{16}" 453 }; 454 455 bbanpattern = bbancountrypatterns[countrycode]; 456 // As new countries will start using IBAN in the 457 // future, we only check if the countrycode is known. 458 // This prevents false negatives, while almost all 459 // false positives introduced by this, will be caught 460 // by the checksum validation below anyway. 461 // Strict checking should return FALSE for unknown 462 // countries. 463 if (typeof bbanpattern !== "undefined") { 464 ibanregexp = new RegExp("^[A-Z]{2}\\d{2}" + bbanpattern + "$", ""); 465 if (!(ibanregexp.test(iban))) { 466 return false; // invalid country specific format 467 } 468 } 469 470 // now check the checksum, first convert to digits 471 ibancheck = iban.substring(4, iban.length) + iban.substring(0, 4); 472 for (i = 0; i < ibancheck.length; i++) { 473 charAt = ibancheck.charAt(i); 474 if (charAt !== "0") { 475 leadingZeroes = false; 476 } 477 if (!leadingZeroes) { 478 ibancheckdigits += "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".indexOf(charAt); 479 } 480 } 481 482 // calculate the result of: ibancheckdigits % 97 483 for (p = 0; p < ibancheckdigits.length; p++) { 484 cChar = ibancheckdigits.charAt(p); 485 cOperator = "" + cRest + "" + cChar; 486 cRest = cOperator % 97; 487 } 488 return cRest === 1; 489 }, "Please specify a valid IBAN"); 490 491 $.validator.addMethod("integer", function(value, element) { 492 return this.optional(element) || /^-?\d+$/.test(value); 493 }, "A positive or negative non-decimal number please"); 494 495 $.validator.addMethod("ipv4", function(value, element) { 496 return this.optional(element) || /^(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)$/i.test(value); 497 }, "Please enter a valid IP v4 address."); 498 499 $.validator.addMethod("ipv6", function(value, element) { 500 return this.optional(element) || /^((([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){0,5}:((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(::([0-9A-Fa-f]{1,4}:){0,5}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|(::([0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:))$/i.test(value); 501 }, "Please enter a valid IP v6 address."); 502 503 $.validator.addMethod("lettersonly", function(value, element) { 504 return this.optional(element) || /^[a-z]+$/i.test(value); 505 }, "Letters only please"); 506 507 $.validator.addMethod("letterswithbasicpunc", function(value, element) { 508 return this.optional(element) || /^[a-z\-.,()'"\s]+$/i.test(value); 509 }, "Letters or punctuation only please"); 510 511 $.validator.addMethod("mobileNL", function(value, element) { 512 return this.optional(element) || /^((\+|00(\s|\s?\-\s?)?)31(\s|\s?\-\s?)?(\(0\)[\-\s]?)?|0)6((\s|\s?\-\s?)?[0-9]){8}$/.test(value); 513 }, "Please specify a valid mobile number"); 514 515 /* For UK phone functions, do the following server side processing: 516 * Compare original input with this RegEx pattern: 517 * ^\(?(?:(?:00\)?[\s\-]?\(?|\+)(44)\)?[\s\-]?\(?(?:0\)?[\s\-]?\(?)?|0)([1-9]\d{1,4}\)?[\s\d\-]+)$ 518 * Extract $1 and set $prefix to '+44<space>' if $1 is '44', otherwise set $prefix to '0' 519 * Extract $2 and remove hyphens, spaces and parentheses. Phone number is combined $prefix and $2. 520 * A number of very detailed GB telephone number RegEx patterns can also be found at: 521 * http://www.aa-asterisk.org.uk/index.php/Regular_Expressions_for_Validating_and_Formatting_GB_Telephone_Numbers 522 */ 523 $.validator.addMethod("mobileUK", function(phone_number, element) { 524 phone_number = phone_number.replace(/\(|\)|\s+|-/g, ""); 525 return this.optional(element) || phone_number.length > 9 && 526 phone_number.match(/^(?:(?:(?:00\s?|\+)44\s?|0)7(?:[1345789]\d{2}|624)\s?\d{3}\s?\d{3})$/); 527 }, "Please specify a valid mobile number"); 528 529 /* 530 * The número de identidad de extranjero ( NIE )is a code used to identify the non-nationals in Spain 531 */ 532 $.validator.addMethod( "nieES", function( value ) { 533 "use strict"; 534 535 value = value.toUpperCase(); 536 537 // Basic format test 538 if ( !value.match( "((^[A-Z]{1}[0-9]{7}[A-Z0-9]{1}$|^[T]{1}[A-Z0-9]{8}$)|^[0-9]{8}[A-Z]{1}$)" ) ) { 539 return false; 540 } 541 542 // Test NIE 543 //T 544 if ( /^[T]{1}/.test( value ) ) { 545 return ( value[ 8 ] === /^[T]{1}[A-Z0-9]{8}$/.test( value ) ); 546 } 547 548 //XYZ 549 if ( /^[XYZ]{1}/.test( value ) ) { 550 return ( 551 value[ 8 ] === "TRWAGMYFPDXBNJZSQVHLCKE".charAt( 552 value.replace( "X", "0" ) 553 .replace( "Y", "1" ) 554 .replace( "Z", "2" ) 555 .substring( 0, 8 ) % 23 556 ) 557 ); 558 } 559 560 return false; 561 562 }, "Please specify a valid NIE number." ); 563 564 /* 565 * The Número de Identificación Fiscal ( NIF ) is the way tax identification used in Spain for individuals 566 */ 567 $.validator.addMethod( "nifES", function( value ) { 568 "use strict"; 569 570 value = value.toUpperCase(); 571 572 // Basic format test 573 if ( !value.match("((^[A-Z]{1}[0-9]{7}[A-Z0-9]{1}$|^[T]{1}[A-Z0-9]{8}$)|^[0-9]{8}[A-Z]{1}$)") ) { 574 return false; 575 } 576 577 // Test NIF 578 if ( /^[0-9]{8}[A-Z]{1}$/.test( value ) ) { 579 return ( "TRWAGMYFPDXBNJZSQVHLCKE".charAt( value.substring( 8, 0 ) % 23 ) === value.charAt( 8 ) ); 580 } 581 // Test specials NIF (starts with K, L or M) 582 if ( /^[KLM]{1}/.test( value ) ) { 583 return ( value[ 8 ] === String.fromCharCode( 64 ) ); 584 } 585 586 return false; 587 588 }, "Please specify a valid NIF number." ); 589 590 $.validator.addMethod("nowhitespace", function(value, element) { 591 return this.optional(element) || /^\S+$/i.test(value); 592 }, "No white space please"); 593 594 /** 595 * Return true if the field value matches the given format RegExp 596 * 597 * @example $.validator.methods.pattern("AR1004",element,/^AR\d{4}$/) 598 * @result true 599 * 600 * @example $.validator.methods.pattern("BR1004",element,/^AR\d{4}$/) 601 * @result false 602 * 603 * @name $.validator.methods.pattern 604 * @type Boolean 605 * @cat Plugins/Validate/Methods 606 */ 607 $.validator.addMethod("pattern", function(value, element, param) { 608 if (this.optional(element)) { 609 return true; 610 } 611 if (typeof param === "string") { 612 param = new RegExp(param); 613 } 614 return param.test(value); 615 }, "Invalid format."); 616 617 /** 618 * Dutch phone numbers have 10 digits (or 11 and start with +31). 619 */ 620 $.validator.addMethod("phoneNL", function(value, element) { 621 return this.optional(element) || /^((\+|00(\s|\s?\-\s?)?)31(\s|\s?\-\s?)?(\(0\)[\-\s]?)?|0)[1-9]((\s|\s?\-\s?)?[0-9]){8}$/.test(value); 622 }, "Please specify a valid phone number."); 623 624 /* For UK phone functions, do the following server side processing: 625 * Compare original input with this RegEx pattern: 626 * ^\(?(?:(?:00\)?[\s\-]?\(?|\+)(44)\)?[\s\-]?\(?(?:0\)?[\s\-]?\(?)?|0)([1-9]\d{1,4}\)?[\s\d\-]+)$ 627 * Extract $1 and set $prefix to '+44<space>' if $1 is '44', otherwise set $prefix to '0' 628 * Extract $2 and remove hyphens, spaces and parentheses. Phone number is combined $prefix and $2. 629 * A number of very detailed GB telephone number RegEx patterns can also be found at: 630 * http://www.aa-asterisk.org.uk/index.php/Regular_Expressions_for_Validating_and_Formatting_GB_Telephone_Numbers 631 */ 632 $.validator.addMethod("phoneUK", function(phone_number, element) { 633 phone_number = phone_number.replace(/\(|\)|\s+|-/g, ""); 634 return this.optional(element) || phone_number.length > 9 && 635 phone_number.match(/^(?:(?:(?:00\s?|\+)44\s?)|(?:\(?0))(?:\d{2}\)?\s?\d{4}\s?\d{4}|\d{3}\)?\s?\d{3}\s?\d{3,4}|\d{4}\)?\s?(?:\d{5}|\d{3}\s?\d{3})|\d{5}\)?\s?\d{4,5})$/); 636 }, "Please specify a valid phone number"); 637 638 /** 639 * matches US phone number format 640 * 641 * where the area code may not start with 1 and the prefix may not start with 1 642 * allows '-' or ' ' as a separator and allows parens around area code 643 * some people may want to put a '1' in front of their number 644 * 645 * 1(212)-999-2345 or 646 * 212 999 2344 or 647 * 212-999-0983 648 * 649 * but not 650 * 111-123-5434 651 * and not 652 * 212 123 4567 653 */ 654 $.validator.addMethod("phoneUS", function(phone_number, element) { 655 phone_number = phone_number.replace(/\s+/g, ""); 656 return this.optional(element) || phone_number.length > 9 && 657 phone_number.match(/^(\+?1-?)?(\([2-9]([02-9]\d|1[02-9])\)|[2-9]([02-9]\d|1[02-9]))-?[2-9]([02-9]\d|1[02-9])-?\d{4}$/); 658 }, "Please specify a valid phone number"); 659 660 /* For UK phone functions, do the following server side processing: 661 * Compare original input with this RegEx pattern: 662 * ^\(?(?:(?:00\)?[\s\-]?\(?|\+)(44)\)?[\s\-]?\(?(?:0\)?[\s\-]?\(?)?|0)([1-9]\d{1,4}\)?[\s\d\-]+)$ 663 * Extract $1 and set $prefix to '+44<space>' if $1 is '44', otherwise set $prefix to '0' 664 * Extract $2 and remove hyphens, spaces and parentheses. Phone number is combined $prefix and $2. 665 * A number of very detailed GB telephone number RegEx patterns can also be found at: 666 * http://www.aa-asterisk.org.uk/index.php/Regular_Expressions_for_Validating_and_Formatting_GB_Telephone_Numbers 667 */ 668 //Matches UK landline + mobile, accepting only 01-3 for landline or 07 for mobile to exclude many premium numbers 669 $.validator.addMethod("phonesUK", function(phone_number, element) { 670 phone_number = phone_number.replace(/\(|\)|\s+|-/g, ""); 671 return this.optional(element) || phone_number.length > 9 && 672 phone_number.match(/^(?:(?:(?:00\s?|\+)44\s?|0)(?:1\d{8,9}|[23]\d{9}|7(?:[1345789]\d{8}|624\d{6})))$/); 673 }, "Please specify a valid uk phone number"); 674 675 /** 676 * Matches a valid Canadian Postal Code 677 * 678 * @example jQuery.validator.methods.postalCodeCA( "H0H 0H0", element ) 679 * @result true 680 * 681 * @example jQuery.validator.methods.postalCodeCA( "H0H0H0", element ) 682 * @result false 683 * 684 * @name jQuery.validator.methods.postalCodeCA 685 * @type Boolean 686 * @cat Plugins/Validate/Methods 687 */ 688 $.validator.addMethod( "postalCodeCA", function( value, element ) { 689 return this.optional( element ) || /^[ABCEGHJKLMNPRSTVXY]\d[A-Z] \d[A-Z]\d$/.test( value ); 690 }, "Please specify a valid postal code" ); 691 692 /* Matches Italian postcode (CAP) */ 693 $.validator.addMethod("postalcodeIT", function(value, element) { 694 return this.optional(element) || /^\d{5}$/.test(value); 695 }, "Please specify a valid postal code"); 696 697 $.validator.addMethod("postalcodeNL", function(value, element) { 698 return this.optional(element) || /^[1-9][0-9]{3}\s?[a-zA-Z]{2}$/.test(value); 699 }, "Please specify a valid postal code"); 700 701 // Matches UK postcode. Does not match to UK Channel Islands that have their own postcodes (non standard UK) 702 $.validator.addMethod("postcodeUK", function(value, element) { 703 return this.optional(element) || /^((([A-PR-UWYZ][0-9])|([A-PR-UWYZ][0-9][0-9])|([A-PR-UWYZ][A-HK-Y][0-9])|([A-PR-UWYZ][A-HK-Y][0-9][0-9])|([A-PR-UWYZ][0-9][A-HJKSTUW])|([A-PR-UWYZ][A-HK-Y][0-9][ABEHMNPRVWXY]))\s?([0-9][ABD-HJLNP-UW-Z]{2})|(GIR)\s?(0AA))$/i.test(value); 704 }, "Please specify a valid UK postcode"); 705 706 /* 707 * Lets you say "at least X inputs that match selector Y must be filled." 708 * 709 * The end result is that neither of these inputs: 710 * 711 * <input class="productinfo" name="partnumber"> 712 * <input class="productinfo" name="description"> 713 * 714 * ...will validate unless at least one of them is filled. 715 * 716 * partnumber: {require_from_group: [1,".productinfo"]}, 717 * description: {require_from_group: [1,".productinfo"]} 718 * 719 * options[0]: number of fields that must be filled in the group 720 * options[1]: CSS selector that defines the group of conditionally required fields 721 */ 722 $.validator.addMethod("require_from_group", function(value, element, options) { 723 var $fields = $(options[1], element.form), 724 $fieldsFirst = $fields.eq(0), 725 validator = $fieldsFirst.data("valid_req_grp") ? $fieldsFirst.data("valid_req_grp") : $.extend({}, this), 726 isValid = $fields.filter(function() { 727 return validator.elementValue(this); 728 }).length >= options[0]; 729 730 // Store the cloned validator for future validation 731 $fieldsFirst.data("valid_req_grp", validator); 732 733 // If element isn't being validated, run each require_from_group field's validation rules 734 if (!$(element).data("being_validated")) { 735 $fields.data("being_validated", true); 736 $fields.each(function() { 737 validator.element(this); 738 }); 739 $fields.data("being_validated", false); 740 } 741 return isValid; 742 }, $.validator.format("Please fill at least {0} of these fields.")); 743 744 /* 745 * Lets you say "either at least X inputs that match selector Y must be filled, 746 * OR they must all be skipped (left blank)." 747 * 748 * The end result, is that none of these inputs: 749 * 750 * <input class="productinfo" name="partnumber"> 751 * <input class="productinfo" name="description"> 752 * <input class="productinfo" name="color"> 753 * 754 * ...will validate unless either at least two of them are filled, 755 * OR none of them are. 756 * 757 * partnumber: {skip_or_fill_minimum: [2,".productinfo"]}, 758 * description: {skip_or_fill_minimum: [2,".productinfo"]}, 759 * color: {skip_or_fill_minimum: [2,".productinfo"]} 760 * 761 * options[0]: number of fields that must be filled in the group 762 * options[1]: CSS selector that defines the group of conditionally required fields 763 * 764 */ 765 $.validator.addMethod("skip_or_fill_minimum", function(value, element, options) { 766 var $fields = $(options[1], element.form), 767 $fieldsFirst = $fields.eq(0), 768 validator = $fieldsFirst.data("valid_skip") ? $fieldsFirst.data("valid_skip") : $.extend({}, this), 769 numberFilled = $fields.filter(function() { 770 return validator.elementValue(this); 771 }).length, 772 isValid = numberFilled === 0 || numberFilled >= options[0]; 773 774 // Store the cloned validator for future validation 775 $fieldsFirst.data("valid_skip", validator); 776 777 // If element isn't being validated, run each skip_or_fill_minimum field's validation rules 778 if (!$(element).data("being_validated")) { 779 $fields.data("being_validated", true); 780 $fields.each(function() { 781 validator.element(this); 782 }); 783 $fields.data("being_validated", false); 784 } 785 return isValid; 786 }, $.validator.format("Please either skip these fields or fill at least {0} of them.")); 787 788 /* Validates US States and/or Territories by @jdforsythe 789 * Can be case insensitive or require capitalization - default is case insensitive 790 * Can include US Territories or not - default does not 791 * Can include US Military postal abbreviations (AA, AE, AP) - default does not 792 * 793 * Note: "States" always includes DC (District of Colombia) 794 * 795 * Usage examples: 796 * 797 * This is the default - case insensitive, no territories, no military zones 798 * stateInput: { 799 * caseSensitive: false, 800 * includeTerritories: false, 801 * includeMilitary: false 802 * } 803 * 804 * Only allow capital letters, no territories, no military zones 805 * stateInput: { 806 * caseSensitive: false 807 * } 808 * 809 * Case insensitive, include territories but not military zones 810 * stateInput: { 811 * includeTerritories: true 812 * } 813 * 814 * Only allow capital letters, include territories and military zones 815 * stateInput: { 816 * caseSensitive: true, 817 * includeTerritories: true, 818 * includeMilitary: true 819 * } 820 * 821 * 822 * 823 */ 824 825 jQuery.validator.addMethod("stateUS", function(value, element, options) { 826 var isDefault = typeof options === "undefined", 827 caseSensitive = ( isDefault || typeof options.caseSensitive === "undefined" ) ? false : options.caseSensitive, 828 includeTerritories = ( isDefault || typeof options.includeTerritories === "undefined" ) ? false : options.includeTerritories, 829 includeMilitary = ( isDefault || typeof options.includeMilitary === "undefined" ) ? false : options.includeMilitary, 830 regex; 831 832 if (!includeTerritories && !includeMilitary) { 833 regex = "^(A[KLRZ]|C[AOT]|D[CE]|FL|GA|HI|I[ADLN]|K[SY]|LA|M[ADEINOST]|N[CDEHJMVY]|O[HKR]|PA|RI|S[CD]|T[NX]|UT|V[AT]|W[AIVY])$"; 834 } else if (includeTerritories && includeMilitary) { 835 regex = "^(A[AEKLPRSZ]|C[AOT]|D[CE]|FL|G[AU]|HI|I[ADLN]|K[SY]|LA|M[ADEINOPST]|N[CDEHJMVY]|O[HKR]|P[AR]|RI|S[CD]|T[NX]|UT|V[AIT]|W[AIVY])$"; 836 } else if (includeTerritories) { 837 regex = "^(A[KLRSZ]|C[AOT]|D[CE]|FL|G[AU]|HI|I[ADLN]|K[SY]|LA|M[ADEINOPST]|N[CDEHJMVY]|O[HKR]|P[AR]|RI|S[CD]|T[NX]|UT|V[AIT]|W[AIVY])$"; 838 } else { 839 regex = "^(A[AEKLPRZ]|C[AOT]|D[CE]|FL|GA|HI|I[ADLN]|K[SY]|LA|M[ADEINOST]|N[CDEHJMVY]|O[HKR]|PA|RI|S[CD]|T[NX]|UT|V[AT]|W[AIVY])$"; 840 } 841 842 regex = caseSensitive ? new RegExp(regex) : new RegExp(regex, "i"); 843 return this.optional(element) || regex.test(value); 844 }, 845 "Please specify a valid state"); 846 847 // TODO check if value starts with <, otherwise don't try stripping anything 848 $.validator.addMethod("strippedminlength", function(value, element, param) { 849 return $(value).text().length >= param; 850 }, $.validator.format("Please enter at least {0} characters")); 851 852 $.validator.addMethod("time", function(value, element) { 853 return this.optional(element) || /^([01]\d|2[0-3])(:[0-5]\d){1,2}$/.test(value); 854 }, "Please enter a valid time, between 00:00 and 23:59"); 855 856 $.validator.addMethod("time12h", function(value, element) { 857 return this.optional(element) || /^((0?[1-9]|1[012])(:[0-5]\d){1,2}(\ ?[AP]M))$/i.test(value); 858 }, "Please enter a valid time in 12-hour am/pm format"); 859 860 // same as url, but TLD is optional 861 $.validator.addMethod("url2", function(value, element) { 862 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); 863 }, $.validator.messages.url); 864 865 /** 866 * Return true, if the value is a valid vehicle identification number (VIN). 867 * 868 * Works with all kind of text inputs. 869 * 870 * @example <input type="text" size="20" name="VehicleID" class="{required:true,vinUS:true}" /> 871 * @desc Declares a required input element whose value must be a valid vehicle identification number. 872 * 873 * @name $.validator.methods.vinUS 874 * @type Boolean 875 * @cat Plugins/Validate/Methods 876 */ 877 $.validator.addMethod("vinUS", function(v) { 878 if (v.length !== 17) { 879 return false; 880 } 881 882 var LL = [ "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" ], 883 VL = [ 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 7, 9, 2, 3, 4, 5, 6, 7, 8, 9 ], 884 FL = [ 8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2 ], 885 rs = 0, 886 i, n, d, f, cd, cdv; 887 888 for (i = 0; i < 17; i++) { 889 f = FL[i]; 890 d = v.slice(i, i + 1); 891 if (i === 8) { 892 cdv = d; 893 } 894 if (!isNaN(d)) { 895 d *= f; 896 } else { 897 for (n = 0; n < LL.length; n++) { 898 if (d.toUpperCase() === LL[n]) { 899 d = VL[n]; 900 d *= f; 901 if (isNaN(cdv) && n === 8) { 902 cdv = LL[n]; 903 } 904 break; 905 } 906 } 907 } 908 rs += d; 909 } 910 cd = rs % 11; 911 if (cd === 10) { 912 cd = "X"; 913 } 914 if (cd === cdv) { 915 return true; 916 } 917 return false; 918 }, "The specified vehicle identification number (VIN) is invalid."); 919 920 $.validator.addMethod("zipcodeUS", function(value, element) { 921 return this.optional(element) || /^\d{5}(-\d{4})?$/.test(value); 922 }, "The specified US ZIP Code is invalid"); 923 924 $.validator.addMethod("ziprange", function(value, element) { 925 return this.optional(element) || /^90[2-5]\d\{2\}-\d{4}$/.test(value); 926 }, "Your ZIP-code must be in the range 902xx-xxxx to 905xx-xxxx"); 927 928 }));