您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

additional-methods.js 47KB

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