PageRenderTime 35ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs/validator/3.0.0/validator.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 282 lines | 219 code | 41 blank | 22 comment | 47 complexity | ff49abd713a647e132573dbcc2fd0ac6 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. validator = { version: '3.0.0' };
  33. var email = /^(?:[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+\.)*[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+@(?:(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!\.)){0,61}[a-zA-Z0-9]?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!$)){0,61}[a-zA-Z0-9]?)|(?:\[(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\]))$/;
  34. var url = /^(?!mailto:)(?:(?:https?|ftp):\/\/)?(?:\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,})))|localhost)(?::\d{2,5})?(?:\/[^\s]*)?$/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 ipv4Maybe = /^(\d?\d?\d)\.(\d?\d?\d)\.(\d?\d?\d)\.(\d?\d?\d)$/
  37. , ipv6 = /^::|^::1|^([a-fA-F0-9]{1,4}::?){1,7}([a-fA-F0-9]{1,4})$/;
  38. var uuid = {
  39. '3': /^[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i
  40. , '4': /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
  41. , '5': /^[0-9A-F]{8}-[0-9A-F]{4}-5[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
  42. , all: /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i
  43. };
  44. var alpha = /^[a-zA-Z]+$/
  45. , alphanumeric = /^[a-zA-Z0-9]+$/
  46. , numeric = /^-?[0-9]+$/
  47. , int = /^(?:-?(?:0|[1-9][0-9]*))$/
  48. , float = /^(?:-?(?:[0-9]+))?(?:\.[0-9]*)?(?:[eE][\+\-]?(?:[0-9]+))?$/
  49. , hexadecimal = /^[0-9a-fA-F]+$/
  50. , hexcolor = /^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/;
  51. validator.toString = function (input) {
  52. if (input === null || typeof input === 'undefined' || (isNaN(input) && !input.length)) {
  53. input = '';
  54. } else if (typeof input === 'object' && input !== null && input.toString) {
  55. input = input.toString();
  56. }
  57. return input + '';
  58. };
  59. validator.toDate = function (date) {
  60. if (Object.prototype.toString.call(date) === '[object Date]') {
  61. return date;
  62. }
  63. date = Date.parse(date);
  64. return !isNaN(date) ? new Date(date) : null;
  65. };
  66. validator.toFloat = function (str) {
  67. return parseFloat(str);
  68. };
  69. validator.toInt = function (str, radix) {
  70. return parseInt(str, radix || 10);
  71. };
  72. validator.toBoolean = function (str, strict) {
  73. if (strict) {
  74. return str === '1' || str === 'true';
  75. }
  76. return str !== '0' && str !== 'false' && str !== '';
  77. };
  78. validator.equals = function (str, comparison) {
  79. return str === validator.toString(comparison);
  80. };
  81. validator.contains = function (str, elem) {
  82. return str.indexOf(validator.toString(elem)) >= 0;
  83. };
  84. validator.matches = function (str, pattern, modifiers) {
  85. if (Object.prototype.toString.call(pattern) !== '[object RegExp]') {
  86. pattern = new RegExp(pattern, modifiers);
  87. }
  88. return pattern.test(str);
  89. };
  90. validator.isEmail = function (str) {
  91. return email.test(str);
  92. };
  93. validator.isURL = function (str) {
  94. return str.length < 2083 && url.test(str);
  95. };
  96. validator.isIP = function (str, version) {
  97. version = validator.toString(version);
  98. if (!version) {
  99. return validator.isIP(str, 4) || validator.isIP(str, 6);
  100. } else if (version === '4') {
  101. if (!ipv4Maybe.test(str)) {
  102. return false;
  103. }
  104. var parts = str.split('.').sort();
  105. return parts[3] <= 255;
  106. }
  107. return version === '6' && ipv6.test(str);
  108. };
  109. validator.isAlpha = function (str) {
  110. return alpha.test(str);
  111. };
  112. validator.isAlphanumeric = function (str) {
  113. return alphanumeric.test(str);
  114. };
  115. validator.isNumeric = function (str) {
  116. return numeric.test(str);
  117. };
  118. validator.isHexadecimal = function (str) {
  119. return hexadecimal.test(str);
  120. };
  121. validator.isHexColor = function (str) {
  122. return hexcolor.test(str);
  123. };
  124. validator.isLowercase = function (str) {
  125. return str === str.toLowerCase();
  126. };
  127. validator.isUppercase = function (str) {
  128. return str === str.toUpperCase();
  129. };
  130. validator.isInt = function (str) {
  131. return int.test(str);
  132. };
  133. validator.isFloat = function (str) {
  134. return str !== '' && float.test(str);
  135. };
  136. validator.isDivisibleBy = function (str, num) {
  137. return validator.toFloat(str) % validator.toInt(num) === 0;
  138. };
  139. validator.isNull = function (str) {
  140. return str.length === 0;
  141. };
  142. validator.isLength = function (str, min, max) {
  143. return str.length >= min && (typeof max === 'undefined' || str.length <= max);
  144. };
  145. validator.isUUID = function (str, version) {
  146. var pattern = uuid[version ? version : 'all'];
  147. return pattern && pattern.test(str);
  148. };
  149. validator.isDate = function (str) {
  150. return !isNaN(Date.parse(str));
  151. };
  152. validator.isAfter = function (str, date) {
  153. var comparison = validator.toDate(date || new Date())
  154. , original = validator.toDate(str);
  155. return original && comparison && original > comparison;
  156. };
  157. validator.isBefore = function (str, date) {
  158. var comparison = validator.toDate(date || new Date())
  159. , original = validator.toDate(str);
  160. return original && comparison && original < comparison;
  161. };
  162. validator.isIn = function (str, options) {
  163. if (!options || typeof options.indexOf !== 'function') {
  164. return false;
  165. }
  166. if (Object.prototype.toString.call(options) === '[object Array]') {
  167. var array = [];
  168. for (var i = 0, len = options.length; i < len; i++) {
  169. array[i] = validator.toString(options[i]);
  170. }
  171. options = array;
  172. }
  173. return options.indexOf(str) >= 0;
  174. };
  175. validator.isCreditCard = function (str) {
  176. var sanitized = str.replace(/[^0-9]+/g, '');
  177. if (!creditCard.test(sanitized)) {
  178. return false;
  179. }
  180. var sum = 0, digit, tmpNum, shouldDouble;
  181. for (var i = sanitized.length - 1; i >= 0; i--) {
  182. digit = sanitized.substring(i, (i + 1));
  183. tmpNum = parseInt(digit, 10);
  184. if (shouldDouble) {
  185. tmpNum *= 2;
  186. if (tmpNum >= 10) {
  187. sum += ((tmpNum % 10) + 1);
  188. } else {
  189. sum += tmpNum;
  190. }
  191. } else {
  192. sum += tmpNum;
  193. }
  194. shouldDouble = !shouldDouble;
  195. }
  196. return (sum % 10) === 0 ? sanitized : false;
  197. };
  198. validator.ltrim = function (str, chars) {
  199. var pattern = chars ? new RegExp('^[' + chars + ']+', 'g') : /^\s+/g;
  200. return str.replace(pattern, '');
  201. };
  202. validator.rtrim = function (str, chars) {
  203. var pattern = chars ? new RegExp('[' + chars + ']+$', 'g') : /\s+$/g;
  204. return str.replace(pattern, '');
  205. };
  206. validator.trim = function (str, chars) {
  207. var pattern = chars ? new RegExp('^[' + chars + ']+|[' + chars + ']+$', 'g') : /^\s+|\s+$/g;
  208. return str.replace(pattern, '');
  209. };
  210. validator.escape = function (str) {
  211. return (str.replace(/&/g, '&amp;')
  212. .replace(/"/g, '&quot;')
  213. .replace(/</g, '&lt;')
  214. .replace(/>/g, '&gt;'));
  215. };
  216. validator.whitelist = function (str, chars) {
  217. return str.replace(new RegExp('[^' + chars + ']+', 'g'), '');
  218. };
  219. validator.blacklist = function (str, chars) {
  220. return str.replace(new RegExp('[' + chars + ']+', 'g'), '');
  221. };
  222. for (var name in validator) {
  223. if (name === 'toString' || name === 'toDate' || typeof validator[name] !== 'function') {
  224. continue;
  225. }
  226. (function (name) {
  227. var original = validator[name];
  228. validator[name] = function () {
  229. var args = Array.prototype.slice.call(arguments);
  230. args[0] = validator.toString(args[0]);
  231. return original.apply(validator, args);
  232. };
  233. })(name);
  234. }
  235. return validator;
  236. });