additional-methods.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. /*!
  2. * jQuery Validation Plugin 1.11.1
  3. *
  4. * http://bassistance.de/jquery-plugins/jquery-plugin-validation/
  5. * http://docs.jquery.com/Plugins/Validation
  6. *
  7. * Copyright 2013 Jörn Zaefferer
  8. * Released under the MIT license:
  9. * http://www.opensource.org/licenses/mit-license.php
  10. */
  11. (function() {
  12. function stripHtml(value) {
  13. // remove html tags and space chars
  14. return value.replace(/<.[^<>]*?>/g, ' ').replace(/&nbsp;|&#160;/gi, ' ')
  15. // remove punctuation
  16. .replace(/[.(),;:!?%#$'"_+=\/\-]*/g,'');
  17. }
  18. jQuery.validator.addMethod("maxWords", function(value, element, params) {
  19. return this.optional(element) || stripHtml(value).match(/\b\w+\b/g).length <= params;
  20. }, jQuery.validator.format("Please enter {0} words or less."));
  21. jQuery.validator.addMethod("minWords", function(value, element, params) {
  22. return this.optional(element) || stripHtml(value).match(/\b\w+\b/g).length >= params;
  23. }, jQuery.validator.format("Please enter at least {0} words."));
  24. jQuery.validator.addMethod("rangeWords", function(value, element, params) {
  25. var valueStripped = stripHtml(value);
  26. var regex = /\b\w+\b/g;
  27. return this.optional(element) || valueStripped.match(regex).length >= params[0] && valueStripped.match(regex).length <= params[1];
  28. }, jQuery.validator.format("Please enter between {0} and {1} words."));
  29. }());
  30. jQuery.validator.addMethod("letterswithbasicpunc", function(value, element) {
  31. return this.optional(element) || /^[a-z\-.,()'"\s]+$/i.test(value);
  32. }, "Letters or punctuation only please");
  33. jQuery.validator.addMethod("alphanumeric", function(value, element) {
  34. return this.optional(element) || /^\w+$/i.test(value);
  35. }, "Letters, numbers, and underscores only please");
  36. jQuery.validator.addMethod("lettersonly", function(value, element) {
  37. return this.optional(element) || /^[a-z]+$/i.test(value);
  38. }, "Letters only please");
  39. jQuery.validator.addMethod("nowhitespace", function(value, element) {
  40. return this.optional(element) || /^\S+$/i.test(value);
  41. }, "No white space please");
  42. jQuery.validator.addMethod("ziprange", function(value, element) {
  43. return this.optional(element) || /^90[2-5]\d\{2\}-\d{4}$/.test(value);
  44. }, "Your ZIP-code must be in the range 902xx-xxxx to 905-xx-xxxx");
  45. jQuery.validator.addMethod("zipcodeUS", function(value, element) {
  46. return this.optional(element) || /\d{5}-\d{4}$|^\d{5}$/.test(value);
  47. }, "The specified US ZIP Code is invalid");
  48. jQuery.validator.addMethod("integer", function(value, element) {
  49. return this.optional(element) || /^-?\d+$/.test(value);
  50. }, "A positive or negative non-decimal number please");
  51. /**
  52. * Return true, if the value is a valid vehicle identification number (VIN).
  53. *
  54. * Works with all kind of text inputs.
  55. *
  56. * @example <input type="text" size="20" name="VehicleID" class="{required:true,vinUS:true}" />
  57. * @desc Declares a required input element whose value must be a valid vehicle identification number.
  58. *
  59. * @name jQuery.validator.methods.vinUS
  60. * @type Boolean
  61. * @cat Plugins/Validate/Methods
  62. */
  63. jQuery.validator.addMethod("vinUS", function(v) {
  64. if (v.length !== 17) {
  65. return false;
  66. }
  67. var i, n, d, f, cd, cdv;
  68. 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"];
  69. var VL = [1,2,3,4,5,6,7,8,1,2,3,4,5,7,9,2,3,4,5,6,7,8,9];
  70. var FL = [8,7,6,5,4,3,2,10,0,9,8,7,6,5,4,3,2];
  71. var rs = 0;
  72. for(i = 0; i < 17; i++){
  73. f = FL[i];
  74. d = v.slice(i,i+1);
  75. if (i === 8) {
  76. cdv = d;
  77. }
  78. if (!isNaN(d)) {
  79. d *= f;
  80. } else {
  81. for (n = 0; n < LL.length; n++) {
  82. if (d.toUpperCase() === LL[n]) {
  83. d = VL[n];
  84. d *= f;
  85. if (isNaN(cdv) && n === 8) {
  86. cdv = LL[n];
  87. }
  88. break;
  89. }
  90. }
  91. }
  92. rs += d;
  93. }
  94. cd = rs % 11;
  95. if (cd === 10) {
  96. cd = "X";
  97. }
  98. if (cd === cdv) {
  99. return true;
  100. }
  101. return false;
  102. }, "The specified vehicle identification number (VIN) is invalid.");
  103. /**
  104. * Return true, if the value is a valid date, also making this formal check dd/mm/yyyy.
  105. *
  106. * @example jQuery.validator.methods.date("01/01/1900")
  107. * @result true
  108. *
  109. * @example jQuery.validator.methods.date("01/13/1990")
  110. * @result false
  111. *
  112. * @example jQuery.validator.methods.date("01.01.1900")
  113. * @result false
  114. *
  115. * @example <input name="pippo" class="{dateITA:true}" />
  116. * @desc Declares an optional input element whose value must be a valid date.
  117. *
  118. * @name jQuery.validator.methods.dateITA
  119. * @type Boolean
  120. * @cat Plugins/Validate/Methods
  121. */
  122. jQuery.validator.addMethod("dateITA", function(value, element) {
  123. var check = false;
  124. var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
  125. if( re.test(value)) {
  126. var adata = value.split('/');
  127. var gg = parseInt(adata[0],10);
  128. var mm = parseInt(adata[1],10);
  129. var aaaa = parseInt(adata[2],10);
  130. var xdata = new Date(aaaa,mm-1,gg);
  131. if ( ( xdata.getFullYear() === aaaa ) && ( xdata.getMonth() === mm - 1 ) && ( xdata.getDate() === gg ) ){
  132. check = true;
  133. } else {
  134. check = false;
  135. }
  136. } else {
  137. check = false;
  138. }
  139. return this.optional(element) || check;
  140. }, "Please enter a correct date");
  141. /**
  142. * IBAN is the international bank account number.
  143. * It has a country - specific format, that is checked here too
  144. */
  145. jQuery.validator.addMethod("iban", function(value, element) {
  146. // some quick simple tests to prevent needless work
  147. if (this.optional(element)) {
  148. return true;
  149. }
  150. if (!(/^([a-zA-Z0-9]{4} ){2,8}[a-zA-Z0-9]{1,4}|[a-zA-Z0-9]{12,34}$/.test(value))) {
  151. return false;
  152. }
  153. // check the country code and find the country specific format
  154. var iban = value.replace(/ /g,'').toUpperCase(); // remove spaces and to upper case
  155. var countrycode = iban.substring(0,2);
  156. var bbancountrypatterns = {
  157. 'AL': "\\d{8}[\\dA-Z]{16}",
  158. 'AD': "\\d{8}[\\dA-Z]{12}",
  159. 'AT': "\\d{16}",
  160. 'AZ': "[\\dA-Z]{4}\\d{20}",
  161. 'BE': "\\d{12}",
  162. 'BH': "[A-Z]{4}[\\dA-Z]{14}",
  163. 'BA': "\\d{16}",
  164. 'BR': "\\d{23}[A-Z][\\dA-Z]",
  165. 'BG': "[A-Z]{4}\\d{6}[\\dA-Z]{8}",
  166. 'CR': "\\d{17}",
  167. 'HR': "\\d{17}",
  168. 'CY': "\\d{8}[\\dA-Z]{16}",
  169. 'CZ': "\\d{20}",
  170. 'DK': "\\d{14}",
  171. 'DO': "[A-Z]{4}\\d{20}",
  172. 'EE': "\\d{16}",
  173. 'FO': "\\d{14}",
  174. 'FI': "\\d{14}",
  175. 'FR': "\\d{10}[\\dA-Z]{11}\\d{2}",
  176. 'GE': "[\\dA-Z]{2}\\d{16}",
  177. 'DE': "\\d{18}",
  178. 'GI': "[A-Z]{4}[\\dA-Z]{15}",
  179. 'GR': "\\d{7}[\\dA-Z]{16}",
  180. 'GL': "\\d{14}",
  181. 'GT': "[\\dA-Z]{4}[\\dA-Z]{20}",
  182. 'HU': "\\d{24}",
  183. 'IS': "\\d{22}",
  184. 'IE': "[\\dA-Z]{4}\\d{14}",
  185. 'IL': "\\d{19}",
  186. 'IT': "[A-Z]\\d{10}[\\dA-Z]{12}",
  187. 'KZ': "\\d{3}[\\dA-Z]{13}",
  188. 'KW': "[A-Z]{4}[\\dA-Z]{22}",
  189. 'LV': "[A-Z]{4}[\\dA-Z]{13}",
  190. 'LB': "\\d{4}[\\dA-Z]{20}",
  191. 'LI': "\\d{5}[\\dA-Z]{12}",
  192. 'LT': "\\d{16}",
  193. 'LU': "\\d{3}[\\dA-Z]{13}",
  194. 'MK': "\\d{3}[\\dA-Z]{10}\\d{2}",
  195. 'MT': "[A-Z]{4}\\d{5}[\\dA-Z]{18}",
  196. 'MR': "\\d{23}",
  197. 'MU': "[A-Z]{4}\\d{19}[A-Z]{3}",
  198. 'MC': "\\d{10}[\\dA-Z]{11}\\d{2}",
  199. 'MD': "[\\dA-Z]{2}\\d{18}",
  200. 'ME': "\\d{18}",
  201. 'NL': "[A-Z]{4}\\d{10}",
  202. 'NO': "\\d{11}",
  203. 'PK': "[\\dA-Z]{4}\\d{16}",
  204. 'PS': "[\\dA-Z]{4}\\d{21}",
  205. 'PL': "\\d{24}",
  206. 'PT': "\\d{21}",
  207. 'RO': "[A-Z]{4}[\\dA-Z]{16}",
  208. 'SM': "[A-Z]\\d{10}[\\dA-Z]{12}",
  209. 'SA': "\\d{2}[\\dA-Z]{18}",
  210. 'RS': "\\d{18}",
  211. 'SK': "\\d{20}",
  212. 'SI': "\\d{15}",
  213. 'ES': "\\d{20}",
  214. 'SE': "\\d{20}",
  215. 'CH': "\\d{5}[\\dA-Z]{12}",
  216. 'TN': "\\d{20}",
  217. 'TR': "\\d{5}[\\dA-Z]{17}",
  218. 'AE': "\\d{3}\\d{16}",
  219. 'GB': "[A-Z]{4}\\d{14}",
  220. 'VG': "[\\dA-Z]{4}\\d{16}"
  221. };
  222. var bbanpattern = bbancountrypatterns[countrycode];
  223. // As new countries will start using IBAN in the
  224. // future, we only check if the countrycode is known.
  225. // This prevents false negatives, while almost all
  226. // false positives introduced by this, will be caught
  227. // by the checksum validation below anyway.
  228. // Strict checking should return FALSE for unknown
  229. // countries.
  230. if (typeof bbanpattern !== 'undefined') {
  231. var ibanregexp = new RegExp("^[A-Z]{2}\\d{2}" + bbanpattern + "$", "");
  232. if (!(ibanregexp.test(iban))) {
  233. return false; // invalid country specific format
  234. }
  235. }
  236. // now check the checksum, first convert to digits
  237. var ibancheck = iban.substring(4,iban.length) + iban.substring(0,4);
  238. var ibancheckdigits = "";
  239. var leadingZeroes = true;
  240. var charAt;
  241. for (var i =0; i<ibancheck.length; i++) {
  242. charAt = ibancheck.charAt(i);
  243. if (charAt !== "0") {
  244. leadingZeroes = false;
  245. }
  246. if (!leadingZeroes) {
  247. ibancheckdigits += "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".indexOf(charAt);
  248. }
  249. }
  250. // calculate the result of: ibancheckdigits % 97
  251. var cRest = '';
  252. var cOperator = '';
  253. for (var p=0; p<ibancheckdigits.length; p++) {
  254. var cChar = ibancheckdigits.charAt(p);
  255. cOperator = '' + cRest + '' + cChar;
  256. cRest = cOperator % 97;
  257. }
  258. return cRest === 1;
  259. }, "Please specify a valid IBAN");
  260. jQuery.validator.addMethod("dateNL", function(value, element) {
  261. return this.optional(element) || /^(0?[1-9]|[12]\d|3[01])[\.\/\-](0?[1-9]|1[012])[\.\/\-]([12]\d)?(\d\d)$/.test(value);
  262. }, "Please enter a correct date");
  263. /**
  264. * Dutch phone numbers have 10 digits (or 11 and start with +31).
  265. */
  266. jQuery.validator.addMethod("phoneNL", function(value, element) {
  267. 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);
  268. }, "Please specify a valid phone number.");
  269. jQuery.validator.addMethod("mobileNL", function(value, element) {
  270. return this.optional(element) || /^((\+|00(\s|\s?\-\s?)?)31(\s|\s?\-\s?)?(\(0\)[\-\s]?)?|0)6((\s|\s?\-\s?)?[0-9]){8}$/.test(value);
  271. }, "Please specify a valid mobile number");
  272. jQuery.validator.addMethod("postalcodeNL", function(value, element) {
  273. return this.optional(element) || /^[1-9][0-9]{3}\s?[a-zA-Z]{2}$/.test(value);
  274. }, "Please specify a valid postal code");
  275. /*
  276. * Dutch bank account numbers (not 'giro' numbers) have 9 digits
  277. * and pass the '11 check'.
  278. * We accept the notation with spaces, as that is common.
  279. * acceptable: 123456789 or 12 34 56 789
  280. */
  281. jQuery.validator.addMethod("bankaccountNL", function(value, element) {
  282. if (this.optional(element)) {
  283. return true;
  284. }
  285. if (!(/^[0-9]{9}|([0-9]{2} ){3}[0-9]{3}$/.test(value))) {
  286. return false;
  287. }
  288. // now '11 check'
  289. var account = value.replace(/ /g,''); // remove spaces
  290. var sum = 0;
  291. var len = account.length;
  292. for (var pos=0; pos<len; pos++) {
  293. var factor = len - pos;
  294. var digit = account.substring(pos, pos+1);
  295. sum = sum + factor * digit;
  296. }
  297. return sum % 11 === 0;
  298. }, "Please specify a valid bank account number");
  299. /**
  300. * Dutch giro account numbers (not bank numbers) have max 7 digits
  301. */
  302. jQuery.validator.addMethod("giroaccountNL", function(value, element) {
  303. return this.optional(element) || /^[0-9]{1,7}$/.test(value);
  304. }, "Please specify a valid giro account number");
  305. jQuery.validator.addMethod("bankorgiroaccountNL", function(value, element) {
  306. return this.optional(element) ||
  307. ($.validator.methods["bankaccountNL"].call(this, value, element)) ||
  308. ($.validator.methods["giroaccountNL"].call(this, value, element));
  309. }, "Please specify a valid bank or giro account number");
  310. jQuery.validator.addMethod("time", function(value, element) {
  311. return this.optional(element) || /^([01]\d|2[0-3])(:[0-5]\d){1,2}$/.test(value);
  312. }, "Please enter a valid time, between 00:00 and 23:59");
  313. jQuery.validator.addMethod("time12h", function(value, element) {
  314. return this.optional(element) || /^((0?[1-9]|1[012])(:[0-5]\d){1,2}(\ ?[AP]M))$/i.test(value);
  315. }, "Please enter a valid time in 12-hour am/pm format");
  316. /**
  317. * matches US phone number format
  318. *
  319. * where the area code may not start with 1 and the prefix may not start with 1
  320. * allows '-' or ' ' as a separator and allows parens around area code
  321. * some people may want to put a '1' in front of their number
  322. *
  323. * 1(212)-999-2345 or
  324. * 212 999 2344 or
  325. * 212-999-0983
  326. *
  327. * but not
  328. * 111-123-5434
  329. * and not
  330. * 212 123 4567
  331. */
  332. jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
  333. phone_number = phone_number.replace(/\s+/g, "");
  334. return this.optional(element) || phone_number.length > 9 &&
  335. phone_number.match(/^(\+?1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
  336. }, "Please specify a valid phone number");
  337. jQuery.validator.addMethod('phoneUK', function(phone_number, element) {
  338. phone_number = phone_number.replace(/\(|\)|\s+|-/g,'');
  339. return this.optional(element) || phone_number.length > 9 &&
  340. 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})$/);
  341. }, 'Please specify a valid phone number');
  342. jQuery.validator.addMethod('mobileUK', function(phone_number, element) {
  343. phone_number = phone_number.replace(/\(|\)|\s+|-/g,'');
  344. return this.optional(element) || phone_number.length > 9 &&
  345. phone_number.match(/^(?:(?:(?:00\s?|\+)44\s?|0)7(?:[45789]\d{2}|624)\s?\d{3}\s?\d{3})$/);
  346. }, 'Please specify a valid mobile number');
  347. //Matches UK landline + mobile, accepting only 01-3 for landline or 07 for mobile to exclude many premium numbers
  348. jQuery.validator.addMethod('phonesUK', function(phone_number, element) {
  349. phone_number = phone_number.replace(/\(|\)|\s+|-/g,'');
  350. return this.optional(element) || phone_number.length > 9 &&
  351. phone_number.match(/^(?:(?:(?:00\s?|\+)44\s?|0)(?:1\d{8,9}|[23]\d{9}|7(?:[45789]\d{8}|624\d{6})))$/);
  352. }, 'Please specify a valid uk phone number');
  353. // On the above three UK functions, do the following server side processing:
  354. // Compare original input with this RegEx pattern:
  355. // ^\(?(?:(?:00\)?[\s\-]?\(?|\+)(44)\)?[\s\-]?\(?(?:0\)?[\s\-]?\(?)?|0)([1-9]\d{1,4}\)?[\s\d\-]+)$
  356. // Extract $1 and set $prefix to '+44<space>' if $1 is '44', otherwise set $prefix to '0'
  357. // Extract $2 and remove hyphens, spaces and parentheses. Phone number is combined $prefix and $2.
  358. // A number of very detailed GB telephone number RegEx patterns can also be found at:
  359. // http://www.aa-asterisk.org.uk/index.php/Regular_Expressions_for_Validating_and_Formatting_GB_Telephone_Numbers
  360. // Matches UK postcode. Does not match to UK Channel Islands that have their own postcodes (non standard UK)
  361. jQuery.validator.addMethod('postcodeUK', function(value, element) {
  362. 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);
  363. }, 'Please specify a valid UK postcode');
  364. // TODO check if value starts with <, otherwise don't try stripping anything
  365. jQuery.validator.addMethod("strippedminlength", function(value, element, param) {
  366. return jQuery(value).text().length >= param;
  367. }, jQuery.validator.format("Please enter at least {0} characters"));
  368. // same as email, but TLD is optional
  369. jQuery.validator.addMethod("email2", function(value, element, param) {
  370. return this.optional(element) || /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([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])))\.?$/i.test(value);
  371. }, jQuery.validator.messages.email);
  372. // same as url, but TLD is optional
  373. jQuery.validator.addMethod("url2", function(value, element, param) {
  374. 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);
  375. }, jQuery.validator.messages.url);
  376. // NOTICE: Modified version of Castle.Components.Validator.CreditCardValidator
  377. // Redistributed under the the Apache License 2.0 at http://www.apache.org/licenses/LICENSE-2.0
  378. // Valid Types: mastercard, visa, amex, dinersclub, enroute, discover, jcb, unknown, all (overrides all other settings)
  379. jQuery.validator.addMethod("creditcardtypes", function(value, element, param) {
  380. if (/[^0-9\-]+/.test(value)) {
  381. return false;
  382. }
  383. value = value.replace(/\D/g, "");
  384. var validTypes = 0x0000;
  385. if (param.mastercard) {
  386. validTypes |= 0x0001;
  387. }
  388. if (param.visa) {
  389. validTypes |= 0x0002;
  390. }
  391. if (param.amex) {
  392. validTypes |= 0x0004;
  393. }
  394. if (param.dinersclub) {
  395. validTypes |= 0x0008;
  396. }
  397. if (param.enroute) {
  398. validTypes |= 0x0010;
  399. }
  400. if (param.discover) {
  401. validTypes |= 0x0020;
  402. }
  403. if (param.jcb) {
  404. validTypes |= 0x0040;
  405. }
  406. if (param.unknown) {
  407. validTypes |= 0x0080;
  408. }
  409. if (param.all) {
  410. validTypes = 0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080;
  411. }
  412. if (validTypes & 0x0001 && /^(5[12345])/.test(value)) { //mastercard
  413. return value.length === 16;
  414. }
  415. if (validTypes & 0x0002 && /^(4)/.test(value)) { //visa
  416. return value.length === 16;
  417. }
  418. if (validTypes & 0x0004 && /^(3[47])/.test(value)) { //amex
  419. return value.length === 15;
  420. }
  421. if (validTypes & 0x0008 && /^(3(0[012345]|[68]))/.test(value)) { //dinersclub
  422. return value.length === 14;
  423. }
  424. if (validTypes & 0x0010 && /^(2(014|149))/.test(value)) { //enroute
  425. return value.length === 15;
  426. }
  427. if (validTypes & 0x0020 && /^(6011)/.test(value)) { //discover
  428. return value.length === 16;
  429. }
  430. if (validTypes & 0x0040 && /^(3)/.test(value)) { //jcb
  431. return value.length === 16;
  432. }
  433. if (validTypes & 0x0040 && /^(2131|1800)/.test(value)) { //jcb
  434. return value.length === 15;
  435. }
  436. if (validTypes & 0x0080) { //unknown
  437. return true;
  438. }
  439. return false;
  440. }, "Please enter a valid credit card number.");
  441. jQuery.validator.addMethod("ipv4", function(value, element, param) {
  442. 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);
  443. }, "Please enter a valid IP v4 address.");
  444. jQuery.validator.addMethod("ipv6", function(value, element, param) {
  445. 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);
  446. }, "Please enter a valid IP v6 address.");
  447. /**
  448. * Return true if the field value matches the given format RegExp
  449. *
  450. * @example jQuery.validator.methods.pattern("AR1004",element,/^AR\d{4}$/)
  451. * @result true
  452. *
  453. * @example jQuery.validator.methods.pattern("BR1004",element,/^AR\d{4}$/)
  454. * @result false
  455. *
  456. * @name jQuery.validator.methods.pattern
  457. * @type Boolean
  458. * @cat Plugins/Validate/Methods
  459. */
  460. jQuery.validator.addMethod("pattern", function(value, element, param) {
  461. if (this.optional(element)) {
  462. return true;
  463. }
  464. if (typeof param === 'string') {
  465. param = new RegExp('^(?:' + param + ')$');
  466. }
  467. return param.test(value);
  468. }, "Invalid format.");
  469. /*
  470. * Lets you say "at least X inputs that match selector Y must be filled."
  471. *
  472. * The end result is that neither of these inputs:
  473. *
  474. * <input class="productinfo" name="partnumber">
  475. * <input class="productinfo" name="description">
  476. *
  477. * ...will validate unless at least one of them is filled.
  478. *
  479. * partnumber: {require_from_group: [1,".productinfo"]},
  480. * description: {require_from_group: [1,".productinfo"]}
  481. *
  482. */
  483. jQuery.validator.addMethod("require_from_group", function(value, element, options) {
  484. var validator = this;
  485. var selector = options[1];
  486. var validOrNot = $(selector, element.form).filter(function() {
  487. return validator.elementValue(this);
  488. }).length >= options[0];
  489. if(!$(element).data('being_validated')) {
  490. var fields = $(selector, element.form);
  491. fields.data('being_validated', true);
  492. fields.valid();
  493. fields.data('being_validated', false);
  494. }
  495. return validOrNot;
  496. }, jQuery.format("Please fill at least {0} of these fields."));
  497. /*
  498. * Lets you say "either at least X inputs that match selector Y must be filled,
  499. * OR they must all be skipped (left blank)."
  500. *
  501. * The end result, is that none of these inputs:
  502. *
  503. * <input class="productinfo" name="partnumber">
  504. * <input class="productinfo" name="description">
  505. * <input class="productinfo" name="color">
  506. *
  507. * ...will validate unless either at least two of them are filled,
  508. * OR none of them are.
  509. *
  510. * partnumber: {skip_or_fill_minimum: [2,".productinfo"]},
  511. * description: {skip_or_fill_minimum: [2,".productinfo"]},
  512. * color: {skip_or_fill_minimum: [2,".productinfo"]}
  513. *
  514. */
  515. jQuery.validator.addMethod("skip_or_fill_minimum", function(value, element, options) {
  516. var validator = this,
  517. numberRequired = options[0],
  518. selector = options[1];
  519. var numberFilled = $(selector, element.form).filter(function() {
  520. return validator.elementValue(this);
  521. }).length;
  522. var valid = numberFilled >= numberRequired || numberFilled === 0;
  523. if(!$(element).data('being_validated')) {
  524. var fields = $(selector, element.form);
  525. fields.data('being_validated', true);
  526. fields.valid();
  527. fields.data('being_validated', false);
  528. }
  529. return valid;
  530. }, jQuery.format("Please either skip these fields or fill at least {0} of them."));
  531. // Accept a value from a file input based on a required mimetype
  532. jQuery.validator.addMethod("accept", function(value, element, param) {
  533. // Split mime on commas in case we have multiple types we can accept
  534. var typeParam = typeof param === "string" ? param.replace(/\s/g, '').replace(/,/g, '|') : "image/*",
  535. optionalValue = this.optional(element),
  536. i, file;
  537. // Element is optional
  538. if (optionalValue) {
  539. return optionalValue;
  540. }
  541. if ($(element).attr("type") === "file") {
  542. // If we are using a wildcard, make it regex friendly
  543. typeParam = typeParam.replace(/\*/g, ".*");
  544. // Check if the element has a FileList before checking each file
  545. if (element.files && element.files.length) {
  546. for (i = 0; i < element.files.length; i++) {
  547. file = element.files[i];
  548. // Grab the mimetype from the loaded file, verify it matches
  549. if (!file.type.match(new RegExp( ".?(" + typeParam + ")$", "i"))) {
  550. return false;
  551. }
  552. }
  553. }
  554. }
  555. // Either return true because we've validated each file, or because the
  556. // browser does not support element.files and the FileList feature
  557. return true;
  558. }, jQuery.format("Please enter a value with a valid mimetype."));
  559. // Older "accept" file extension method. Old docs: http://docs.jquery.com/Plugins/Validation/Methods/accept
  560. jQuery.validator.addMethod("extension", function(value, element, param) {
  561. param = typeof param === "string" ? param.replace(/,/g, '|') : "png|jpe?g|gif";
  562. return this.optional(element) || value.match(new RegExp(".(" + param + ")$", "i"));
  563. }, jQuery.format("Please enter a value with a valid extension."));