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