PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/ajax/libs/validator/3.5.0/validator.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 373 lines | 299 code | 50 blank | 24 comment | 70 complexity | b9bf2414eec8315a6b181edbac8e4209 MD5 | raw file
  1. /*!
  2. * Copyright (c) 2014 Chris O'Hara <cohara87@gmail.com>
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. (function (name, definition) {
  24. if (typeof module !== 'undefined') {
  25. module.exports = definition();
  26. } else if (typeof define === 'function' && typeof define.amd === 'object') {
  27. define(definition);
  28. } else {
  29. this[name] = definition();
  30. }
  31. })('validator', function (validator) {
  32. 'use strict';
  33. validator = { version: '3.4.0' };
  34. var email = /^((([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;
  35. var creditCard = /^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$/;
  36. var isbn10Maybe = /^(?:[0-9]{9}X|[0-9]{10})$/
  37. , isbn13Maybe = /^(?:[0-9]{13})$/;
  38. var ipv4Maybe = /^(\d?\d?\d)\.(\d?\d?\d)\.(\d?\d?\d)\.(\d?\d?\d)$/
  39. , ipv6 = /^::|^::1|^([a-fA-F0-9]{1,4}::?){1,7}([a-fA-F0-9]{1,4})$/;
  40. var uuid = {
  41. '3': /^[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i
  42. , '4': /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
  43. , '5': /^[0-9A-F]{8}-[0-9A-F]{4}-5[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
  44. , all: /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i
  45. };
  46. var alpha = /^[a-zA-Z]+$/
  47. , alphanumeric = /^[a-zA-Z0-9]+$/
  48. , numeric = /^-?[0-9]+$/
  49. , int = /^(?:-?(?:0|[1-9][0-9]*))$/
  50. , float = /^(?:-?(?:[0-9]+))?(?:\.[0-9]*)?(?:[eE][\+\-]?(?:[0-9]+))?$/
  51. , hexadecimal = /^[0-9a-fA-F]+$/
  52. , hexcolor = /^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/;
  53. validator.extend = function (name, fn) {
  54. validator[name] = function () {
  55. var args = Array.prototype.slice.call(arguments);
  56. args[0] = validator.toString(args[0]);
  57. return fn.apply(validator, args);
  58. };
  59. };
  60. validator.noCoerce = ['toString', 'toDate', 'extend', 'init', 'flatten', 'merge'];
  61. //Right before exporting the validator object, pass each of the builtins
  62. //through extend() so that their first argument is coerced to a string
  63. validator.init = function () {
  64. for (var name in validator) {
  65. if (typeof validator[name] !== 'function' || validator.noCoerce.indexOf(name) >= 0) {
  66. continue;
  67. }
  68. validator.extend(name, validator[name]);
  69. }
  70. };
  71. validator.toString = function (input) {
  72. if (input === null || typeof input === 'undefined' || (isNaN(input) && !input.length)) {
  73. input = '';
  74. } else if (typeof input === 'object' && input.toString) {
  75. input = input.toString();
  76. } else if (typeof input !== 'string') {
  77. input += '';
  78. }
  79. return input;
  80. };
  81. validator.toDate = function (date) {
  82. if (Object.prototype.toString.call(date) === '[object Date]') {
  83. return date;
  84. }
  85. date = Date.parse(date);
  86. return !isNaN(date) ? new Date(date) : null;
  87. };
  88. validator.toFloat = function (str) {
  89. return parseFloat(str);
  90. };
  91. validator.toInt = function (str, radix) {
  92. return parseInt(str, radix || 10);
  93. };
  94. validator.toBoolean = function (str, strict) {
  95. if (strict) {
  96. return str === '1' || str === 'true';
  97. }
  98. return str !== '0' && str !== 'false' && str !== '';
  99. };
  100. validator.flatten = function (array, separator) {
  101. if (!array) {
  102. return '';
  103. }
  104. var str = array[0];
  105. for (var i = 1; i < array.length; i++) {
  106. str += separator + array[i];
  107. }
  108. return str;
  109. };
  110. validator.merge = function (obj, defaults) {
  111. obj = obj || {};
  112. for (var key in defaults) {
  113. if (typeof obj[key] === 'undefined') {
  114. obj[key] = defaults[key];
  115. }
  116. }
  117. return obj;
  118. };
  119. validator.equals = function (str, comparison) {
  120. return str === validator.toString(comparison);
  121. };
  122. validator.contains = function (str, elem) {
  123. return str.indexOf(validator.toString(elem)) >= 0;
  124. };
  125. validator.matches = function (str, pattern, modifiers) {
  126. if (Object.prototype.toString.call(pattern) !== '[object RegExp]') {
  127. pattern = new RegExp(pattern, modifiers);
  128. }
  129. return pattern.test(str);
  130. };
  131. validator.isEmail = function (str) {
  132. return email.test(str);
  133. };
  134. var default_url_options = {
  135. protocols: [ 'http', 'https', 'ftp' ]
  136. , require_tld: true
  137. , require_protocol: false
  138. };
  139. validator.isURL = function (str, options) {
  140. options = validator.merge(options, default_url_options);
  141. var url = new RegExp('^(?!mailto:)(?:(?:' + validator.flatten(options.protocols, '|') + ')://)' + (options.require_protocol ? '' : '?') + '(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))' + (options.require_tld ? '' : '?') + ')|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$', 'i');
  142. return str.length < 2083 && url.test(str);
  143. };
  144. validator.isIP = function (str, version) {
  145. version = validator.toString(version);
  146. if (!version) {
  147. return validator.isIP(str, 4) || validator.isIP(str, 6);
  148. } else if (version === '4') {
  149. if (!ipv4Maybe.test(str)) {
  150. return false;
  151. }
  152. var parts = str.split('.').sort();
  153. return parts[3] <= 255;
  154. }
  155. return version === '6' && ipv6.test(str);
  156. };
  157. validator.isAlpha = function (str) {
  158. return alpha.test(str);
  159. };
  160. validator.isAlphanumeric = function (str) {
  161. return alphanumeric.test(str);
  162. };
  163. validator.isNumeric = function (str) {
  164. return numeric.test(str);
  165. };
  166. validator.isHexadecimal = function (str) {
  167. return hexadecimal.test(str);
  168. };
  169. validator.isHexColor = function (str) {
  170. return hexcolor.test(str);
  171. };
  172. validator.isLowercase = function (str) {
  173. return str === str.toLowerCase();
  174. };
  175. validator.isUppercase = function (str) {
  176. return str === str.toUpperCase();
  177. };
  178. validator.isInt = function (str) {
  179. return int.test(str);
  180. };
  181. validator.isFloat = function (str) {
  182. return str !== '' && float.test(str);
  183. };
  184. validator.isDivisibleBy = function (str, num) {
  185. return validator.toFloat(str) % validator.toInt(num) === 0;
  186. };
  187. validator.isNull = function (str) {
  188. return str.length === 0;
  189. };
  190. validator.isLength = function (str, min, max) {
  191. return str.length >= min && (typeof max === 'undefined' || str.length <= max);
  192. };
  193. validator.isUUID = function (str, version) {
  194. var pattern = uuid[version ? version : 'all'];
  195. return pattern && pattern.test(str);
  196. };
  197. validator.isDate = function (str) {
  198. return !isNaN(Date.parse(str));
  199. };
  200. validator.isAfter = function (str, date) {
  201. var comparison = validator.toDate(date || new Date())
  202. , original = validator.toDate(str);
  203. return original && comparison && original > comparison;
  204. };
  205. validator.isBefore = function (str, date) {
  206. var comparison = validator.toDate(date || new Date())
  207. , original = validator.toDate(str);
  208. return original && comparison && original < comparison;
  209. };
  210. validator.isIn = function (str, options) {
  211. if (!options || typeof options.indexOf !== 'function') {
  212. return false;
  213. }
  214. if (Object.prototype.toString.call(options) === '[object Array]') {
  215. var array = [];
  216. for (var i = 0, len = options.length; i < len; i++) {
  217. array[i] = validator.toString(options[i]);
  218. }
  219. options = array;
  220. }
  221. return options.indexOf(str) >= 0;
  222. };
  223. validator.isCreditCard = function (str) {
  224. var sanitized = str.replace(/[^0-9]+/g, '');
  225. if (!creditCard.test(sanitized)) {
  226. return false;
  227. }
  228. var sum = 0, digit, tmpNum, shouldDouble;
  229. for (var i = sanitized.length - 1; i >= 0; i--) {
  230. digit = sanitized.substring(i, (i + 1));
  231. tmpNum = parseInt(digit, 10);
  232. if (shouldDouble) {
  233. tmpNum *= 2;
  234. if (tmpNum >= 10) {
  235. sum += ((tmpNum % 10) + 1);
  236. } else {
  237. sum += tmpNum;
  238. }
  239. } else {
  240. sum += tmpNum;
  241. }
  242. shouldDouble = !shouldDouble;
  243. }
  244. return (sum % 10) === 0 ? sanitized : false;
  245. };
  246. validator.isISBN = function (str, version) {
  247. version = validator.toString(version);
  248. if (!version) {
  249. return validator.isISBN(str, 10) || validator.isISBN(str, 13);
  250. }
  251. var sanitized = str.replace(/[\s-]+/g, '')
  252. , checksum = 0, i;
  253. if (version === '10') {
  254. if (!isbn10Maybe.test(sanitized)) {
  255. return false;
  256. }
  257. for (i = 0; i < 9; i++) {
  258. checksum += (i + 1) * sanitized.charAt(i);
  259. }
  260. if (sanitized.charAt(9) === 'X') {
  261. checksum += 10 * 10;
  262. } else {
  263. checksum += 10 * sanitized.charAt(9);
  264. }
  265. if ((checksum % 11) === 0) {
  266. return sanitized;
  267. }
  268. } else if (version === '13') {
  269. if (!isbn13Maybe.test(sanitized)) {
  270. return false;
  271. }
  272. var factor = [ 1, 3 ];
  273. for (i = 0; i < 12; i++) {
  274. checksum += factor[i % 2] * sanitized.charAt(i);
  275. }
  276. if (sanitized.charAt(12) - ((10 - (checksum % 10)) % 10) === 0) {
  277. return sanitized;
  278. }
  279. }
  280. return false;
  281. };
  282. validator.isJSON = function (str) {
  283. try {
  284. JSON.parse(str);
  285. } catch (e) {
  286. if (e instanceof SyntaxError) {
  287. return false;
  288. }
  289. }
  290. return true;
  291. };
  292. validator.ltrim = function (str, chars) {
  293. var pattern = chars ? new RegExp('^[' + chars + ']+', 'g') : /^\s+/g;
  294. return str.replace(pattern, '');
  295. };
  296. validator.rtrim = function (str, chars) {
  297. var pattern = chars ? new RegExp('[' + chars + ']+$', 'g') : /\s+$/g;
  298. return str.replace(pattern, '');
  299. };
  300. validator.trim = function (str, chars) {
  301. var pattern = chars ? new RegExp('^[' + chars + ']+|[' + chars + ']+$', 'g') : /^\s+|\s+$/g;
  302. return str.replace(pattern, '');
  303. };
  304. validator.escape = function (str) {
  305. return (str.replace(/&/g, '&amp;')
  306. .replace(/"/g, '&quot;')
  307. .replace(/</g, '&lt;')
  308. .replace(/>/g, '&gt;'));
  309. };
  310. validator.whitelist = function (str, chars) {
  311. return str.replace(new RegExp('[^' + chars + ']+', 'g'), '');
  312. };
  313. validator.blacklist = function (str, chars) {
  314. return str.replace(new RegExp('[' + chars + ']+', 'g'), '');
  315. };
  316. validator.init();
  317. return validator;
  318. });