PageRenderTime 60ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/WebRoot/res/js/validate/additional-methods.js

https://gitlab.com/sumonsun/CronHub
JavaScript | 280 lines | 264 code | 2 blank | 14 comment | 0 complexity | f76e0346df3c48bee7efdf1b3404e5fb MD5 | raw file
  1. /**
  2. * jQuery Validation Plugin 1.8.1
  3. *
  4. * http://bassistance.de/jquery-plugins/jquery-plugin-validation/
  5. * http://docs.jquery.com/Plugins/Validation
  6. *
  7. * Copyright (c) 2006 - 2011 Jörn Zaefferer
  8. *
  9. * Dual licensed under the MIT and GPL licenses:
  10. * http://www.opensource.org/licenses/mit-license.php
  11. * http://www.gnu.org/licenses/gpl.html
  12. */
  13. (function() {
  14. function stripHtml(value) {
  15. // remove html tags and space chars
  16. return value.replace(/<.[^<>]*?>/g, ' ').replace(/&nbsp;|&#160;/gi, ' ')
  17. // remove numbers and punctuation
  18. .replace(/[0-9.(),;:!?%#$'"_+=\/-]*/g,'');
  19. }
  20. jQuery.validator.addMethod("maxWords", function(value, element, params) {
  21. return this.optional(element) || stripHtml(value).match(/\b\w+\b/g).length < params;
  22. }, jQuery.validator.format("Please enter {0} words or less."));
  23. jQuery.validator.addMethod("minWords", function(value, element, params) {
  24. return this.optional(element) || stripHtml(value).match(/\b\w+\b/g).length >= params;
  25. }, jQuery.validator.format("Please enter at least {0} words."));
  26. jQuery.validator.addMethod("rangeWords", function(value, element, params) {
  27. return this.optional(element) || stripHtml(value).match(/\b\w+\b/g).length >= params[0] && value.match(/bw+b/g).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, spaces or 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("integer", function(value, element) {
  46. return this.optional(element) || /^-?\d+$/.test(value);
  47. }, "A positive or negative non-decimal number please");
  48. /**
  49. * Return true, if the value is a valid vehicle identification number (VIN).
  50. *
  51. * Works with all kind of text inputs.
  52. *
  53. * @example <input type="text" size="20" name="VehicleID" class="{required:true,vinUS:true}" />
  54. * @desc Declares a required input element whose value must be a valid vehicle identification number.
  55. *
  56. * @name jQuery.validator.methods.vinUS
  57. * @type Boolean
  58. * @cat Plugins/Validate/Methods
  59. */
  60. jQuery.validator.addMethod(
  61. "vinUS",
  62. function(v){
  63. if (v.length != 17)
  64. return false;
  65. var i, n, d, f, cd, cdv;
  66. 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"];
  67. 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];
  68. var FL = [8,7,6,5,4,3,2,10,0,9,8,7,6,5,4,3,2];
  69. var rs = 0;
  70. for(i = 0; i < 17; i++){
  71. f = FL[i];
  72. d = v.slice(i,i+1);
  73. if(i == 8){
  74. cdv = d;
  75. }
  76. if(!isNaN(d)){
  77. d *= f;
  78. }
  79. else{
  80. for(n = 0; n < LL.length; n++){
  81. if(d.toUpperCase() === LL[n]){
  82. d = VL[n];
  83. d *= f;
  84. if(isNaN(cdv) && n == 8){
  85. cdv = LL[n];
  86. }
  87. break;
  88. }
  89. }
  90. }
  91. rs += d;
  92. }
  93. cd = rs % 11;
  94. if(cd == 10){cd = "X";}
  95. if(cd == cdv){return true;}
  96. return false;
  97. },
  98. "The specified vehicle identification number (VIN) is invalid."
  99. );
  100. /**
  101. * Return true, if the value is a valid date, also making this formal check dd/mm/yyyy.
  102. *
  103. * @example jQuery.validator.methods.date("01/01/1900")
  104. * @result true
  105. *
  106. * @example jQuery.validator.methods.date("01/13/1990")
  107. * @result false
  108. *
  109. * @example jQuery.validator.methods.date("01.01.1900")
  110. * @result false
  111. *
  112. * @example <input name="pippo" class="{dateITA:true}" />
  113. * @desc Declares an optional input element whose value must be a valid date.
  114. *
  115. * @name jQuery.validator.methods.dateITA
  116. * @type Boolean
  117. * @cat Plugins/Validate/Methods
  118. */
  119. jQuery.validator.addMethod(
  120. "dateITA",
  121. function(value, element) {
  122. var check = false;
  123. var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
  124. if( re.test(value)){
  125. var adata = value.split('/');
  126. var gg = parseInt(adata[0],10);
  127. var mm = parseInt(adata[1],10);
  128. var aaaa = parseInt(adata[2],10);
  129. var xdata = new Date(aaaa,mm-1,gg);
  130. if ( ( xdata.getFullYear() == aaaa ) && ( xdata.getMonth () == mm - 1 ) && ( xdata.getDate() == gg ) )
  131. check = true;
  132. else
  133. check = false;
  134. } else
  135. check = false;
  136. return this.optional(element) || check;
  137. },
  138. "Please enter a correct date"
  139. );
  140. jQuery.validator.addMethod("dateNL", function(value, element) {
  141. return this.optional(element) || /^\d\d?[\.\/-]\d\d?[\.\/-]\d\d\d?\d?$/.test(value);
  142. }, "Vul hier een geldige datum in."
  143. );
  144. jQuery.validator.addMethod("time", function(value, element) {
  145. return this.optional(element) || /^([01][0-9])|(2[0123]):([0-5])([0-9])$/.test(value);
  146. }, "Please enter a valid time, between 00:00 and 23:59"
  147. );
  148. /**
  149. * matches US phone number format
  150. *
  151. * where the area code may not start with 1 and the prefix may not start with 1
  152. * allows '-' or ' ' as a separator and allows parens around area code
  153. * some people may want to put a '1' in front of their number
  154. *
  155. * 1(212)-999-2345
  156. * or
  157. * 212 999 2344
  158. * or
  159. * 212-999-0983
  160. *
  161. * but not
  162. * 111-123-5434
  163. * and not
  164. * 212 123 4567
  165. */
  166. jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
  167. phone_number = phone_number.replace(/\s+/g, "");
  168. return this.optional(element) || phone_number.length > 9 &&
  169. phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
  170. }, "Please specify a valid phone number");
  171. jQuery.validator.addMethod('phoneUK', function(phone_number, element) {
  172. return this.optional(element) || phone_number.length > 9 &&
  173. phone_number.match(/^(\(?(0|\+44)[1-9]{1}\d{1,4}?\)?\s?\d{3,4}\s?\d{3,4})$/);
  174. }, 'Please specify a valid phone number');
  175. jQuery.validator.addMethod('mobileUK', function(phone_number, element) {
  176. return this.optional(element) || phone_number.length > 9 &&
  177. phone_number.match(/^((0|\+44)7(5|6|7|8|9){1}\d{2}\s?\d{6})$/);
  178. }, 'Please specify a valid mobile number');
  179. // TODO check if value starts with <, otherwise don't try stripping anything
  180. jQuery.validator.addMethod("strippedminlength", function(value, element, param) {
  181. return jQuery(value).text().length >= param;
  182. }, jQuery.validator.format("Please enter at least {0} characters"));
  183. // same as email, but TLD is optional
  184. jQuery.validator.addMethod("email2", function(value, element, param) {
  185. 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);
  186. }, jQuery.validator.messages.email);
  187. // same as url, but TLD is optional
  188. jQuery.validator.addMethod("url2", function(value, element, param) {
  189. 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);
  190. }, jQuery.validator.messages.url);
  191. // NOTICE: Modified version of Castle.Components.Validator.CreditCardValidator
  192. // Redistributed under the the Apache License 2.0 at http://www.apache.org/licenses/LICENSE-2.0
  193. // Valid Types: mastercard, visa, amex, dinersclub, enroute, discover, jcb, unknown, all (overrides all other settings)
  194. jQuery.validator.addMethod("creditcardtypes", function(value, element, param) {
  195. if (/[^0-9-]+/.test(value))
  196. return false;
  197. value = value.replace(/\D/g, "");
  198. var validTypes = 0x0000;
  199. if (param.mastercard)
  200. validTypes |= 0x0001;
  201. if (param.visa)
  202. validTypes |= 0x0002;
  203. if (param.amex)
  204. validTypes |= 0x0004;
  205. if (param.dinersclub)
  206. validTypes |= 0x0008;
  207. if (param.enroute)
  208. validTypes |= 0x0010;
  209. if (param.discover)
  210. validTypes |= 0x0020;
  211. if (param.jcb)
  212. validTypes |= 0x0040;
  213. if (param.unknown)
  214. validTypes |= 0x0080;
  215. if (param.all)
  216. validTypes = 0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080;
  217. if (validTypes & 0x0001 && /^(51|52|53|54|55)/.test(value)) { //mastercard
  218. return value.length == 16;
  219. }
  220. if (validTypes & 0x0002 && /^(4)/.test(value)) { //visa
  221. return value.length == 16;
  222. }
  223. if (validTypes & 0x0004 && /^(34|37)/.test(value)) { //amex
  224. return value.length == 15;
  225. }
  226. if (validTypes & 0x0008 && /^(300|301|302|303|304|305|36|38)/.test(value)) { //dinersclub
  227. return value.length == 14;
  228. }
  229. if (validTypes & 0x0010 && /^(2014|2149)/.test(value)) { //enroute
  230. return value.length == 15;
  231. }
  232. if (validTypes & 0x0020 && /^(6011)/.test(value)) { //discover
  233. return value.length == 16;
  234. }
  235. if (validTypes & 0x0040 && /^(3)/.test(value)) { //jcb
  236. return value.length == 16;
  237. }
  238. if (validTypes & 0x0040 && /^(2131|1800)/.test(value)) { //jcb
  239. return value.length == 15;
  240. }
  241. if (validTypes & 0x0080) { //unknown
  242. return true;
  243. }
  244. return false;
  245. }, "Please enter a valid credit card number.");
  246. jQuery.validator.addMethod("ipv4", function(value, element, param) {
  247. return this.optional(element) || /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/i.test(value);
  248. }, "Please enter a valid IP v4 address.");
  249. jQuery.validator.addMethod("ipv6", function(value, element, param) {
  250. 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);
  251. }, "Please enter a valid IP v6 address.");