You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1996 lines
73 KiB

  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. define([
  6. 'jquery',
  7. 'moment',
  8. 'mageUtils',
  9. 'jquery-ui-modules/widget',
  10. 'jquery/validate',
  11. 'mage/translate'
  12. ], function ($, moment, utils) {
  13. 'use strict';
  14. var creditCartTypes, rules, showLabel, originValidateDelegate;
  15. $.extend(true, $, {
  16. // @TODO: Move methods 'isEmpty', 'isEmptyNoTrim', 'parseNumber', 'stripHtml' in file with utility functions
  17. mage: {
  18. /**
  19. * Check if string is empty with trim
  20. * @param {String} value
  21. */
  22. isEmpty: function (value) {
  23. return value === '' || value === undefined ||
  24. value == null || value.length === 0 || /^\s+$/.test(value);
  25. },
  26. /**
  27. * Check if string is empty no trim
  28. * @param {String} value
  29. */
  30. isEmptyNoTrim: function (value) {
  31. return value === '' || value == null || value.length === 0;
  32. },
  33. /**
  34. * Checks if {value} is between numbers {from} and {to}
  35. * @param {String} value
  36. * @param {String} from
  37. * @param {String} to
  38. * @returns {Boolean}
  39. */
  40. isBetween: function (value, from, to) {
  41. return ($.mage.isEmpty(from) || value >= $.mage.parseNumber(from)) &&
  42. ($.mage.isEmpty(to) || value <= $.mage.parseNumber(to));
  43. },
  44. /**
  45. * Parse price string
  46. * @param {String} value
  47. */
  48. parseNumber: function (value) {
  49. var isDot, isComa;
  50. if (typeof value !== 'string') {
  51. return parseFloat(value);
  52. }
  53. isDot = value.indexOf('.');
  54. isComa = value.indexOf(',');
  55. if (isDot !== -1 && isComa !== -1) {
  56. if (isComa > isDot) {
  57. value = value.replace('.', '').replace(',', '.');
  58. } else {
  59. value = value.replace(',', '');
  60. }
  61. } else if (isComa !== -1) {
  62. value = value.replace(',', '.');
  63. }
  64. return parseFloat(value);
  65. },
  66. /**
  67. * Removes HTML tags and space characters, numbers and punctuation.
  68. *
  69. * @param {String} value - Value being stripped.
  70. * @return {String}
  71. */
  72. stripHtml: function (value) {
  73. return value.replace(/<.[^<>]*?>/g, ' ').replace(/&nbsp;|&#160;/gi, ' ')
  74. .replace(/[0-9.(),;:!?%#$'"_+=\/-]*/g, '');
  75. }
  76. }
  77. });
  78. /**
  79. * @param {String} name
  80. * @param {*} method
  81. * @param {*} message
  82. * @param {*} dontSkip
  83. */
  84. $.validator.addMethod = function (name, method, message, dontSkip) {
  85. $.validator.methods[name] = method;
  86. $.validator.messages[name] = message !== undefined ? message : $.validator.messages[name];
  87. if (method.length < 3 || dontSkip) {
  88. $.validator.addClassRules(name, $.validator.normalizeRule(name));
  89. }
  90. };
  91. /**
  92. * Javascript object with credit card types
  93. * 0 - regexp for card number
  94. * 1 - regexp for cvn
  95. * 2 - check or not credit card number trough Luhn algorithm by
  96. */
  97. creditCartTypes = {
  98. 'SO': [
  99. new RegExp('^(6334[5-9]([0-9]{11}|[0-9]{13,14}))|(6767([0-9]{12}|[0-9]{14,15}))$'),
  100. new RegExp('^([0-9]{3}|[0-9]{4})?$'),
  101. true
  102. ],
  103. 'SM': [
  104. new RegExp('(^(5[0678])[0-9]{11,18}$)|(^(6[^05])[0-9]{11,18}$)|' +
  105. '(^(601)[^1][0-9]{9,16}$)|(^(6011)[0-9]{9,11}$)|(^(6011)[0-9]{13,16}$)|' +
  106. '(^(65)[0-9]{11,13}$)|(^(65)[0-9]{15,18}$)|(^(49030)[2-9]([0-9]{10}$|[0-9]{12,13}$))|' +
  107. '(^(49033)[5-9]([0-9]{10}$|[0-9]{12,13}$))|(^(49110)[1-2]([0-9]{10}$|[0-9]{12,13}$))|' +
  108. '(^(49117)[4-9]([0-9]{10}$|[0-9]{12,13}$))|(^(49118)[0-2]([0-9]{10}$|[0-9]{12,13}$))|' +
  109. '(^(4936)([0-9]{12}$|[0-9]{14,15}$))'), new RegExp('^([0-9]{3}|[0-9]{4})?$'),
  110. true
  111. ],
  112. 'VI': [new RegExp('^4[0-9]{12}([0-9]{3})?$'), new RegExp('^[0-9]{3}$'), true],
  113. 'MC': [
  114. new RegExp('^(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}$'),
  115. new RegExp('^[0-9]{3}$'),
  116. true
  117. ],
  118. 'AE': [new RegExp('^3[47][0-9]{13}$'), new RegExp('^[0-9]{4}$'), true],
  119. 'DI': [new RegExp('^(6011(0|[2-4]|74|7[7-9]|8[6-9]|9)|6(4[4-9]|5))\\d*$'), new RegExp('^[0-9]{3}$'), true],
  120. 'JCB': [new RegExp('^35(2[8-9]|[3-8])\\d*$'), new RegExp('^[0-9]{3}$'), true],
  121. 'DN': [new RegExp('^(3(0[0-5]|095|6|[8-9]))\\d*$'), new RegExp('^[0-9]{3}$'), true],
  122. 'UN': [
  123. new RegExp('^(622(1(2[6-9]|[3-9])|[3-8]|9([[0-1]|2[0-5]))|62[4-6]|628([2-8]))\\d*?$'),
  124. new RegExp('^[0-9]{3}$'),
  125. true
  126. ],
  127. 'MI': [new RegExp('^(5(0|[6-9])|63|67(?!59|6770|6774))\\d*$'), new RegExp('^[0-9]{3}$'), true],
  128. 'MD': [new RegExp('^6759(?!24|38|40|6[3-9]|70|76)|676770|676774\\d*$'), new RegExp('^[0-9]{3}$'), true]
  129. };
  130. /**
  131. * validate credit card number using mod10
  132. * @param {String} s
  133. * @return {Boolean}
  134. */
  135. function validateCreditCard(s) {
  136. // remove non-numerics
  137. var v = '0123456789',
  138. w = '',
  139. i, j, k, m, c, a, x;
  140. for (i = 0; i < s.length; i++) {
  141. x = s.charAt(i);
  142. if (v.indexOf(x, 0) !== -1) {
  143. w += x;
  144. }
  145. }
  146. // validate number
  147. j = w.length / 2;
  148. k = Math.floor(j);
  149. m = Math.ceil(j) - k;
  150. c = 0;
  151. for (i = 0; i < k; i++) {
  152. a = w.charAt(i * 2 + m) * 2;
  153. c += a > 9 ? Math.floor(a / 10 + a % 10) : a;
  154. }
  155. for (i = 0; i < k + m; i++) {
  156. c += w.charAt(i * 2 + 1 - m) * 1;
  157. }
  158. return c % 10 === 0;
  159. }
  160. /**
  161. * validate all table required inputs at once, using single hidden input
  162. * @param {String} value
  163. * @param {HTMLElement} element
  164. *
  165. * @return {Boolean}
  166. */
  167. function tableSingleValidation(value, element) {
  168. var empty = $(element).closest('table')
  169. .find('input.required-option:visible')
  170. .filter(function (i, el) {
  171. if ($(el).is('disabled')) {
  172. return $.mage.isEmpty(el.value);
  173. }
  174. })
  175. .length;
  176. return empty === 0;
  177. }
  178. /**
  179. *
  180. * @param {float} qty
  181. * @param {float} qtyIncrements
  182. * @returns {float}
  183. */
  184. function resolveModulo(qty, qtyIncrements) {
  185. var divideEpsilon = 10000,
  186. epsilon,
  187. remainder;
  188. while (qtyIncrements < 1) {
  189. qty *= 10;
  190. qtyIncrements *= 10;
  191. }
  192. epsilon = qtyIncrements / divideEpsilon;
  193. remainder = qty % qtyIncrements;
  194. if (Math.abs(remainder - qtyIncrements) < epsilon ||
  195. Math.abs(remainder) < epsilon) {
  196. remainder = 0;
  197. }
  198. return remainder;
  199. }
  200. /**
  201. * Collection of validation rules including rules from additional-methods.js
  202. * @type {Object}
  203. */
  204. rules = {
  205. 'max-words': [
  206. function (value, element, params) {
  207. return this.optional(element) || $.mage.stripHtml(value).match(/\b\w+\b/g).length <= params;
  208. },
  209. $.mage.__('Please enter {0} words or less.')
  210. ],
  211. 'min-words': [
  212. function (value, element, params) {
  213. return this.optional(element) || $.mage.stripHtml(value).match(/\b\w+\b/g).length >= params;
  214. },
  215. $.mage.__('Please enter at least {0} words.')
  216. ],
  217. 'range-words': [
  218. function (value, element, params) {
  219. return this.optional(element) ||
  220. $.mage.stripHtml(value).match(/\b\w+\b/g).length >= params[0] &&
  221. value.match(/bw+b/g).length < params[1];
  222. },
  223. $.mage.__('Please enter between {0} and {1} words.')
  224. ],
  225. 'letters-with-basic-punc': [
  226. function (value, element) {
  227. return this.optional(element) || /^[a-z\-.,()'\"\s]+$/i.test(value);
  228. },
  229. $.mage.__('Letters or punctuation only please')
  230. ],
  231. 'alphanumeric': [
  232. function (value, element) {
  233. return this.optional(element) || /^\w+$/i.test(value);
  234. },
  235. $.mage.__('Letters, numbers, spaces or underscores only please')
  236. ],
  237. 'letters-only': [
  238. function (value, element) {
  239. return this.optional(element) || /^[a-z]+$/i.test(value);
  240. },
  241. $.mage.__('Letters only please')
  242. ],
  243. 'no-whitespace': [
  244. function (value, element) {
  245. return this.optional(element) || /^\S+$/i.test(value);
  246. },
  247. $.mage.__('No white space please')
  248. ],
  249. 'no-marginal-whitespace': [
  250. function (value, element) {
  251. return this.optional(element) || !/^\s+|\s+$/i.test(value);
  252. },
  253. $.mage.__('No marginal white space please')
  254. ],
  255. 'zip-range': [
  256. function (value, element) {
  257. return this.optional(element) || /^90[2-5]-\d{2}-\d{4}$/.test(value);
  258. },
  259. $.mage.__('Your ZIP-code must be in the range 902xx-xxxx to 905-xx-xxxx')
  260. ],
  261. 'integer': [
  262. function (value, element) {
  263. return this.optional(element) || /^-?\d+$/.test(value);
  264. },
  265. $.mage.__('A positive or negative non-decimal number please')
  266. ],
  267. 'vinUS': [
  268. function (v) {
  269. var i, n, d, f, cd, cdv, LL, VL, FL, rs;
  270. /* eslint-disable max-depth */
  271. if (v.length !== 17) {
  272. return false;
  273. }
  274. LL = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L',
  275. 'M', 'N', 'P', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
  276. VL = [1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 7, 9, 2, 3, 4, 5, 6, 7, 8, 9];
  277. FL = [8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2];
  278. rs = 0;
  279. for (i = 0; i < 17; i++) {
  280. f = FL[i];
  281. d = v.slice(i, i + 1);
  282. if (i === 8) {
  283. cdv = d;
  284. }
  285. if (!isNaN(d)) {
  286. d *= f;
  287. } else {
  288. for (n = 0; n < LL.length; n++) {
  289. if (d.toUpperCase() === LL[n]) {
  290. d = VL[n];
  291. d *= f;
  292. if (isNaN(cdv) && n === 8) {
  293. cdv = LL[n];
  294. }
  295. break;
  296. }
  297. }
  298. }
  299. rs += d;
  300. }
  301. /* eslint-enable max-depth */
  302. cd = rs % 11;
  303. if (cd === 10) {
  304. cd = 'X';
  305. }
  306. if (cd === cdv) {
  307. return true;
  308. }
  309. return false;
  310. },
  311. $.mage.__('The specified vehicle identification number (VIN) is invalid.')
  312. ],
  313. 'dateITA': [
  314. function (value, element) {
  315. var check = false,
  316. re = /^\d{1,2}\/\d{1,2}\/\d{4}$/,
  317. adata, gg, mm, aaaa, xdata;
  318. if (re.test(value)) {
  319. adata = value.split('/');
  320. gg = parseInt(adata[0], 10);
  321. mm = parseInt(adata[1], 10);
  322. aaaa = parseInt(adata[2], 10);
  323. xdata = new Date(aaaa, mm - 1, gg);
  324. if (xdata.getFullYear() === aaaa &&
  325. xdata.getMonth() === mm - 1 &&
  326. xdata.getDate() === gg
  327. ) {
  328. check = true;
  329. } else {
  330. check = false;
  331. }
  332. } else {
  333. check = false;
  334. }
  335. return this.optional(element) || check;
  336. },
  337. $.mage.__('Please enter a correct date')
  338. ],
  339. 'dateNL': [
  340. function (value, element) {
  341. return this.optional(element) || /^\d\d?[\.\/-]\d\d?[\.\/-]\d\d\d?\d?$/.test(value);
  342. },
  343. 'Vul hier een geldige datum in.'
  344. ],
  345. 'time': [
  346. function (value, element) {
  347. return this.optional(element) || /^([01]\d|2[0-3])(:[0-5]\d){0,2}$/.test(value);
  348. },
  349. $.mage.__('Please enter a valid time, between 00:00 and 23:59')
  350. ],
  351. 'time12h': [
  352. function (value, element) {
  353. return this.optional(element) || /^((0?[1-9]|1[012])(:[0-5]\d){0,2}(\s[AP]M))$/i.test(value);
  354. },
  355. $.mage.__('Please enter a valid time, between 00:00 am and 12:00 pm')
  356. ],
  357. 'phoneUS': [
  358. function (phoneNumber, element) {
  359. phoneNumber = phoneNumber.replace(/\s+/g, '');
  360. return this.optional(element) || phoneNumber.length > 9 &&
  361. phoneNumber.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
  362. },
  363. $.mage.__('Please specify a valid phone number')
  364. ],
  365. 'phoneUK': [
  366. function (phoneNumber, element) {
  367. return this.optional(element) || phoneNumber.length > 9 &&
  368. phoneNumber.match(/^(\(?(0|\+44)[1-9]{1}\d{1,4}?\)?\s?\d{3,4}\s?\d{3,4})$/);
  369. },
  370. $.mage.__('Please specify a valid phone number')
  371. ],
  372. 'mobileUK': [
  373. function (phoneNumber, element) {
  374. return this.optional(element) || phoneNumber.length > 9 &&
  375. phoneNumber.match(/^((0|\+44)7\d{3}\s?\d{6})$/);
  376. },
  377. $.mage.__('Please specify a valid mobile number')
  378. ],
  379. 'stripped-min-length': [
  380. function (value, element, param) {
  381. return value.length >= param;
  382. },
  383. $.mage.__('Please enter at least {0} characters')
  384. ],
  385. /* detect chars that would require more than 3 bytes */
  386. 'validate-no-utf8mb4-characters': [
  387. function (value) {
  388. var validator = this,
  389. message = $.mage.__('Please remove invalid characters: {0}.'),
  390. matches = value.match(/(?:[\uD800-\uDBFF][\uDC00-\uDFFF])/g),
  391. result = matches === null;
  392. if (!result) {
  393. validator.charErrorMessage = message.replace('{0}', matches.join());
  394. }
  395. return result;
  396. }, function () {
  397. return this.charErrorMessage;
  398. }
  399. ],
  400. /* eslint-disable max-len */
  401. 'email2': [
  402. function (value, element) {
  403. return this.optional(element) ||
  404. /^((([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);
  405. },
  406. $.validator.messages.email
  407. ],
  408. 'url2': [
  409. function (value, element) {
  410. 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);
  411. },
  412. $.validator.messages.url
  413. ],
  414. /* eslint-enable max-len */
  415. 'credit-card-types': [
  416. function (value, element, param) {
  417. var validTypes;
  418. if (/[^0-9-]+/.test(value)) {
  419. return false;
  420. }
  421. value = value.replace(/\D/g, '');
  422. validTypes = 0x0000;
  423. if (param.mastercard) {
  424. validTypes |= 0x0001;
  425. }
  426. if (param.visa) {
  427. validTypes |= 0x0002;
  428. }
  429. if (param.amex) {
  430. validTypes |= 0x0004;
  431. }
  432. if (param.dinersclub) {
  433. validTypes |= 0x0008;
  434. }
  435. if (param.enroute) {
  436. validTypes |= 0x0010;
  437. }
  438. if (param.discover) {
  439. validTypes |= 0x0020;
  440. }
  441. if (param.jcb) {
  442. validTypes |= 0x0040;
  443. }
  444. if (param.unknown) {
  445. validTypes |= 0x0080;
  446. }
  447. if (param.all) {
  448. validTypes = 0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080;
  449. }
  450. if (validTypes & 0x0001 && /^(51|52|53|54|55)/.test(value)) { //mastercard
  451. return value.length === 16;
  452. }
  453. if (validTypes & 0x0002 && /^(4)/.test(value)) { //visa
  454. return value.length === 16;
  455. }
  456. if (validTypes & 0x0004 && /^(34|37)/.test(value)) { //amex
  457. return value.length === 15;
  458. }
  459. if (validTypes & 0x0008 && /^(300|301|302|303|304|305|36|38)/.test(value)) { //dinersclub
  460. return value.length === 14;
  461. }
  462. if (validTypes & 0x0010 && /^(2014|2149)/.test(value)) { //enroute
  463. return value.length === 15;
  464. }
  465. if (validTypes & 0x0020 && /^(6011)/.test(value)) { //discover
  466. return value.length === 16;
  467. }
  468. if (validTypes & 0x0040 && /^(3)/.test(value)) { //jcb
  469. return value.length === 16;
  470. }
  471. if (validTypes & 0x0040 && /^(2131|1800)/.test(value)) { //jcb
  472. return value.length === 15;
  473. }
  474. if (validTypes & 0x0080) { //unknown
  475. return true;
  476. }
  477. return false;
  478. },
  479. $.mage.__('Please enter a valid credit card number.')
  480. ],
  481. /* eslint-disable max-len */
  482. 'ipv4': [
  483. function (value, element) {
  484. return this.optional(element) ||
  485. /^(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);
  486. },
  487. $.mage.__('Please enter a valid IP v4 address.')
  488. ],
  489. 'ipv6': [
  490. function (value, element) {
  491. 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);
  492. },
  493. $.mage.__('Please enter a valid IP v6 address.')
  494. ],
  495. /* eslint-enable max-len */
  496. 'pattern': [
  497. function (value, element, param) {
  498. return this.optional(element) || new RegExp(param).test(value);
  499. },
  500. $.mage.__('Invalid format.')
  501. ],
  502. 'allow-container-className': [
  503. function (element) {
  504. if (element.type === 'radio' || element.type === 'checkbox') {
  505. return $(element).hasClass('change-container-classname');
  506. }
  507. },
  508. ''
  509. ],
  510. 'validate-no-html-tags': [
  511. function (value) {
  512. return !/<(\/)?\w+/.test(value);
  513. },
  514. $.mage.__('HTML tags are not allowed.')
  515. ],
  516. 'validate-select': [
  517. function (value) {
  518. return value !== 'none' && value != null && value.length !== 0;
  519. },
  520. $.mage.__('Please select an option.')
  521. ],
  522. 'validate-no-empty': [
  523. function (value) {
  524. return !$.mage.isEmpty(value);
  525. },
  526. $.mage.__('Empty Value.')
  527. ],
  528. 'validate-alphanum-with-spaces': [
  529. function (v) {
  530. return $.mage.isEmptyNoTrim(v) || /^[a-zA-Z0-9 ]+$/.test(v);
  531. },
  532. $.mage.__('Please use only letters (a-z or A-Z), numbers (0-9) or spaces only in this field.')
  533. ],
  534. 'validate-data': [
  535. function (v) {
  536. return $.mage.isEmptyNoTrim(v) || /^[A-Za-z]+[A-Za-z0-9_]+$/.test(v);
  537. },
  538. $.mage.__('Please use only letters (a-z or A-Z), numbers (0-9) or underscore (_) in this field, and the first character should be a letter.') //eslint-disable-line max-len
  539. ],
  540. 'validate-street': [
  541. function (v) {
  542. return $.mage.isEmptyNoTrim(v) || /^[ \w]{3,}([A-Za-z]\.)?([ \w]*\#\d+)?(\r\n| )[ \w]{3,}/.test(v);
  543. },
  544. $.mage.__('Please use only letters (a-z or A-Z), numbers (0-9), spaces and "#" in this field.')
  545. ],
  546. 'validate-phoneStrict': [
  547. function (v) {
  548. return $.mage.isEmptyNoTrim(v) || /^(\()?\d{3}(\))?(-|\s)?\d{3}(-|\s)\d{4}$/.test(v);
  549. },
  550. $.mage.__('Please enter a valid phone number. For example (123) 456-7890 or 123-456-7890.')
  551. ],
  552. 'validate-phoneLax': [
  553. function (v) {
  554. return $.mage.isEmptyNoTrim(v) ||
  555. /^((\d[\-. ]?)?((\(\d{3}\))|\d{3}))?[\-. ]?\d{3}[\-. ]?\d{4}$/.test(v);
  556. },
  557. $.mage.__('Please enter a valid phone number. For example (123) 456-7890 or 123-456-7890.')
  558. ],
  559. 'validate-fax': [
  560. function (v) {
  561. return $.mage.isEmptyNoTrim(v) || /^(\()?\d{3}(\))?(-|\s)?\d{3}(-|\s)\d{4}$/.test(v);
  562. },
  563. $.mage.__('Please enter a valid fax number (Ex: 123-456-7890).')
  564. ],
  565. 'validate-email': [
  566. function (v) {
  567. return $.mage.isEmptyNoTrim(v) || /^([a-z0-9,!\#\$%&'\*\+\/=\?\^_`\{\|\}~-]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z0-9,!\#\$%&'\*\+\/=\?\^_`\{\|\}~-]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*@([a-z0-9-]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z0-9-]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*\.(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]){2,})$/i.test(v); //eslint-disable-line max-len
  568. },
  569. $.mage.__('Please enter a valid email address (Ex: johndoe@domain.com).')
  570. ],
  571. 'validate-emailSender': [
  572. function (v) {
  573. return $.mage.isEmptyNoTrim(v) || /^[\S ]+$/.test(v);
  574. },
  575. $.mage.__('Please enter a valid email address (Ex: johndoe@domain.com).')
  576. ],
  577. 'validate-password': [
  578. function (v) {
  579. var pass;
  580. if (v == null) {
  581. return false;
  582. }
  583. //strip leading and trailing spaces
  584. pass = v.trim();
  585. if (!pass.length) {
  586. return true;
  587. }
  588. return !(pass.length > 0 && pass.length < 6);
  589. },
  590. $.mage.__('Please enter 6 or more characters. Leading and trailing spaces will be ignored.')
  591. ],
  592. 'validate-admin-password': [
  593. function (v) {
  594. var pass;
  595. if (v == null) {
  596. return false;
  597. }
  598. pass = v.trim();
  599. // strip leading and trailing spaces
  600. if (pass.length === 0) {
  601. return true;
  602. }
  603. if (!/[a-z]/i.test(v) || !/[0-9]/.test(v)) {
  604. return false;
  605. }
  606. if (pass.length < 7) {
  607. return false;
  608. }
  609. return true;
  610. },
  611. $.mage.__('Please enter 7 or more characters, using both numeric and alphabetic.')
  612. ],
  613. 'validate-customer-password': [
  614. function (v, elm) {
  615. var validator = this,
  616. counter = 0,
  617. passwordMinLength = $(elm).data('password-min-length'),
  618. passwordMinCharacterSets = $(elm).data('password-min-character-sets'),
  619. pass = v.trim(),
  620. result = pass.length >= passwordMinLength;
  621. if (result === false) {
  622. validator.passwordErrorMessage = $.mage.__('Minimum length of this field must be equal or greater than %1 symbols. Leading and trailing spaces will be ignored.').replace('%1', passwordMinLength); //eslint-disable-line max-len
  623. return result;
  624. }
  625. if (pass.match(/\d+/)) {
  626. counter++;
  627. }
  628. if (pass.match(/[a-z]+/)) {
  629. counter++;
  630. }
  631. if (pass.match(/[A-Z]+/)) {
  632. counter++;
  633. }
  634. if (pass.match(/[^a-zA-Z0-9]+/)) {
  635. counter++;
  636. }
  637. if (counter < passwordMinCharacterSets) {
  638. result = false;
  639. validator.passwordErrorMessage = $.mage.__('Minimum of different classes of characters in password is %1. Classes of characters: Lower Case, Upper Case, Digits, Special Characters.').replace('%1', passwordMinCharacterSets); //eslint-disable-line max-len
  640. }
  641. return result;
  642. }, function () {
  643. return this.passwordErrorMessage;
  644. }
  645. ],
  646. 'validate-url': [
  647. function (v) {
  648. if ($.mage.isEmptyNoTrim(v)) {
  649. return true;
  650. }
  651. v = (v || '').replace(/^\s+/, '').replace(/\s+$/, '');
  652. return (/^(http|https|ftp):\/\/(([A-Z0-9]([A-Z0-9_-]*[A-Z0-9]|))(\.[A-Z0-9]([A-Z0-9_-]*[A-Z0-9]|))*)(:(\d+))?(\/[A-Z0-9~](([A-Z0-9_~-]|\.)*[A-Z0-9~]|))*\/?(.*)?$/i).test(v); //eslint-disable-line max-len
  653. },
  654. $.mage.__('Please enter a valid URL. Protocol is required (http://, https:// or ftp://).')
  655. ],
  656. 'validate-clean-url': [
  657. function (v) {
  658. return $.mage.isEmptyNoTrim(v) || /^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+.(com|org|net|dk|at|us|tv|info|uk|co.uk|biz|se)$)(:(\d+))?\/?/i.test(v) || /^(www)((\.[A-Z0-9][A-Z0-9_-]*)+.(com|org|net|dk|at|us|tv|info|uk|co.uk|biz|se)$)(:(\d+))?\/?/i.test(v); //eslint-disable-line max-len
  659. },
  660. $.mage.__('Please enter a valid URL. For example http://www.example.com or www.example.com.')
  661. ],
  662. 'validate-xml-identifier': [
  663. function (v) {
  664. return $.mage.isEmptyNoTrim(v) || /^[A-Z][A-Z0-9_\/-]*$/i.test(v);
  665. },
  666. $.mage.__('Please enter a valid XML-identifier (Ex: something_1, block5, id-4).')
  667. ],
  668. 'validate-ssn': [
  669. function (v) {
  670. return $.mage.isEmptyNoTrim(v) || /^\d{3}-?\d{2}-?\d{4}$/.test(v);
  671. },
  672. $.mage.__('Please enter a valid social security number (Ex: 123-45-6789).')
  673. ],
  674. 'validate-zip-us': [
  675. function (v) {
  676. return $.mage.isEmptyNoTrim(v) || /(^\d{5}$)|(^\d{5}-\d{4}$)/.test(v);
  677. },
  678. $.mage.__('Please enter a valid zip code (Ex: 90602 or 90602-1234).')
  679. ],
  680. 'validate-date-au': [
  681. function (v) {
  682. var regex, d;
  683. if ($.mage.isEmptyNoTrim(v)) {
  684. return true;
  685. }
  686. regex = /^(\d{2})\/(\d{2})\/(\d{4})$/;
  687. if ($.mage.isEmpty(v) || !regex.test(v)) {
  688. return false;
  689. }
  690. d = new Date(v.replace(regex, '$2/$1/$3'));
  691. return parseInt(RegExp.$2, 10) === 1 + d.getMonth() &&
  692. parseInt(RegExp.$1, 10) === d.getDate() &&
  693. parseInt(RegExp.$3, 10) === d.getFullYear();
  694. },
  695. $.mage.__('Please use this date format: dd/mm/yyyy. For example 17/03/2006 for the 17th of March, 2006.')
  696. ],
  697. 'validate-currency-dollar': [
  698. function (v) {
  699. return $.mage.isEmptyNoTrim(v) || /^\$?\-?([1-9]{1}[0-9]{0,2}(\,[0-9]{3})*(\.[0-9]{0,2})?|[1-9]{1}\d*(\.[0-9]{0,2})?|0(\.[0-9]{0,2})?|(\.[0-9]{1,2})?)$/.test(v); //eslint-disable-line max-len
  700. },
  701. $.mage.__('Please enter a valid $ amount. For example $100.00.')
  702. ],
  703. 'validate-not-negative-number': [
  704. function (v) {
  705. if ($.mage.isEmptyNoTrim(v)) {
  706. return true;
  707. }
  708. v = $.mage.parseNumber(v);
  709. return !isNaN(v) && v >= 0;
  710. },
  711. $.mage.__('Please enter a number 0 or greater in this field.')
  712. ],
  713. // validate-not-negative-number should be replaced in all places with this one and then removed
  714. 'validate-zero-or-greater': [
  715. function (v) {
  716. if ($.mage.isEmptyNoTrim(v)) {
  717. return true;
  718. }
  719. v = $.mage.parseNumber(v);
  720. return !isNaN(v) && v >= 0;
  721. },
  722. $.mage.__('Please enter a number 0 or greater in this field.')
  723. ],
  724. 'validate-greater-than-zero': [
  725. function (v) {
  726. if ($.mage.isEmptyNoTrim(v)) {
  727. return true;
  728. }
  729. v = $.mage.parseNumber(v);
  730. return !isNaN(v) && v > 0;
  731. },
  732. $.mage.__('Please enter a number greater than 0 in this field.')
  733. ],
  734. 'validate-css-length': [
  735. function (v) {
  736. if (v !== '') {
  737. return (/^[0-9]*\.*[0-9]+(px|pc|pt|ex|em|mm|cm|in|%)?$/).test(v);
  738. }
  739. return true;
  740. },
  741. $.mage.__('Please input a valid CSS-length (Ex: 100px, 77pt, 20em, .5ex or 50%).')
  742. ],
  743. // Additional methods
  744. 'validate-number': [
  745. function (v) {
  746. return $.mage.isEmptyNoTrim(v) || !isNaN($.mage.parseNumber(v)) && /^\s*-?\d*(\.\d*)?\s*$/.test(v);
  747. },
  748. $.mage.__('Please enter a valid number in this field.')
  749. ],
  750. 'required-number': [
  751. function (v) {
  752. return !!v.length;
  753. },
  754. $.mage.__('Please enter a valid number in this field.')
  755. ],
  756. 'validate-number-range': [
  757. function (v, elm, param) {
  758. var numValue, dataAttrRange, classNameRange, result, range, m, classes, ii;
  759. if ($.mage.isEmptyNoTrim(v)) {
  760. return true;
  761. }
  762. numValue = $.mage.parseNumber(v);
  763. if (isNaN(numValue)) {
  764. return false;
  765. }
  766. dataAttrRange = /^(-?[\d.,]+)?-(-?[\d.,]+)?$/;
  767. classNameRange = /^number-range-(-?[\d.,]+)?-(-?[\d.,]+)?$/;
  768. result = true;
  769. range = param;
  770. if (typeof range === 'string') {
  771. m = dataAttrRange.exec(range);
  772. if (m) {
  773. result = result && $.mage.isBetween(numValue, m[1], m[2]);
  774. } else {
  775. result = false;
  776. }
  777. } else if (elm && elm.className) {
  778. classes = elm.className.split(' ');
  779. ii = classes.length;
  780. while (ii--) {
  781. range = classes[ii];
  782. m = classNameRange.exec(range);
  783. if (m) { //eslint-disable-line max-depth
  784. result = result && $.mage.isBetween(numValue, m[1], m[2]);
  785. break;
  786. }
  787. }
  788. }
  789. return result;
  790. },
  791. $.mage.__('The value is not within the specified range.'),
  792. true
  793. ],
  794. 'validate-digits': [
  795. function (v) {
  796. return $.mage.isEmptyNoTrim(v) || !/[^\d]/.test(v);
  797. },
  798. $.mage.__('Please enter a valid number in this field.')
  799. ],
  800. 'validate-forbidden-extensions': [
  801. function (v, elem) {
  802. var forbiddenExtensions = $(elem).attr('data-validation-params'),
  803. forbiddenExtensionsArray = forbiddenExtensions.split(','),
  804. extensionsArray = v.split(','),
  805. result = true;
  806. this.validateExtensionsMessage = $.mage.__('Forbidden extensions has been used. Avoid usage of ') +
  807. forbiddenExtensions;
  808. $.each(extensionsArray, function (key, extension) {
  809. if (forbiddenExtensionsArray.indexOf(extension) !== -1) {
  810. result = false;
  811. }
  812. });
  813. return result;
  814. }, function () {
  815. return this.validateExtensionsMessage;
  816. }
  817. ],
  818. 'validate-digits-range': [
  819. function (v, elm, param) {
  820. var numValue, dataAttrRange, classNameRange, result, range, m, classes, ii;
  821. if ($.mage.isEmptyNoTrim(v)) {
  822. return true;
  823. }
  824. numValue = $.mage.parseNumber(v);
  825. if (isNaN(numValue)) {
  826. return false;
  827. }
  828. dataAttrRange = /^(-?\d+)?-(-?\d+)?$/;
  829. classNameRange = /^digits-range-(-?\d+)?-(-?\d+)?$/;
  830. result = true;
  831. range = param;
  832. if (typeof range === 'string') {
  833. m = dataAttrRange.exec(range);
  834. if (m) {
  835. result = result && $.mage.isBetween(numValue, m[1], m[2]);
  836. } else {
  837. result = false;
  838. }
  839. } else if (elm && elm.className) {
  840. classes = elm.className.split(' ');
  841. ii = classes.length;
  842. while (ii--) {
  843. range = classes[ii];
  844. m = classNameRange.exec(range);
  845. if (m) { //eslint-disable-line max-depth
  846. result = result && $.mage.isBetween(numValue, m[1], m[2]);
  847. break;
  848. }
  849. }
  850. }
  851. return result;
  852. },
  853. $.mage.__('The value is not within the specified range.'),
  854. true
  855. ],
  856. 'validate-range': [
  857. function (v, elm) {
  858. var minValue, maxValue, ranges, reRange, result, values,
  859. i, name, validRange, minValidRange, maxValidRange;
  860. if ($.mage.isEmptyNoTrim(v)) {
  861. return true;
  862. } else if ($.validator.methods['validate-digits'] && $.validator.methods['validate-digits'](v)) {
  863. minValue = maxValue = $.mage.parseNumber(v);
  864. } else {
  865. ranges = /^(-?\d+)?-(-?\d+)?$/.exec(v);
  866. if (ranges) {
  867. minValue = $.mage.parseNumber(ranges[1]);
  868. maxValue = $.mage.parseNumber(ranges[2]);
  869. if (minValue > maxValue) { //eslint-disable-line max-depth
  870. return false;
  871. }
  872. } else {
  873. return false;
  874. }
  875. }
  876. reRange = /^range-(-?\d+)?-(-?\d+)?$/;
  877. result = true;
  878. values = $(elm).prop('class').split(' ');
  879. for (i = values.length - 1; i >= 0; i--) {
  880. name = values[i];
  881. validRange = reRange.exec(name);
  882. if (validRange) {
  883. minValidRange = $.mage.parseNumber(validRange[1]);
  884. maxValidRange = $.mage.parseNumber(validRange[2]);
  885. result = result &&
  886. (isNaN(minValidRange) || minValue >= minValidRange) &&
  887. (isNaN(maxValidRange) || maxValue <= maxValidRange);
  888. }
  889. }
  890. return result;
  891. },
  892. $.mage.__('The value is not within the specified range.')
  893. ],
  894. 'validate-alpha': [
  895. function (v) {
  896. return $.mage.isEmptyNoTrim(v) || /^[a-zA-Z]+$/.test(v);
  897. },
  898. $.mage.__('Please use letters only (a-z or A-Z) in this field.')
  899. ],
  900. 'validate-code': [
  901. function (v) {
  902. return $.mage.isEmptyNoTrim(v) || /^[a-zA-Z]+[a-zA-Z0-9_]+$/.test(v);
  903. },
  904. $.mage.__('Please use only letters (a-z or A-Z), numbers (0-9) or underscore (_) in this field, and the first character should be a letter.') //eslint-disable-line max-len
  905. ],
  906. 'validate-alphanum': [
  907. function (v) {
  908. return $.mage.isEmptyNoTrim(v) || /^[a-zA-Z0-9]+$/.test(v);
  909. },
  910. $.mage.__('Please use only letters (a-z or A-Z) or numbers (0-9) in this field. No spaces or other characters are allowed.') //eslint-disable-line max-len
  911. ],
  912. 'validate-not-number-first': [
  913. function (value) {
  914. return $.mage.isEmptyNoTrim(value) || /^[^0-9-\.].*$/.test(value.trim());
  915. },
  916. $.mage.__('First character must be letter.')
  917. ],
  918. 'validate-date': [
  919. function (value, params, additionalParams) {
  920. var test = moment(value, utils.convertToMomentFormat(additionalParams.dateFormat));
  921. return $.mage.isEmptyNoTrim(value) || test.isValid();
  922. },
  923. $.mage.__('Please enter a valid date.')
  924. ],
  925. 'validate-date-range': [
  926. function (v, elm) {
  927. var m = /\bdate-range-(\w+)-(\w+)\b/.exec(elm.className),
  928. currentYear, normalizedTime, dependentElements;
  929. if (!m || m[2] === 'to' || $.mage.isEmptyNoTrim(v)) {
  930. return true;
  931. }
  932. currentYear = new Date().getFullYear() + '';
  933. /**
  934. * @param {String} vd
  935. * @return {Number}
  936. */
  937. normalizedTime = function (vd) {
  938. vd = vd.split(/[.\/]/);
  939. if (vd[2] && vd[2].length < 4) {
  940. vd[2] = currentYear.substr(0, vd[2].length) + vd[2];
  941. }
  942. return new Date(vd.join('/')).getTime();
  943. };
  944. dependentElements = $(elm.form).find('.validate-date-range.date-range-' + m[1] + '-to');
  945. return !dependentElements.length || $.mage.isEmptyNoTrim(dependentElements[0].value) ||
  946. normalizedTime(v) <= normalizedTime(dependentElements[0].value);
  947. },
  948. $.mage.__('Make sure the To Date is later than or the same as the From Date.')
  949. ],
  950. 'validate-cpassword': [
  951. function () {
  952. var conf = $('#confirmation').length > 0 ? $('#confirmation') : $($('.validate-cpassword')[0]),
  953. pass = false,
  954. passwordElements, i, passwordElement;
  955. if ($('#password')) {
  956. pass = $('#password');
  957. }
  958. passwordElements = $('.validate-password');
  959. for (i = 0; i < passwordElements.length; i++) {
  960. passwordElement = $(passwordElements[i]);
  961. if (passwordElement.closest('form').attr('id') === conf.closest('form').attr('id')) {
  962. pass = passwordElement;
  963. }
  964. }
  965. if ($('.validate-admin-password').length) {
  966. pass = $($('.validate-admin-password')[0]);
  967. }
  968. return pass.val() === conf.val();
  969. },
  970. $.mage.__('Please make sure your passwords match.')
  971. ],
  972. 'validate-identifier': [
  973. function (v) {
  974. return $.mage.isEmptyNoTrim(v) || /^[a-z0-9][a-z0-9_\/-]+(\.[a-z0-9_-]+)?$/.test(v);
  975. },
  976. $.mage.__('Please enter a valid URL Key (Ex: "example-page", "example-page.html" or "anotherlevel/example-page").') //eslint-disable-line max-len
  977. ],
  978. 'validate-zip-international': [
  979. /*function(v) {
  980. // @TODO: Cleanup
  981. return Validation.get('IsEmpty').test(v) ||
  982. /(^[A-z0-9]{2,10}([\s]{0,1}|[\-]{0,1})[A-z0-9]{2,10}$)/.test(v);
  983. }*/
  984. function () {
  985. return true;
  986. },
  987. $.mage.__('Please enter a valid zip code.')
  988. ],
  989. 'validate-one-required': [
  990. function (v, elm) {
  991. var p = $(elm).parent(),
  992. options = p.find('input');
  993. return options.map(function (el) {
  994. return $(el).val();
  995. }).length > 0;
  996. },
  997. $.mage.__('Please select one of the options above.')
  998. ],
  999. 'validate-state': [
  1000. function (v) {
  1001. return v !== 0;
  1002. },
  1003. $.mage.__('Please select State/Province.')
  1004. ],
  1005. 'required-file': [
  1006. function (v, elm) {
  1007. var result = !$.mage.isEmptyNoTrim(v),
  1008. ovId;
  1009. if (!result) {
  1010. ovId = $('#' + $(elm).attr('id') + '_value');
  1011. if (ovId.length > 0) {
  1012. result = !$.mage.isEmptyNoTrim(ovId.val());
  1013. }
  1014. }
  1015. return result;
  1016. },
  1017. $.mage.__('Please select a file.')
  1018. ],
  1019. 'validate-ajax-error': [
  1020. function (v, element) {
  1021. element = $(element);
  1022. element.on('change.ajaxError', function () {
  1023. element.removeClass('validate-ajax-error');
  1024. element.off('change.ajaxError');
  1025. });
  1026. return !element.hasClass('validate-ajax-error');
  1027. },
  1028. ''
  1029. ],
  1030. 'validate-optional-datetime': [
  1031. function (v, elm, param) {
  1032. var dateTimeParts = $('.datetime-picker[id^="options_' + param + '"]'),
  1033. hasWithValue = false,
  1034. hasWithNoValue = false,
  1035. pattern = /day_part$/i,
  1036. i;
  1037. for (i = 0; i < dateTimeParts.length; i++) {
  1038. if (!pattern.test($(dateTimeParts[i]).attr('id'))) {
  1039. if ($(dateTimeParts[i]).val() === 's') { //eslint-disable-line max-depth
  1040. hasWithValue = true;
  1041. } else {
  1042. hasWithNoValue = true;
  1043. }
  1044. }
  1045. }
  1046. return hasWithValue ^ hasWithNoValue;
  1047. },
  1048. $.mage.__('The field isn\'t complete.')
  1049. ],
  1050. 'validate-required-datetime': [
  1051. function (v, elm, param) {
  1052. var dateTimeParts = $('.datetime-picker[id^="options_' + param + '"]'),
  1053. i;
  1054. for (i = 0; i < dateTimeParts.length; i++) {
  1055. if (dateTimeParts[i].value === '') {
  1056. return false;
  1057. }
  1058. }
  1059. return true;
  1060. },
  1061. $.mage.__('This is a required field.')
  1062. ],
  1063. 'validate-one-required-by-name': [
  1064. function (v, elm, selector) {
  1065. var name = elm.name.replace(/([\\"])/g, '\\$1'),
  1066. container = this.currentForm;
  1067. selector = selector === true ? 'input[name="' + name + '"]:checked' : selector;
  1068. return !!container.querySelectorAll(selector).length;
  1069. },
  1070. $.mage.__('Please select one of the options.')
  1071. ],
  1072. 'less-than-equals-to': [
  1073. function (value, element, params) {
  1074. if ($.isNumeric($(params).val()) && $.isNumeric(value)) {
  1075. this.lteToVal = $(params).val();
  1076. return parseFloat(value) <= parseFloat($(params).val());
  1077. }
  1078. return true;
  1079. },
  1080. function () {
  1081. var message = $.mage.__('Please enter a value less than or equal to %s.');
  1082. return message.replace('%s', this.lteToVal);
  1083. }
  1084. ],
  1085. 'greater-than-equals-to': [
  1086. function (value, element, params) {
  1087. if ($.isNumeric($(params).val()) && $.isNumeric(value)) {
  1088. this.gteToVal = $(params).val();
  1089. return parseFloat(value) >= parseFloat($(params).val());
  1090. }
  1091. return true;
  1092. },
  1093. function () {
  1094. var message = $.mage.__('Please enter a value greater than or equal to %s.');
  1095. return message.replace('%s', this.gteToVal);
  1096. }
  1097. ],
  1098. 'validate-emails': [
  1099. function (value) {
  1100. var validRegexp, emails, i;
  1101. if ($.mage.isEmpty(value)) {
  1102. return true;
  1103. }
  1104. validRegexp = /^([a-z0-9,!\#\$%&'\*\+\/=\?\^_`\{\|\}~-]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z0-9,!\#\$%&'\*\+\/=\?\^_`\{\|\}~-]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*@([a-z0-9-]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z0-9-]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*\.(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]){2,})$/i; //eslint-disable-line max-len
  1105. emails = value.split(/[\s\n\,]+/g);
  1106. for (i = 0; i < emails.length; i++) {
  1107. if (!validRegexp.test(emails[i].trim())) {
  1108. return false;
  1109. }
  1110. }
  1111. return true;
  1112. },
  1113. $.mage.__('Please enter valid email addresses, separated by commas. For example, johndoe@domain.com, johnsmith@domain.com.') //eslint-disable-line max-len
  1114. ],
  1115. 'validate-cc-type-select': [
  1116. /**
  1117. * Validate credit card type matches credit card number
  1118. * @param {*} value - select credit card type
  1119. * @param {*} element - element contains the select box for credit card types
  1120. * @param {*} params - selector for credit card number
  1121. * @return {Boolean}
  1122. */
  1123. function (value, element, params) {
  1124. if (value && params && creditCartTypes[value]) {
  1125. return creditCartTypes[value][0].test($(params).val().replace(/\s+/g, ''));
  1126. }
  1127. return false;
  1128. },
  1129. $.mage.__('Card type does not match credit card number.')
  1130. ],
  1131. 'validate-cc-number': [
  1132. /**
  1133. * Validate credit card number based on mod 10.
  1134. *
  1135. * @param {*} value - credit card number
  1136. * @return {Boolean}
  1137. */
  1138. function (value) {
  1139. if (value) {
  1140. return validateCreditCard(value);
  1141. }
  1142. return false;
  1143. },
  1144. $.mage.__('Please enter a valid credit card number.')
  1145. ],
  1146. 'validate-cc-type': [
  1147. /**
  1148. * Validate credit card number is for the correct credit card type.
  1149. *
  1150. * @param {String} value - credit card number
  1151. * @param {*} element - element contains credit card number
  1152. * @param {*} params - selector for credit card type
  1153. * @return {Boolean}
  1154. */
  1155. function (value, element, params) {
  1156. var ccType;
  1157. if (value && params) {
  1158. ccType = $(params).val();
  1159. value = value.replace(/\s/g, '').replace(/\-/g, '');
  1160. if (creditCartTypes[ccType] && creditCartTypes[ccType][0]) {
  1161. return creditCartTypes[ccType][0].test(value);
  1162. } else if (creditCartTypes[ccType] && !creditCartTypes[ccType][0]) {
  1163. return true;
  1164. }
  1165. }
  1166. return false;
  1167. },
  1168. $.mage.__('Credit card number does not match credit card type.')
  1169. ],
  1170. 'validate-cc-exp': [
  1171. /**
  1172. * Validate credit card expiration date, make sure it's within the year and not before current month.
  1173. *
  1174. * @param {*} value - month
  1175. * @param {*} element - element contains month
  1176. * @param {*} params - year selector
  1177. * @return {Boolean}
  1178. */
  1179. function (value, element, params) {
  1180. var isValid = false,
  1181. month, year, currentTime, currentMonth, currentYear;
  1182. if (value && params) {
  1183. month = value;
  1184. year = $(params).val();
  1185. currentTime = new Date();
  1186. currentMonth = currentTime.getMonth() + 1;
  1187. currentYear = currentTime.getFullYear();
  1188. isValid = !year || year > currentYear || year == currentYear && month >= currentMonth; //eslint-disable-line
  1189. }
  1190. return isValid;
  1191. },
  1192. $.mage.__('Incorrect credit card expiration date.')
  1193. ],
  1194. 'validate-cc-cvn': [
  1195. /**
  1196. * Validate credit card cvn based on credit card type.
  1197. *
  1198. * @param {*} value - credit card cvn
  1199. * @param {*} element - element contains credit card cvn
  1200. * @param {*} params - credit card type selector
  1201. * @return {*}
  1202. */
  1203. function (value, element, params) {
  1204. var ccType;
  1205. if (value && params) {
  1206. ccType = $(params).val();
  1207. if (creditCartTypes[ccType] && creditCartTypes[ccType][0]) {
  1208. return creditCartTypes[ccType][1].test(value);
  1209. }
  1210. }
  1211. return false;
  1212. },
  1213. $.mage.__('Please enter a valid credit card verification number.')
  1214. ],
  1215. 'validate-cc-ukss': [
  1216. /**
  1217. * Validate Switch/Solo/Maestro issue number and start date is filled.
  1218. *
  1219. * @param {*} value - input field value
  1220. * @return {*}
  1221. */
  1222. function (value) {
  1223. return value;
  1224. },
  1225. $.mage.__('Please enter issue number or start date for switch/solo card type.')
  1226. ],
  1227. 'validate-length': [
  1228. function (v, elm) {
  1229. var reMax = new RegExp(/^maximum-length-[0-9]+$/),
  1230. reMin = new RegExp(/^minimum-length-[0-9]+$/),
  1231. validator = this,
  1232. result = true,
  1233. length = 0;
  1234. $.each(elm.className.split(' '), function (index, name) {
  1235. if (name.match(reMax) && result) {
  1236. length = name.split('-')[2];
  1237. result = v.length <= length;
  1238. validator.validateMessage =
  1239. $.mage.__('Please enter less or equal than %1 symbols.').replace('%1', length);
  1240. }
  1241. if (name.match(reMin) && result && !$.mage.isEmpty(v)) {
  1242. length = name.split('-')[2];
  1243. result = v.length >= length;
  1244. validator.validateMessage =
  1245. $.mage.__('Please enter more or equal than %1 symbols.').replace('%1', length);
  1246. }
  1247. });
  1248. return result;
  1249. }, function () {
  1250. return this.validateMessage;
  1251. }
  1252. ],
  1253. 'required-entry': [
  1254. function (value) {
  1255. return !$.mage.isEmpty(value);
  1256. }, $.mage.__('This is a required field.')
  1257. ],
  1258. 'not-negative-amount': [
  1259. function (v) {
  1260. if (v.length) {
  1261. return (/^\s*\d+([,.]\d+)*\s*%?\s*$/).test(v);
  1262. }
  1263. return true;
  1264. },
  1265. $.mage.__('Please enter positive number in this field.')
  1266. ],
  1267. 'validate-per-page-value-list': [
  1268. function (v) {
  1269. var isValid = true,
  1270. values = v.split(','),
  1271. i;
  1272. if ($.mage.isEmpty(v)) {
  1273. return isValid;
  1274. }
  1275. for (i = 0; i < values.length; i++) {
  1276. if (!/^[0-9]+$/.test(values[i])) {
  1277. isValid = false;
  1278. }
  1279. }
  1280. return isValid;
  1281. },
  1282. $.mage.__('Please enter a valid value, ex: 10,20,30')
  1283. ],
  1284. 'validate-per-page-value': [
  1285. function (v, elm) {
  1286. var values;
  1287. if ($.mage.isEmpty(v)) {
  1288. return false;
  1289. }
  1290. values = $('#' + elm.id + '_values').val().split(',');
  1291. return values.indexOf(v) !== -1;
  1292. },
  1293. $.mage.__('Please enter a valid value from list')
  1294. ],
  1295. 'validate-new-password': [
  1296. function (v) {
  1297. if ($.validator.methods['validate-password'] && !$.validator.methods['validate-password'](v)) {
  1298. return false;
  1299. }
  1300. if ($.mage.isEmpty(v) && v !== '') {
  1301. return false;
  1302. }
  1303. return true;
  1304. },
  1305. $.mage.__('Please enter 6 or more characters. Leading and trailing spaces will be ignored.')
  1306. ],
  1307. 'required-if-not-specified': [
  1308. function (value, element, params) {
  1309. var valid = false,
  1310. alternate = $(params),
  1311. alternateValue;
  1312. if (alternate.length > 0) {
  1313. valid = this.check(alternate);
  1314. // if valid, it may be blank, so check for that
  1315. if (valid) {
  1316. alternateValue = alternate.val();
  1317. if (typeof alternateValue == 'undefined' || alternateValue.length === 0) { //eslint-disable-line
  1318. valid = false;
  1319. }
  1320. }
  1321. }
  1322. if (!valid) {
  1323. valid = !this.optional(element);
  1324. }
  1325. return valid;
  1326. },
  1327. $.mage.__('This is a required field.')
  1328. ],
  1329. 'required-if-all-sku-empty-and-file-not-loaded': [
  1330. function (value, element, params) {
  1331. var valid = false,
  1332. alternate = $(params.specifiedId),
  1333. alternateValue;
  1334. if (alternate.length > 0) {
  1335. valid = this.check(alternate);
  1336. // if valid, it may be blank, so check for that
  1337. if (valid) {
  1338. alternateValue = alternate.val();
  1339. if (typeof alternateValue == 'undefined' || alternateValue.length === 0) { //eslint-disable-line
  1340. valid = false;
  1341. }
  1342. }
  1343. }
  1344. if (!valid) {
  1345. valid = !this.optional(element);
  1346. }
  1347. $('input[' + params.dataSku + '=true]').each(function () {
  1348. if ($(this).val() !== '') {
  1349. valid = true;
  1350. }
  1351. });
  1352. return valid;
  1353. },
  1354. $.mage.__('Please enter valid SKU key.')
  1355. ],
  1356. 'required-if-specified': [
  1357. function (value, element, params) {
  1358. var valid = true,
  1359. dependent = $(params),
  1360. dependentValue;
  1361. if (dependent.length > 0) {
  1362. valid = this.check(dependent);
  1363. // if valid, it may be blank, so check for that
  1364. if (valid) {
  1365. dependentValue = dependent.val();
  1366. valid = typeof dependentValue != 'undefined' && dependentValue.length > 0;
  1367. }
  1368. }
  1369. if (valid) {
  1370. valid = !this.optional(element);
  1371. } else {
  1372. valid = true; // dependent was not valid, so don't even check
  1373. }
  1374. return valid;
  1375. },
  1376. $.mage.__('This is a required field.')
  1377. ],
  1378. 'required-number-if-specified': [
  1379. function (value, element, params) {
  1380. var valid = true,
  1381. dependent = $(params),
  1382. depeValue;
  1383. if (dependent.length) {
  1384. valid = this.check(dependent);
  1385. if (valid) {
  1386. depeValue = dependent[0].value;
  1387. valid = !!(depeValue && depeValue.length);
  1388. }
  1389. }
  1390. return valid ? !!value.length : true;
  1391. },
  1392. $.mage.__('Please enter a valid number.')
  1393. ],
  1394. 'datetime-validation': [
  1395. function (value, element) {
  1396. var isValid = true;
  1397. if ($(element).val().length === 0) {
  1398. isValid = false;
  1399. $(element).addClass('mage-error');
  1400. }
  1401. return isValid;
  1402. },
  1403. $.mage.__('This is required field')
  1404. ],
  1405. 'required-text-swatch-entry': [
  1406. tableSingleValidation,
  1407. $.mage.__('Admin is a required field in each row.')
  1408. ],
  1409. 'required-visual-swatch-entry': [
  1410. tableSingleValidation,
  1411. $.mage.__('Admin is a required field in each row.')
  1412. ],
  1413. 'required-dropdown-attribute-entry': [
  1414. tableSingleValidation,
  1415. $.mage.__('Admin is a required field in each row.')
  1416. ],
  1417. 'validate-item-quantity': [
  1418. function (value, element, params) {
  1419. var validator = this,
  1420. result = false,
  1421. // obtain values for validation
  1422. qty = $.mage.parseNumber(value),
  1423. isMinAllowedValid = typeof params.minAllowed === 'undefined' ||
  1424. qty >= $.mage.parseNumber(params.minAllowed),
  1425. isMaxAllowedValid = typeof params.maxAllowed === 'undefined' ||
  1426. qty <= $.mage.parseNumber(params.maxAllowed),
  1427. isQtyIncrementsValid = typeof params.qtyIncrements === 'undefined' ||
  1428. resolveModulo(qty, $.mage.parseNumber(params.qtyIncrements)) === 0.0;
  1429. result = qty > 0;
  1430. if (result === false) {
  1431. validator.itemQtyErrorMessage = $.mage.__('Please enter a quantity greater than 0.');//eslint-disable-line max-len
  1432. return result;
  1433. }
  1434. result = isMinAllowedValid;
  1435. if (result === false) {
  1436. validator.itemQtyErrorMessage = $.mage.__('The fewest you may purchase is %1.').replace('%1', params.minAllowed);//eslint-disable-line max-len
  1437. return result;
  1438. }
  1439. result = isMaxAllowedValid;
  1440. if (result === false) {
  1441. validator.itemQtyErrorMessage = $.mage.__('The maximum you may purchase is %1.').replace('%1', params.maxAllowed);//eslint-disable-line max-len
  1442. return result;
  1443. }
  1444. result = isQtyIncrementsValid;
  1445. if (result === false) {
  1446. validator.itemQtyErrorMessage = $.mage.__('You can buy this product only in quantities of %1 at a time.').replace('%1', params.qtyIncrements);//eslint-disable-line max-len
  1447. return result;
  1448. }
  1449. return result;
  1450. }, function () {
  1451. return this.itemQtyErrorMessage;
  1452. }
  1453. ],
  1454. 'password-not-equal-to-user-name': [
  1455. function (value, element, params) {
  1456. if (typeof params === 'string') {
  1457. return value.toLowerCase() !== params.toLowerCase();
  1458. }
  1459. return true;
  1460. },
  1461. $.mage.__('The password can\'t be the same as the email address. Create a new password and try again.')
  1462. ]
  1463. };
  1464. $.each(rules, function (i, rule) {
  1465. rule.unshift(i);
  1466. $.validator.addMethod.apply($.validator, rule);
  1467. });
  1468. $.validator.addClassRules({
  1469. 'required-option': {
  1470. required: true
  1471. },
  1472. 'required-options-count': {
  1473. required: true
  1474. },
  1475. 'validate-both-passwords': {
  1476. 'validate-cpassword': true
  1477. }
  1478. });
  1479. $.validator.messages = $.extend($.validator.messages, {
  1480. required: $.mage.__('This is a required field.'),
  1481. remote: $.mage.__('Please fix this field.'),
  1482. email: $.mage.__('Please enter a valid email address.'),
  1483. url: $.mage.__('Please enter a valid URL.'),
  1484. date: $.mage.__('Please enter a valid date.'),
  1485. dateISO: $.mage.__('Please enter a valid date (ISO).'),
  1486. number: $.mage.__('Please enter a valid number.'),
  1487. digits: $.mage.__('Please enter only digits.'),
  1488. creditcard: $.mage.__('Please enter a valid credit card number.'),
  1489. equalTo: $.mage.__('Please enter the same value again.'),
  1490. maxlength: $.validator.format($.mage.__('Please enter no more than {0} characters.')),
  1491. minlength: $.validator.format($.mage.__('Please enter at least {0} characters.')),
  1492. rangelength: $.validator.format($.mage.__('Please enter a value between {0} and {1} characters long.')),
  1493. range: $.validator.format($.mage.__('Please enter a value between {0} and {1}.')),
  1494. max: $.validator.format($.mage.__('Please enter a value less than or equal to {0}.')),
  1495. min: $.validator.format($.mage.__('Please enter a value greater than or equal to {0}.'))
  1496. });
  1497. if ($.metadata) {
  1498. // Setting the type as html5 to enable data-validate attribute
  1499. $.metadata.setType('html5');
  1500. }
  1501. showLabel = $.validator.prototype.showLabel;
  1502. $.extend(true, $.validator.prototype, {
  1503. /**
  1504. * @param {*} element
  1505. * @param {*} message
  1506. */
  1507. showLabel: function (element, message) {
  1508. var label, elem;
  1509. showLabel.call(this, element, message);
  1510. // ARIA (adding aria-invalid & aria-describedby)
  1511. label = this.errorsFor(element);
  1512. elem = $(element);
  1513. if (!label.attr('id')) {
  1514. label.attr('id', this.idOrName(element) + '-error');
  1515. }
  1516. elem.attr('aria-invalid', 'true')
  1517. .attr('aria-describedby', label.attr('id'));
  1518. }
  1519. });
  1520. /**
  1521. * Validate form field without instantiating validate plug-in.
  1522. *
  1523. * @param {Element|String} element - DOM element or selector
  1524. * @return {Boolean} validation result
  1525. */
  1526. $.validator.validateElement = function (element) {
  1527. var form, validator, valid, classes;
  1528. element = $(element);
  1529. form = element.get(0).form;
  1530. validator = form ? $(form).data('validator') : null;
  1531. if (validator) {
  1532. return validator.element(element.get(0));
  1533. }
  1534. valid = true;
  1535. classes = element.prop('class').split(' ');
  1536. $.each(classes, $.proxy(function (i, className) {
  1537. if (this.methods[className] && !this.methods[className](element.val(), element.get(0))) {
  1538. valid = false;
  1539. return valid;
  1540. }
  1541. }, this));
  1542. return valid;
  1543. };
  1544. originValidateDelegate = $.fn.validateDelegate;
  1545. /**
  1546. * @return {*}
  1547. */
  1548. $.fn.validateDelegate = function () {
  1549. if (!this[0].form) {
  1550. return this;
  1551. }
  1552. return originValidateDelegate.apply(this, arguments);
  1553. };
  1554. /**
  1555. * Validate single element.
  1556. *
  1557. * @param {Element} element
  1558. * @param {Object} config
  1559. * @returns {*}
  1560. */
  1561. $.validator.validateSingleElement = function (element, config) {
  1562. var errors = {},
  1563. valid = true,
  1564. validateConfig = {
  1565. errorElement: 'label',
  1566. ignore: '.ignore-validate',
  1567. hideError: false
  1568. },
  1569. form, validator, classes, elementValue;
  1570. $.extend(validateConfig, config);
  1571. element = $(element).not(validateConfig.ignore);
  1572. if (!element.length) {
  1573. return true;
  1574. }
  1575. form = element.get(0).form;
  1576. validator = form ? $(form).data('validator') : null;
  1577. if (validator) {
  1578. return validator.element(element.get(0));
  1579. }
  1580. classes = element.prop('class').split(' ');
  1581. validator = element.parent().data('validator') ||
  1582. $.mage.validation(validateConfig, element.parent()).validate;
  1583. element.removeClass(validator.settings.errorClass);
  1584. validator.toHide = validator.toShow;
  1585. validator.hideErrors();
  1586. validator.toShow = validator.toHide = $([]);
  1587. $.each(classes, $.proxy(function (i, className) {
  1588. elementValue = element.val();
  1589. if (element.is(':checkbox') || element.is(':radio')) {
  1590. elementValue = element.is(':checked') || null;
  1591. }
  1592. if (this.methods[className] && !this.methods[className](elementValue, element.get(0))) {
  1593. valid = false;
  1594. errors[element.get(0).name] = this.messages[className];
  1595. validator.invalid[element.get(0).name] = true;
  1596. if (!validateConfig.hideError) {
  1597. validator.showErrors(errors);
  1598. }
  1599. return valid;
  1600. }
  1601. }, this));
  1602. return valid;
  1603. };
  1604. $.widget('mage.validation', {
  1605. options: {
  1606. meta: 'validate',
  1607. onfocusout: false,
  1608. onkeyup: false,
  1609. onclick: false,
  1610. ignoreTitle: true,
  1611. errorClass: 'mage-error',
  1612. errorElement: 'div',
  1613. /**
  1614. * @param {*} error
  1615. * @param {*} element
  1616. */
  1617. errorPlacement: function (error, element) {
  1618. var errorPlacement = element,
  1619. fieldWrapper;
  1620. // logic for date-picker error placement
  1621. if (element.hasClass('_has-datepicker')) {
  1622. errorPlacement = element.siblings('button');
  1623. }
  1624. // logic for field wrapper
  1625. fieldWrapper = element.closest('.addon');
  1626. if (fieldWrapper.length) {
  1627. errorPlacement = fieldWrapper.after(error);
  1628. }
  1629. //logic for checkboxes/radio
  1630. if (element.is(':checkbox') || element.is(':radio')) {
  1631. errorPlacement = element.parents('.control').children().last();
  1632. //fallback if group does not have .control parent
  1633. if (!errorPlacement.length) {
  1634. errorPlacement = element.siblings('label').last();
  1635. }
  1636. }
  1637. //logic for control with tooltip
  1638. if (element.siblings('.tooltip').length) {
  1639. errorPlacement = element.siblings('.tooltip');
  1640. }
  1641. //logic for select with tooltip in after element
  1642. if (element.next().find('.tooltip').length) {
  1643. errorPlacement = element.next();
  1644. }
  1645. errorPlacement.after(error);
  1646. }
  1647. },
  1648. /**
  1649. * Check if form pass validation rules without submit.
  1650. *
  1651. * @return boolean
  1652. */
  1653. isValid: function () {
  1654. return this.element.valid();
  1655. },
  1656. /**
  1657. * Remove validation error messages
  1658. */
  1659. clearError: function () {
  1660. if (arguments.length) {
  1661. $.each(arguments, $.proxy(function (index, item) {
  1662. this.validate.prepareElement(item);
  1663. this.validate.hideErrors();
  1664. }, this));
  1665. } else {
  1666. this.validate.resetForm();
  1667. }
  1668. },
  1669. /**
  1670. * Validation creation.
  1671. *
  1672. * @protected
  1673. */
  1674. _create: function () {
  1675. this.validate = this.element.validate(this.options);
  1676. // ARIA (adding aria-required attribute)
  1677. this.element
  1678. .find('.field.required')
  1679. .find('.control')
  1680. .find('input, select, textarea')
  1681. .attr('aria-required', 'true');
  1682. this._listenFormValidate();
  1683. },
  1684. /**
  1685. * Validation listening.
  1686. *
  1687. * @protected
  1688. */
  1689. _listenFormValidate: function () {
  1690. $('form').on('invalid-form.validate', this.listenFormValidateHandler);
  1691. },
  1692. /**
  1693. * Handle form validation. Focus on first invalid form field.
  1694. *
  1695. * @param {jQuery.Event} event
  1696. * @param {Object} validation
  1697. */
  1698. listenFormValidateHandler: function (event, validation) {
  1699. var firstActive = $(validation.errorList[0].element || []),
  1700. lastActive = $(validation.findLastActive() ||
  1701. validation.errorList.length && validation.errorList[0].element || []),
  1702. windowHeight = $(window).height(),
  1703. parent, successList;
  1704. if (lastActive.is(':hidden')) {
  1705. parent = lastActive.parent();
  1706. $('html, body').animate({
  1707. scrollTop: parent.offset().top - windowHeight / 2
  1708. });
  1709. }
  1710. // ARIA (removing aria attributes if success)
  1711. successList = validation.successList;
  1712. if (successList.length) {
  1713. $.each(successList, function () {
  1714. $(this)
  1715. .removeAttr('aria-describedby')
  1716. .removeAttr('aria-invalid');
  1717. });
  1718. }
  1719. if (firstActive.length) {
  1720. $('html, body').stop().animate({
  1721. scrollTop: firstActive.parent().offset().top - windowHeight / 2
  1722. });
  1723. firstActive.focus();
  1724. }
  1725. }
  1726. });
  1727. return $.mage.validation;
  1728. });