additional-methods.js 38 KB

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