additional-methods.js 40 KB

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