PageRenderTime 58ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/files//1.5.1/validate.js

https://gitlab.com/Mirros/jsdelivr
JavaScript | 609 lines | 386 code | 118 blank | 105 comment | 96 complexity | 36d9e29cbdab7c21cb6dadcdfc4435a2 MD5 | raw file
  1. /*
  2. * validate.js 1.4.1
  3. * Copyright (c) 2011 - 2014 Rick Harrison, http://rickharrison.me
  4. * validate.js is open sourced under the MIT license.
  5. * Portions of validate.js are inspired by CodeIgniter.
  6. * http://rickharrison.github.com/validate.js
  7. */
  8. (function(window, document, undefined) {
  9. /*
  10. * If you would like an application-wide config, change these defaults.
  11. * Otherwise, use the setMessage() function to configure form specific messages.
  12. */
  13. var defaults = {
  14. messages: {
  15. required: 'The %s field is required.',
  16. matches: 'The %s field does not match the %s field.',
  17. "default": 'The %s field is still set to default, please change.',
  18. valid_email: 'The %s field must contain a valid email address.',
  19. valid_emails: 'The %s field must contain all valid email addresses.',
  20. min_length: 'The %s field must be at least %s characters in length.',
  21. max_length: 'The %s field must not exceed %s characters in length.',
  22. exact_length: 'The %s field must be exactly %s characters in length.',
  23. greater_than: 'The %s field must contain a number greater than %s.',
  24. less_than: 'The %s field must contain a number less than %s.',
  25. alpha: 'The %s field must only contain alphabetical characters.',
  26. alpha_numeric: 'The %s field must only contain alpha-numeric characters.',
  27. alpha_dash: 'The %s field must only contain alpha-numeric characters, underscores, and dashes.',
  28. numeric: 'The %s field must contain only numbers.',
  29. integer: 'The %s field must contain an integer.',
  30. decimal: 'The %s field must contain a decimal number.',
  31. is_natural: 'The %s field must contain only positive numbers.',
  32. is_natural_no_zero: 'The %s field must contain a number greater than zero.',
  33. valid_ip: 'The %s field must contain a valid IP.',
  34. valid_base64: 'The %s field must contain a base64 string.',
  35. valid_credit_card: 'The %s field must contain a valid credit card number.',
  36. is_file_type: 'The %s field must contain only %s files.',
  37. valid_url: 'The %s field must contain a valid URL.',
  38. greater_than_date: 'The %s field must contain a more recent date than %s.',
  39. less_than_date: 'The %s field must contain an older date than %s.',
  40. greater_than_or_equal_date: 'The %s field must contain a date that\'s at least as recent as %s.',
  41. less_than_or_equal_date: 'The %s field must contain a date that\'s %s or older.'
  42. },
  43. callback: function(errors) {
  44. }
  45. };
  46. /*
  47. * Define the regular expressions that will be used
  48. */
  49. var ruleRegex = /^(.+?)\[(.+)\]$/,
  50. numericRegex = /^[0-9]+$/,
  51. integerRegex = /^\-?[0-9]+$/,
  52. decimalRegex = /^\-?[0-9]*\.?[0-9]+$/,
  53. emailRegex = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[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])?)*$/,
  54. alphaRegex = /^[a-z]+$/i,
  55. alphaNumericRegex = /^[a-z0-9]+$/i,
  56. alphaDashRegex = /^[a-z0-9_\-]+$/i,
  57. naturalRegex = /^[0-9]+$/i,
  58. naturalNoZeroRegex = /^[1-9][0-9]*$/i,
  59. ipRegex = /^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})$/i,
  60. base64Regex = /[^a-zA-Z0-9\/\+=]/i,
  61. numericDashRegex = /^[\d\-\s]+$/,
  62. urlRegex = /^((http|https):\/\/(\w+:{0,1}\w*@)?(\S+)|)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$/,
  63. dateRegex = /\d{4}-\d{1,2}-\d{1,2}/;
  64. /*
  65. * The exposed public object to validate a form:
  66. *
  67. * @param formNameOrNode - String - The name attribute of the form (i.e. <form name="myForm"></form>) or node of the form element
  68. * @param fields - Array - [{
  69. * name: The name of the element (i.e. <input name="myField" />)
  70. * display: 'Field Name'
  71. * rules: required|matches[password_confirm]
  72. * }]
  73. * @param callback - Function - The callback after validation has been performed.
  74. * @argument errors - An array of validation errors
  75. * @argument event - The javascript event
  76. */
  77. var FormValidator = function(formNameOrNode, fields, callback) {
  78. this.callback = callback || defaults.callback;
  79. this.errors = [];
  80. this.fields = {};
  81. this.form = this._formByNameOrNode(formNameOrNode) || {};
  82. this.messages = {};
  83. this.handlers = {};
  84. this.conditionals = {};
  85. for (var i = 0, fieldLength = fields.length; i < fieldLength; i++) {
  86. var field = fields[i];
  87. // If passed in incorrectly, we need to skip the field.
  88. if ((!field.name && !field.names) || !field.rules) {
  89. continue;
  90. }
  91. /*
  92. * Build the master fields array that has all the information needed to validate
  93. */
  94. if (field.names) {
  95. for (var j = 0, fieldNamesLength = field.names.length; j < fieldNamesLength; j++) {
  96. this._addField(field, field.names[j]);
  97. }
  98. } else {
  99. this._addField(field, field.name);
  100. }
  101. }
  102. /*
  103. * Attach an event callback for the form submission
  104. */
  105. var _onsubmit = this.form.onsubmit;
  106. this.form.onsubmit = (function(that) {
  107. return function(evt) {
  108. try {
  109. return that._validateForm(evt) && (_onsubmit === undefined || _onsubmit());
  110. } catch(e) {}
  111. };
  112. })(this);
  113. },
  114. attributeValue = function (element, attributeName) {
  115. var i;
  116. if ((element.length > 0) && (element[0].type === 'radio' || element[0].type === 'checkbox')) {
  117. for (i = 0, elementLength = element.length; i < elementLength; i++) {
  118. if (element[i].checked) {
  119. return element[i][attributeName];
  120. }
  121. }
  122. return;
  123. }
  124. return element[attributeName];
  125. };
  126. /*
  127. * @public
  128. * Sets a custom message for one of the rules
  129. */
  130. FormValidator.prototype.setMessage = function(rule, message) {
  131. this.messages[rule] = message;
  132. // return this for chaining
  133. return this;
  134. };
  135. /*
  136. * @public
  137. * Registers a callback for a custom rule (i.e. callback_username_check)
  138. */
  139. FormValidator.prototype.registerCallback = function(name, handler) {
  140. if (name && typeof name === 'string' && handler && typeof handler === 'function') {
  141. this.handlers[name] = handler;
  142. }
  143. // return this for chaining
  144. return this;
  145. };
  146. /*
  147. * @public
  148. * Registers a conditional for a custom 'depends' rule
  149. */
  150. FormValidator.prototype.registerConditional = function(name, conditional) {
  151. if (name && typeof name === 'string' && conditional && typeof conditional === 'function') {
  152. this.conditionals[name] = conditional;
  153. }
  154. // return this for chaining
  155. return this;
  156. };
  157. /*
  158. * @private
  159. * Determines if a form dom node was passed in or just a string representing the form name
  160. */
  161. FormValidator.prototype._formByNameOrNode = function(formNameOrNode) {
  162. return (typeof formNameOrNode === 'object') ? formNameOrNode : document.forms[formNameOrNode];
  163. };
  164. /*
  165. * @private
  166. * Adds a file to the master fields array
  167. */
  168. FormValidator.prototype._addField = function(field, nameValue) {
  169. this.fields[nameValue] = {
  170. name: nameValue,
  171. display: field.display || nameValue,
  172. rules: field.rules,
  173. depends: field.depends,
  174. id: null,
  175. element: null,
  176. type: null,
  177. value: null,
  178. checked: null
  179. };
  180. };
  181. /*
  182. * @private
  183. * Runs the validation when the form is submitted.
  184. */
  185. FormValidator.prototype._validateForm = function(evt) {
  186. this.errors = [];
  187. for (var key in this.fields) {
  188. if (this.fields.hasOwnProperty(key)) {
  189. var field = this.fields[key] || {},
  190. element = this.form[field.name];
  191. if (element && element !== undefined) {
  192. field.id = attributeValue(element, 'id');
  193. field.element = element;
  194. field.type = (element.length > 0) ? element[0].type : element.type;
  195. field.value = attributeValue(element, 'value');
  196. field.checked = attributeValue(element, 'checked');
  197. /*
  198. * Run through the rules for each field.
  199. * If the field has a depends conditional, only validate the field
  200. * if it passes the custom function
  201. */
  202. if (field.depends && typeof field.depends === "function") {
  203. if (field.depends.call(this, field)) {
  204. this._validateField(field);
  205. }
  206. } else if (field.depends && typeof field.depends === "string" && this.conditionals[field.depends]) {
  207. if (this.conditionals[field.depends].call(this,field)) {
  208. this._validateField(field);
  209. }
  210. } else {
  211. this._validateField(field);
  212. }
  213. }
  214. }
  215. }
  216. if (typeof this.callback === 'function') {
  217. this.callback(this.errors, evt);
  218. }
  219. if (this.errors.length > 0) {
  220. if (evt && evt.preventDefault) {
  221. evt.preventDefault();
  222. } else if (event) {
  223. // IE uses the global event variable
  224. event.returnValue = false;
  225. }
  226. }
  227. return true;
  228. };
  229. /*
  230. * @private
  231. * Looks at the fields value and evaluates it against the given rules
  232. */
  233. FormValidator.prototype._validateField = function(field) {
  234. var rules = field.rules.split('|'),
  235. indexOfRequired = field.rules.indexOf('required'),
  236. isEmpty = (!field.value || field.value === '' || field.value === undefined);
  237. /*
  238. * Run through the rules and execute the validation methods as needed
  239. */
  240. for (var i = 0, ruleLength = rules.length; i < ruleLength; i++) {
  241. var method = rules[i],
  242. param = null,
  243. failed = false,
  244. parts = ruleRegex.exec(method);
  245. /*
  246. * If this field is not required and the value is empty, continue on to the next rule unless it's a callback.
  247. * This ensures that a callback will always be called but other rules will be skipped.
  248. */
  249. if (indexOfRequired === -1 && method.indexOf('!callback_') === -1 && isEmpty) {
  250. continue;
  251. }
  252. /*
  253. * If the rule has a parameter (i.e. matches[param]) split it out
  254. */
  255. if (parts) {
  256. method = parts[1];
  257. param = parts[2];
  258. }
  259. if (method.charAt(0) === '!') {
  260. method = method.substring(1, method.length);
  261. }
  262. /*
  263. * If the hook is defined, run it to find any validation errors
  264. */
  265. if (typeof this._hooks[method] === 'function') {
  266. if (!this._hooks[method].apply(this, [field, param])) {
  267. failed = true;
  268. }
  269. } else if (method.substring(0, 9) === 'callback_') {
  270. // Custom method. Execute the handler if it was registered
  271. method = method.substring(9, method.length);
  272. if (typeof this.handlers[method] === 'function') {
  273. if (this.handlers[method].apply(this, [field.value, param, field]) === false) {
  274. failed = true;
  275. }
  276. }
  277. }
  278. /*
  279. * If the hook failed, add a message to the errors array
  280. */
  281. if (failed) {
  282. // Make sure we have a message for this rule
  283. var source = this.messages[field.name + '.' + method] || this.messages[method] || defaults.messages[method],
  284. message = 'An error has occurred with the ' + field.display + ' field.';
  285. if (source) {
  286. message = source.replace('%s', field.display);
  287. if (param) {
  288. message = message.replace('%s', (this.fields[param]) ? this.fields[param].display : param);
  289. }
  290. }
  291. this.errors.push({
  292. id: field.id,
  293. element: field.element,
  294. name: field.name,
  295. message: message,
  296. rule: method
  297. });
  298. // Break out so as to not spam with validation errors (i.e. required and valid_email)
  299. break;
  300. }
  301. }
  302. };
  303. /**
  304. * private function _getValidDate: helper function to convert a string date to a Date object
  305. * @param date (String) must be in format yyyy-mm-dd or use keyword: today
  306. * @returns {Date} returns false if invalid
  307. */
  308. FormValidator.prototype._getValidDate = function(date) {
  309. if (!date.match('today') && !date.match(dateRegex)) {
  310. return false;
  311. }
  312. var validDate = new Date(),
  313. validDateArray;
  314. if (!date.match('today')) {
  315. validDateArray = date.split('-');
  316. validDate.setFullYear(validDateArray[0]);
  317. validDate.setMonth(validDateArray[1] - 1);
  318. validDate.setDate(validDateArray[2]);
  319. }
  320. return validDate;
  321. };
  322. /*
  323. * @private
  324. * Object containing all of the validation hooks
  325. */
  326. FormValidator.prototype._hooks = {
  327. required: function(field) {
  328. var value = field.value;
  329. if ((field.type === 'checkbox') || (field.type === 'radio')) {
  330. return (field.checked === true);
  331. }
  332. return (value !== null && value !== '');
  333. },
  334. "default": function(field, defaultName){
  335. return field.value !== defaultName;
  336. },
  337. matches: function(field, matchName) {
  338. var el = this.form[matchName];
  339. if (el) {
  340. return field.value === el.value;
  341. }
  342. return false;
  343. },
  344. valid_email: function(field) {
  345. return emailRegex.test(field.value);
  346. },
  347. valid_emails: function(field) {
  348. var result = field.value.split(/\s*,\s*/g);
  349. for (var i = 0, resultLength = result.length; i < resultLength; i++) {
  350. if (!emailRegex.test(result[i])) {
  351. return false;
  352. }
  353. }
  354. return true;
  355. },
  356. min_length: function(field, length) {
  357. if (!numericRegex.test(length)) {
  358. return false;
  359. }
  360. return (field.value.length >= parseInt(length, 10));
  361. },
  362. max_length: function(field, length) {
  363. if (!numericRegex.test(length)) {
  364. return false;
  365. }
  366. return (field.value.length <= parseInt(length, 10));
  367. },
  368. exact_length: function(field, length) {
  369. if (!numericRegex.test(length)) {
  370. return false;
  371. }
  372. return (field.value.length === parseInt(length, 10));
  373. },
  374. greater_than: function(field, param) {
  375. if (!decimalRegex.test(field.value)) {
  376. return false;
  377. }
  378. return (parseFloat(field.value) > parseFloat(param));
  379. },
  380. less_than: function(field, param) {
  381. if (!decimalRegex.test(field.value)) {
  382. return false;
  383. }
  384. return (parseFloat(field.value) < parseFloat(param));
  385. },
  386. alpha: function(field) {
  387. return (alphaRegex.test(field.value));
  388. },
  389. alpha_numeric: function(field) {
  390. return (alphaNumericRegex.test(field.value));
  391. },
  392. alpha_dash: function(field) {
  393. return (alphaDashRegex.test(field.value));
  394. },
  395. numeric: function(field) {
  396. return (numericRegex.test(field.value));
  397. },
  398. integer: function(field) {
  399. return (integerRegex.test(field.value));
  400. },
  401. decimal: function(field) {
  402. return (decimalRegex.test(field.value));
  403. },
  404. is_natural: function(field) {
  405. return (naturalRegex.test(field.value));
  406. },
  407. is_natural_no_zero: function(field) {
  408. return (naturalNoZeroRegex.test(field.value));
  409. },
  410. valid_ip: function(field) {
  411. return (ipRegex.test(field.value));
  412. },
  413. valid_base64: function(field) {
  414. return (base64Regex.test(field.value));
  415. },
  416. valid_url: function(field) {
  417. return (urlRegex.test(field.value));
  418. },
  419. valid_credit_card: function(field){
  420. // Luhn Check Code from https://gist.github.com/4075533
  421. // accept only digits, dashes or spaces
  422. if (!numericDashRegex.test(field.value)) return false;
  423. // The Luhn Algorithm. It's so pretty.
  424. var nCheck = 0, nDigit = 0, bEven = false;
  425. var strippedField = field.value.replace(/\D/g, "");
  426. for (var n = strippedField.length - 1; n >= 0; n--) {
  427. var cDigit = strippedField.charAt(n);
  428. nDigit = parseInt(cDigit, 10);
  429. if (bEven) {
  430. if ((nDigit *= 2) > 9) nDigit -= 9;
  431. }
  432. nCheck += nDigit;
  433. bEven = !bEven;
  434. }
  435. return (nCheck % 10) === 0;
  436. },
  437. is_file_type: function(field,type) {
  438. if (field.type !== 'file') {
  439. return true;
  440. }
  441. var ext = field.value.substr((field.value.lastIndexOf('.') + 1)),
  442. typeArray = type.split(','),
  443. inArray = false,
  444. i = 0,
  445. len = typeArray.length;
  446. for (i; i < len; i++) {
  447. if (ext == typeArray[i]) inArray = true;
  448. }
  449. return inArray;
  450. },
  451. greater_than_date: function (field, date) {
  452. var enteredDate = this._getValidDate(field.value),
  453. validDate = this._getValidDate(date);
  454. if (!validDate || !enteredDate) {
  455. return false;
  456. }
  457. return enteredDate > validDate;
  458. },
  459. less_than_date: function (field, date) {
  460. var enteredDate = this._getValidDate(field.value),
  461. validDate = this._getValidDate(date);
  462. if (!validDate || !enteredDate) {
  463. return false;
  464. }
  465. return enteredDate < validDate;
  466. },
  467. greater_than_or_equal_date: function (field, date) {
  468. var enteredDate = this._getValidDate(field.value),
  469. validDate = this._getValidDate(date);
  470. if (!validDate || !enteredDate) {
  471. return false;
  472. }
  473. return enteredDate >= validDate;
  474. },
  475. less_than_or_equal_date: function (field, date) {
  476. var enteredDate = this._getValidDate(field.value),
  477. validDate = this._getValidDate(date);
  478. if (!validDate || !enteredDate) {
  479. return false;
  480. }
  481. return enteredDate <= validDate;
  482. }
  483. };
  484. window.FormValidator = FormValidator;
  485. })(window, document);
  486. /*
  487. * Export as a CommonJS module
  488. */
  489. if (typeof module !== 'undefined' && module.exports) {
  490. module.exports = FormValidator;
  491. }