PageRenderTime 47ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/jquery-validate/additional-methods.js

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