Advertisement
Guest User

formancejs Phone Number Brazil

a guest
Mar 18th, 2015
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 51.69 KB | None | 0 0
  1. /**
  2. * Plugin alterado para suportar telefones do Brasil, com 10 e 11 dígitos
  3. * @auth Hugo Sóstenes
  4. *
  5. */
  6. // Generated by CoffeeScript 1.6.2
  7. (function() {
  8. var $, cardFromNumber, cardFromType, cards, defaultFormat, formatBackCardNumber, formatBackDateDDMMYYYY, formatBackDateYYYYMMDD, formatBackExpiry, formatBackOntarioDriversLicenseNumber, formatBackOntarioOutdoorsCardNumber, formatBackOntarioPhotoHealthCardNumber, formatBackPhoneNumber, formatBackPostalCode, formatBackTimeYYMM, formatBackUkSortCode, formatCardNumber, formatDateDDMMYYYY, formatDateYYYYMMDD, formatExpiry, formatForwardDateDDMMYYYY, formatForwardDateYYYYMMDD, formatForwardExpiry, formatForwardSlashDateDDMMYYYY, formatForwardSlashDateYYYYMMDD, formatForwardSlashExpiry, formatForwardSlashTimeYYMM, formatForwardTimeYYMM, formatForwardUkSortCode, formatOntarioDriversLicenseNumber, formatOntarioOutdoorsCardNumber, formatOntarioPhotoHealthCardNumber, formatPasteOntarioDriversLicenseNumber, formatPasteOntarioOutdoorsCardNumber, formatPasteOntarioPhotoHealthCardNumber, formatPastePhoneNumber, formatPastePostalCode, formatPhoneNumber, formatPostalCode, formatTimeYYMM, formatUkSortCode, hasTextSelected, luhnCheck, parseCreditCardExpiry, parseDateDDMMYYYY, parseTimeYYMM, parse_date_yyyy_mm_dd, reFormatCardNumber, reFormatPhoneNumber, restrictAlphaNumeric, restrictCVC, restrictCardNumber, restrictDateDDMMYYYY, restrictDateYYYYMMDD, restrictExpiry, restrictNumeric, restrictOntarioDriversLicenseNumber, restrictOntarioOutdoorsCardNumber, restrictOntarioPhotoHealthCardNumber, restrictPhoneNumber, restrictPostalCode, restrictTimeYYMM, restrictUkSortCode, setCardType,
  9. __slice = [].slice,
  10. __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
  11.  
  12. $ = jQuery;
  13.  
  14. $.formance = {};
  15.  
  16. $.formance.fn = {};
  17.  
  18. $.fn.formance = function() {
  19. var args, method;
  20.  
  21. method = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
  22. return $.formance.fn[method].apply(this, args);
  23. };
  24.  
  25. restrictNumeric = function(e) {
  26. var $target, input;
  27.  
  28. $target = $(e.target);
  29. if (e.metaKey || e.ctrlKey) {
  30. return true;
  31. }
  32. if (e.which === 32) {
  33. return false;
  34. }
  35. if (e.which === 0) {
  36. return true;
  37. }
  38. if (e.which < 33) {
  39. return true;
  40. }
  41. input = String.fromCharCode(e.which);
  42. return !!/[\d\s]/.test(input);
  43. };
  44.  
  45. restrictAlphaNumeric = function(e) {
  46. var $target, input;
  47.  
  48. $target = $(e.target);
  49. if (e.metaKey || e.ctrlKey) {
  50. return true;
  51. }
  52. if (e.which === 32) {
  53. return false;
  54. }
  55. if (e.which === 0) {
  56. return true;
  57. }
  58. if (e.which < 33) {
  59. return true;
  60. }
  61. input = String.fromCharCode(e.which);
  62. return !!/[\d\sA-Za-z]/.test(input);
  63. };
  64.  
  65. hasTextSelected = function($target) {
  66. var _ref;
  67.  
  68. if (($target.prop('selectionStart') != null) && $target.prop('selectionStart') !== $target.prop('selectionEnd')) {
  69. return true;
  70. }
  71. if (typeof document !== "undefined" && document !== null ? (_ref = document.selection) != null ? typeof _ref.createRange === "function" ? _ref.createRange().text : void 0 : void 0 : void 0) {
  72. return true;
  73. }
  74. return false;
  75. };
  76.  
  77. $.formance.fn.restrictNumeric = function() {
  78. this.on('keypress', restrictNumeric);
  79. return this;
  80. };
  81.  
  82. $.formance.fn.restrictAlphaNumeric = function() {
  83. this.on('keypress', restrictAlphaNumeric);
  84. return this;
  85. };
  86.  
  87. $.formance.fn.hasTextSelected = hasTextSelected;
  88.  
  89. $ = jQuery;
  90.  
  91. hasTextSelected = $.formance.fn.hasTextSelected;
  92.  
  93. defaultFormat = /(\d{1,4})/g;
  94.  
  95. cards = [
  96. {
  97. type: 'maestro',
  98. pattern: /^(5018|5020|5038|6304|6759|676[1-3])/,
  99. format: defaultFormat,
  100. length: [12, 13, 14, 15, 16, 17, 18, 19],
  101. cvcLength: [3],
  102. luhn: true
  103. }, {
  104. type: 'dinersclub',
  105. pattern: /^(36|38|30[0-5])/,
  106. format: defaultFormat,
  107. length: [14],
  108. cvcLength: [3],
  109. luhn: true
  110. }, {
  111. type: 'laser',
  112. pattern: /^(6706|6771|6709)/,
  113. format: defaultFormat,
  114. length: [16, 17, 18, 19],
  115. cvcLength: [3],
  116. luhn: true
  117. }, {
  118. type: 'jcb',
  119. pattern: /^35/,
  120. format: defaultFormat,
  121. length: [16],
  122. cvcLength: [3],
  123. luhn: true
  124. }, {
  125. type: 'unionpay',
  126. pattern: /^62/,
  127. format: defaultFormat,
  128. length: [16, 17, 18, 19],
  129. cvcLength: [3],
  130. luhn: false
  131. }, {
  132. type: 'discover',
  133. pattern: /^(6011|65|64[4-9]|622)/,
  134. format: defaultFormat,
  135. length: [16],
  136. cvcLength: [3],
  137. luhn: true
  138. }, {
  139. type: 'mastercard',
  140. pattern: /^5[1-5]/,
  141. format: defaultFormat,
  142. length: [16],
  143. cvcLength: [3],
  144. luhn: true
  145. }, {
  146. type: 'amex',
  147. pattern: /^3[47]/,
  148. format: /(\d{1,4})(\d{1,6})?(\d{1,5})?/,
  149. length: [15],
  150. cvcLength: [3, 4],
  151. luhn: true
  152. }, {
  153. type: 'visa',
  154. pattern: /^4/,
  155. format: defaultFormat,
  156. length: [13, 14, 15, 16],
  157. cvcLength: [3],
  158. luhn: true
  159. }
  160. ];
  161.  
  162. cardFromNumber = function(num) {
  163. var card, _i, _len;
  164.  
  165. num = (num + '').replace(/\D/g, '');
  166. for (_i = 0, _len = cards.length; _i < _len; _i++) {
  167. card = cards[_i];
  168. if (card.pattern.test(num)) {
  169. return card;
  170. }
  171. }
  172. };
  173.  
  174. cardFromType = function(type) {
  175. var card, _i, _len;
  176.  
  177. for (_i = 0, _len = cards.length; _i < _len; _i++) {
  178. card = cards[_i];
  179. if (card.type === type) {
  180. return card;
  181. }
  182. }
  183. };
  184.  
  185. restrictCVC = function(e) {
  186. var $target, digit, val;
  187.  
  188. $target = $(e.currentTarget);
  189. digit = String.fromCharCode(e.which);
  190. if (!/^\d+$/.test(digit)) {
  191. return;
  192. }
  193. val = $target.val() + digit;
  194. return val.length <= 4;
  195. };
  196.  
  197. $.formance.fn.format_credit_card_cvc = function() {
  198. this.formance('restrictNumeric');
  199. this.on('keypress', restrictCVC);
  200. return this;
  201. };
  202.  
  203. $.formance.fn.validate_credit_card_cvc = function() {
  204. var cvc, type, _ref, _ref1;
  205.  
  206. type = $(this).data('credit_card_type');
  207. cvc = $(this).val();
  208. cvc = $.trim(cvc);
  209. if (!/^\d+$/.test(cvc)) {
  210. return false;
  211. }
  212. if (type) {
  213. return _ref = cvc.length, __indexOf.call((_ref1 = cardFromType(type)) != null ? _ref1.cvcLength : void 0, _ref) >= 0;
  214. } else {
  215. return cvc.length >= 3 && cvc.length <= 4;
  216. }
  217. };
  218.  
  219. $ = jQuery;
  220.  
  221. hasTextSelected = $.formance.fn.hasTextSelected;
  222.  
  223. restrictExpiry = function(e) {
  224. var $target, digit, value;
  225.  
  226. $target = $(e.currentTarget);
  227. digit = String.fromCharCode(e.which);
  228. if (!/^\d+$/.test(digit)) {
  229. return;
  230. }
  231. if (hasTextSelected($target)) {
  232. return;
  233. }
  234. value = $target.val() + digit;
  235. value = value.replace(/\D/g, '');
  236. if (value.length > 6) {
  237. return false;
  238. }
  239. };
  240.  
  241. formatExpiry = function(e) {
  242. var $target, digit, val;
  243.  
  244. digit = String.fromCharCode(e.which);
  245. if (!/^\d+$/.test(digit)) {
  246. return;
  247. }
  248. $target = $(e.currentTarget);
  249. val = $target.val() + digit;
  250. if (/^\d$/.test(val) && (val !== '0' && val !== '1')) {
  251. e.preventDefault();
  252. return $target.val("0" + val + " / ");
  253. } else if (/^\d\d$/.test(val)) {
  254. e.preventDefault();
  255. return $target.val("" + val + " / ");
  256. }
  257. };
  258.  
  259. formatForwardExpiry = function(e) {
  260. var $target, digit, val;
  261.  
  262. digit = String.fromCharCode(e.which);
  263. if (!/^\d+$/.test(digit)) {
  264. return;
  265. }
  266. $target = $(e.currentTarget);
  267. val = $target.val();
  268. if (/^\d\d$/.test(val)) {
  269. return $target.val("" + val + " / ");
  270. }
  271. };
  272.  
  273. formatForwardSlashExpiry = function(e) {
  274. var $target, slash, val;
  275.  
  276. slash = String.fromCharCode(e.which);
  277. if (slash !== '/') {
  278. return;
  279. }
  280. $target = $(e.currentTarget);
  281. val = $target.val();
  282. if (/^\d$/.test(val) && val !== '0') {
  283. return $target.val("0" + val + " / ");
  284. }
  285. };
  286.  
  287. formatBackExpiry = function(e) {
  288. var $target, value;
  289.  
  290. if (e.meta) {
  291. return;
  292. }
  293. $target = $(e.currentTarget);
  294. value = $target.val();
  295. if (e.which !== 8) {
  296. return;
  297. }
  298. if (($target.prop('selectionStart') != null) && $target.prop('selectionStart') !== value.length) {
  299. return;
  300. }
  301. if (/\d(\s|\/)+$/.test(value)) {
  302. e.preventDefault();
  303. return $target.val(value.replace(/\d(\s|\/)*$/, ''));
  304. } else if (/\s\/\s?\d?$/.test(value)) {
  305. e.preventDefault();
  306. return $target.val(value.replace(/\s\/\s?\d?$/, ''));
  307. }
  308. };
  309.  
  310. $.formance.fn.format_credit_card_expiry = function() {
  311. this.formance('restrictNumeric');
  312. this.on('keypress', restrictExpiry);
  313. this.on('keypress', formatExpiry);
  314. this.on('keypress', formatForwardSlashExpiry);
  315. this.on('keypress', formatForwardExpiry);
  316. this.on('keydown', formatBackExpiry);
  317. return this;
  318. };
  319.  
  320. parseCreditCardExpiry = function(expiry_string) {
  321. var month, prefix, val, year, _ref;
  322.  
  323. val = expiry_string.replace(/\s/g, '');
  324. _ref = val.split('/', 2), month = _ref[0], year = _ref[1];
  325. if ((year != null ? year.length : void 0) === 2 && /^\d+$/.test(year)) {
  326. prefix = (new Date).getFullYear();
  327. prefix = prefix.toString().slice(0, 2);
  328. year = prefix + year;
  329. }
  330. month = parseInt(month, 10);
  331. year = parseInt(year, 10);
  332. return {
  333. month: month,
  334. year: year
  335. };
  336. };
  337.  
  338. $.formance.fn.val_credit_card_expiry = function() {
  339. var expiry;
  340.  
  341. expiry = parseCreditCardExpiry(this.val());
  342. if ((expiry.month == null) || isNaN(expiry.month)) {
  343. return false;
  344. }
  345. if ((expiry.year == null) || isNaN(expiry.year)) {
  346. return false;
  347. }
  348. return new Date(expiry.year, expiry.month - 1);
  349. };
  350.  
  351. $.formance.fn.validate_credit_card_expiry = function() {
  352. var currentTime, expiry, expiry_date, month, prefix, year, _ref;
  353.  
  354. expiry_date = parseCreditCardExpiry(this.val());
  355. month = expiry_date.month;
  356. year = expiry_date.year;
  357. if (typeof month === 'object' && 'month' in month) {
  358. _ref = month, month = _ref.month, year = _ref.year;
  359. }
  360. if (!(month && year)) {
  361. return false;
  362. }
  363. month = $.trim(month);
  364. year = $.trim(year);
  365. if (!/^\d+$/.test(month)) {
  366. return false;
  367. }
  368. if (!/^\d+$/.test(year)) {
  369. return false;
  370. }
  371. if (!(parseInt(month, 10) <= 12)) {
  372. return false;
  373. }
  374. if (year.length === 2) {
  375. prefix = (new Date).getFullYear();
  376. prefix = prefix.toString().slice(0, 2);
  377. year = prefix + year;
  378. }
  379. expiry = new Date(year, month);
  380. currentTime = new Date;
  381. expiry.setMonth(expiry.getMonth() - 1);
  382. expiry.setMonth(expiry.getMonth() + 1, 1);
  383. return expiry > currentTime;
  384. };
  385.  
  386. $ = jQuery;
  387.  
  388. hasTextSelected = $.formance.fn.hasTextSelected;
  389.  
  390. defaultFormat = /(\d{1,4})/g;
  391.  
  392. cards = [
  393. {
  394. type: 'maestro',
  395. pattern: /^(5018|5020|5038|6304|6759|676[1-3])/,
  396. format: defaultFormat,
  397. length: [12, 13, 14, 15, 16, 17, 18, 19],
  398. cvcLength: [3],
  399. luhn: true
  400. }, {
  401. type: 'dinersclub',
  402. pattern: /^(36|38|30[0-5])/,
  403. format: defaultFormat,
  404. length: [14],
  405. cvcLength: [3],
  406. luhn: true
  407. }, {
  408. type: 'laser',
  409. pattern: /^(6706|6771|6709)/,
  410. format: defaultFormat,
  411. length: [16, 17, 18, 19],
  412. cvcLength: [3],
  413. luhn: true
  414. }, {
  415. type: 'jcb',
  416. pattern: /^35/,
  417. format: defaultFormat,
  418. length: [16],
  419. cvcLength: [3],
  420. luhn: true
  421. }, {
  422. type: 'unionpay',
  423. pattern: /^62/,
  424. format: defaultFormat,
  425. length: [16, 17, 18, 19],
  426. cvcLength: [3],
  427. luhn: false
  428. }, {
  429. type: 'discover',
  430. pattern: /^(6011|65|64[4-9]|622)/,
  431. format: defaultFormat,
  432. length: [16],
  433. cvcLength: [3],
  434. luhn: true
  435. }, {
  436. type: 'mastercard',
  437. pattern: /^5[1-5]/,
  438. format: defaultFormat,
  439. length: [16],
  440. cvcLength: [3],
  441. luhn: true
  442. }, {
  443. type: 'amex',
  444. pattern: /^3[47]/,
  445. format: /(\d{1,4})(\d{1,6})?(\d{1,5})?/,
  446. length: [15],
  447. cvcLength: [3, 4],
  448. luhn: true
  449. }, {
  450. type: 'visa',
  451. pattern: /^4/,
  452. format: defaultFormat,
  453. length: [13, 14, 15, 16],
  454. cvcLength: [3],
  455. luhn: true
  456. }
  457. ];
  458.  
  459. cardFromNumber = function(num) {
  460. var card, _i, _len;
  461.  
  462. num = (num + '').replace(/\D/g, '');
  463. for (_i = 0, _len = cards.length; _i < _len; _i++) {
  464. card = cards[_i];
  465. if (card.pattern.test(num)) {
  466. return card;
  467. }
  468. }
  469. };
  470.  
  471. cardFromType = function(type) {
  472. var card, _i, _len;
  473.  
  474. for (_i = 0, _len = cards.length; _i < _len; _i++) {
  475. card = cards[_i];
  476. if (card.type === type) {
  477. return card;
  478. }
  479. }
  480. };
  481.  
  482. luhnCheck = function(num) {
  483. var digit, digits, odd, sum, _i, _len;
  484.  
  485. odd = true;
  486. sum = 0;
  487. digits = (num + '').split('').reverse();
  488. for (_i = 0, _len = digits.length; _i < _len; _i++) {
  489. digit = digits[_i];
  490. digit = parseInt(digit, 10);
  491. if ((odd = !odd)) {
  492. digit *= 2;
  493. }
  494. if (digit > 9) {
  495. digit -= 9;
  496. }
  497. sum += digit;
  498. }
  499. return sum % 10 === 0;
  500. };
  501.  
  502. restrictCardNumber = function(e) {
  503. var $target, card, digit, value;
  504.  
  505. $target = $(e.currentTarget);
  506. digit = String.fromCharCode(e.which);
  507. if (!/^\d+$/.test(digit)) {
  508. return;
  509. }
  510. if (hasTextSelected($target)) {
  511. return;
  512. }
  513. value = ($target.val() + digit).replace(/\D/g, '');
  514. card = cardFromNumber(value);
  515. if (card) {
  516. return value.length <= card.length[card.length.length - 1];
  517. } else {
  518. return value.length <= 16;
  519. }
  520. };
  521.  
  522. reFormatCardNumber = function(e) {
  523. var _this = this;
  524.  
  525. return setTimeout(function() {
  526. var $target, value;
  527.  
  528. $target = $(e.currentTarget);
  529. value = $target.val();
  530. value = $.formance.formatCardNumber(value);
  531. return $target.val(value);
  532. });
  533. };
  534.  
  535. formatCardNumber = function(e) {
  536. var $target, card, digit, length, re, upperLength, value;
  537.  
  538. digit = String.fromCharCode(e.which);
  539. if (!/^\d+$/.test(digit)) {
  540. return;
  541. }
  542. $target = $(e.currentTarget);
  543. value = $target.val();
  544. card = cardFromNumber(value + digit);
  545. length = (value.replace(/\D/g, '') + digit).length;
  546. upperLength = 16;
  547. if (card) {
  548. upperLength = card.length[card.length.length - 1];
  549. }
  550. if (length >= upperLength) {
  551. return;
  552. }
  553. if (($target.prop('selectionStart') != null) && $target.prop('selectionStart') !== value.length) {
  554. return;
  555. }
  556. if (card && card.type === 'amex') {
  557. re = /^(\d{4}|\d{4}\s\d{6})$/;
  558. } else {
  559. re = /(?:^|\s)(\d{4})$/;
  560. }
  561. if (re.test(value)) {
  562. e.preventDefault();
  563. return $target.val(value + ' ' + digit);
  564. } else if (re.test(value + digit)) {
  565. e.preventDefault();
  566. return $target.val(value + digit + ' ');
  567. }
  568. };
  569.  
  570. formatBackCardNumber = function(e) {
  571. var $target, value;
  572.  
  573. $target = $(e.currentTarget);
  574. value = $target.val();
  575. if (e.meta) {
  576. return;
  577. }
  578. if (e.which !== 8) {
  579. return;
  580. }
  581. if (($target.prop('selectionStart') != null) && $target.prop('selectionStart') !== value.length) {
  582. return;
  583. }
  584. if (/\d\s$/.test(value)) {
  585. e.preventDefault();
  586. return $target.val(value.replace(/\d\s$/, ''));
  587. } else if (/\s\d?$/.test(value)) {
  588. e.preventDefault();
  589. return $target.val(value.replace(/\s\d?$/, ''));
  590. }
  591. };
  592.  
  593. setCardType = function(e) {
  594. var $target, allTypes, card, cardType, val;
  595.  
  596. $target = $(e.currentTarget);
  597. val = $target.val();
  598. cardType = $.formance.creditCardType(val) || 'unknown';
  599. if (!$target.hasClass(cardType)) {
  600. allTypes = (function() {
  601. var _i, _len, _results;
  602.  
  603. _results = [];
  604. for (_i = 0, _len = cards.length; _i < _len; _i++) {
  605. card = cards[_i];
  606. _results.push(card.type);
  607. }
  608. return _results;
  609. })();
  610. $target.removeClass('unknown');
  611. $target.removeClass(allTypes.join(' '));
  612. $target.addClass(cardType);
  613. $target.toggleClass('identified', cardType !== 'unknown');
  614. return $target.trigger('payment.cardType', cardType);
  615. }
  616. };
  617.  
  618. $.formance.creditCardType = function(num) {
  619. var _ref;
  620.  
  621. if (!num) {
  622. return null;
  623. }
  624. return ((_ref = cardFromNumber(num)) != null ? _ref.type : void 0) || null;
  625. };
  626.  
  627. $.formance.formatCreditCardNumber = function(num) {
  628. var card, groups, upperLength, _ref;
  629.  
  630. card = cardFromNumber(num);
  631. if (!card) {
  632. return num;
  633. }
  634. upperLength = card.length[card.length.length - 1];
  635. num = num.replace(/\D/g, '');
  636. num = num.slice(0, +upperLength + 1 || 9e9);
  637. if (card.format.global) {
  638. return (_ref = num.match(card.format)) != null ? _ref.join(' ') : void 0;
  639. } else {
  640. groups = card.format.exec(num);
  641. if (groups != null) {
  642. groups.shift();
  643. }
  644. return groups != null ? groups.join(' ') : void 0;
  645. }
  646. };
  647.  
  648. $.formance.fn.format_credit_card_number = function() {
  649. this.formance('restrictNumeric');
  650. this.on('keypress', restrictCardNumber);
  651. this.on('keypress', formatCardNumber);
  652. this.on('keydown', formatBackCardNumber);
  653. this.on('keyup', setCardType);
  654. this.on('paste', reFormatCardNumber);
  655. return this;
  656. };
  657.  
  658. $.formance.fn.validate_credit_card_number = function() {
  659. var card, num, _ref;
  660.  
  661. num = $(this).val();
  662. num = (num + '').replace(/\s+|-/g, '');
  663. if (!/^\d+$/.test(num)) {
  664. return false;
  665. }
  666. card = cardFromNumber(num);
  667. if (!card) {
  668. return false;
  669. }
  670. return (_ref = num.length, __indexOf.call(card.length, _ref) >= 0) && (card.luhn === false || luhnCheck(num));
  671. };
  672.  
  673. $ = jQuery;
  674.  
  675. hasTextSelected = $.formance.fn.hasTextSelected;
  676.  
  677. restrictDateDDMMYYYY = function(e) {
  678. var $target, digit, value;
  679.  
  680. $target = $(e.currentTarget);
  681. digit = String.fromCharCode(e.which);
  682. if (!/^\d+$/.test(digit)) {
  683. return;
  684. }
  685. if (hasTextSelected($target)) {
  686. return;
  687. }
  688. value = $target.val() + digit;
  689. value = value.replace(/\D/g, '');
  690. if (value.length > 8) {
  691. return false;
  692. }
  693. };
  694.  
  695. formatDateDDMMYYYY = function(e) {
  696. var $target, digit, old_val, val;
  697.  
  698. digit = String.fromCharCode(e.which);
  699. if (!/^\d+$/.test(digit)) {
  700. return;
  701. }
  702. $target = $(e.currentTarget);
  703. old_val = $target.val();
  704. val = old_val + digit;
  705. if (/^\d$/.test(val) && (digit !== '0' && digit !== '1' && digit !== '2' && digit !== '3')) {
  706. e.preventDefault();
  707. return $target.val("0" + val + " / ");
  708. } else if (/^\d{2}$/.test(val)) {
  709. e.preventDefault();
  710. return $target.val("" + val + " / ");
  711. } else if (/^\d{2}\s\/\s\d$/.test(val) && (digit !== '0' && digit !== '1')) {
  712. e.preventDefault();
  713. return $target.val("" + old_val + "0" + digit + " / ");
  714. } else if (/^\d{2}\s\/\s\d{2}$/.test(val)) {
  715. e.preventDefault();
  716. return $target.val("" + val + " / ");
  717. }
  718. };
  719.  
  720. formatForwardDateDDMMYYYY = function(e) {
  721. var $target, digit, val;
  722.  
  723. digit = String.fromCharCode(e.which);
  724. if (!/^\d+$/.test(digit)) {
  725. return;
  726. }
  727. $target = $(e.currentTarget);
  728. val = $target.val();
  729. if (/^\d{2}$/.test(val) || /^\d{2}\s\/\s\d{2}$/.test(val)) {
  730. return $target.val("" + val + " / ");
  731. }
  732. };
  733.  
  734. formatForwardSlashDateDDMMYYYY = function(e) {
  735. var $target, date, day, month, parse_day, parse_month, slash, val, _ref;
  736.  
  737. slash = String.fromCharCode(e.which);
  738. if (slash !== '/') {
  739. return;
  740. }
  741. $target = $(e.currentTarget);
  742. val = $target.val();
  743. parse_day = /^(\d)$/;
  744. parse_month = /^(\d{2})\s\/\s(\d)$/;
  745. if (parse_day.test(val) && val !== '0') {
  746. return $target.val("0" + val + " / ");
  747. } else if (parse_month.test(val)) {
  748. _ref = val.match(parse_month), date = _ref[0], day = _ref[1], month = _ref[2];
  749. if (month !== '0') {
  750. return $target.val("" + day + " / 0" + month + " / ");
  751. }
  752. }
  753. };
  754.  
  755. formatBackDateDDMMYYYY = function(e) {
  756. var $target, value;
  757.  
  758. if (e.meta) {
  759. return;
  760. }
  761. $target = $(e.currentTarget);
  762. value = $target.val();
  763. if (e.which !== 8) {
  764. return;
  765. }
  766. if (($target.prop('selectionStart') != null) && $target.prop('selectionStart') !== value.length) {
  767. return;
  768. }
  769. if (/\d(\s|\/)+$/.test(value)) {
  770. e.preventDefault();
  771. return $target.val(value.replace(/\d(\s|\/)*$/, ''));
  772. } else if (/\s\/\s?\d?$/.test(value)) {
  773. e.preventDefault();
  774. return $target.val(value.replace(/\s\/\s?\d?$/, ''));
  775. }
  776. };
  777.  
  778. $.formance.fn.format_dd_mm_yyyy = function() {
  779. this.formance('restrictNumeric');
  780. this.on('keypress', restrictDateDDMMYYYY);
  781. this.on('keypress', formatDateDDMMYYYY);
  782. this.on('keypress', formatForwardSlashDateDDMMYYYY);
  783. this.on('keypress', formatForwardDateDDMMYYYY);
  784. this.on('keydown', formatBackDateDDMMYYYY);
  785. return this;
  786. };
  787.  
  788. parseDateDDMMYYYY = function(date_string) {
  789. var day, month, year, _ref;
  790.  
  791. _ref = date_string != null ? date_string.replace(/\s/g, '').split('/', 3) : [NaN, NaN, NaN], day = _ref[0], month = _ref[1], year = _ref[2];
  792. if (!((year != null) && year.length === 4)) {
  793. year = NaN;
  794. }
  795. day = parseInt(day, 10);
  796. month = parseInt(month, 10);
  797. year = parseInt(year, 10);
  798. return {
  799. day: day,
  800. month: month,
  801. year: year
  802. };
  803. };
  804.  
  805. $.formance.fn.val_dd_mm_yyyy = function() {
  806. var date;
  807.  
  808. date = parseDateDDMMYYYY(this.val());
  809. if ((date.day == null) || isNaN(date.day)) {
  810. return false;
  811. }
  812. if ((date.month == null) || isNaN(date.month)) {
  813. return false;
  814. }
  815. if ((date.year == null) || isNaN(date.year)) {
  816. return false;
  817. }
  818. return new Date(date.year, date.month - 1, date.day);
  819. };
  820.  
  821. $.formance.fn.validate_dd_mm_yyyy = function() {
  822. var date, date_dict;
  823.  
  824. date_dict = parseDateDDMMYYYY(this.val());
  825. date = this.formance('val_dd_mm_yyyy');
  826. if (!((date != null) && date instanceof Date)) {
  827. return false;
  828. }
  829. if (date.getDate() !== date_dict.day) {
  830. return false;
  831. }
  832. if (date.getMonth() + 1 !== date_dict.month) {
  833. return false;
  834. }
  835. if (date.getFullYear() !== date_dict.year) {
  836. return false;
  837. }
  838. return true;
  839. };
  840.  
  841. $ = jQuery;
  842.  
  843. hasTextSelected = $.formance.fn.hasTextSelected;
  844.  
  845. $.formance.fn.format_email = function() {
  846. return this;
  847. };
  848.  
  849. $.formance.fn.validate_email = function() {
  850. var $this, algorithms, val, validator;
  851.  
  852. algorithms = {
  853. simple: /^\S+@\S+$/,
  854. complex: /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\ ".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA -Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
  855. };
  856. $this = $(this);
  857. val = $this.val();
  858. if (val == null) {
  859. return false;
  860. }
  861. validator = $this.data('formance_algorithm');
  862. if ((validator != null) && validator in algorithms) {
  863. return algorithms[validator].test(val);
  864. }
  865. return algorithms['simple'].test(val);
  866. };
  867.  
  868. $ = jQuery;
  869.  
  870. $.formance.fn.format_number = function() {
  871. var length;
  872.  
  873. length = $(this).data('formance_length');
  874. if (length != null) {
  875. $(this).attr('maxLength', length);
  876. }
  877. this.formance('restrictNumeric');
  878. return this;
  879. };
  880.  
  881. $.formance.fn.validate_number = function() {
  882. var $this, length, val;
  883.  
  884. $this = $(this);
  885. val = $this.val();
  886. length = $this.data('formance_length');
  887. if ((length != null) && (typeof length === 'number') && (val.length !== length)) {
  888. return false;
  889. }
  890. if ((length != null) && typeof length === 'string' && length !== '') {
  891. if (isNaN(parseInt(length, 10))) {
  892. return false;
  893. }
  894. if (val.length !== parseInt(length, 10)) {
  895. return false;
  896. }
  897. }
  898. return /^\d+$/.test(val);
  899. };
  900.  
  901. $ = jQuery;
  902.  
  903. hasTextSelected = $.formance.fn.hasTextSelected;
  904.  
  905. restrictOntarioDriversLicenseNumber = function(e) {
  906. var $target, char, value;
  907.  
  908. $target = $(e.currentTarget);
  909. char = String.fromCharCode(e.which);
  910. if (!/^[a-zA-Z\d]+$/.test(char)) {
  911. return;
  912. }
  913. if (hasTextSelected($target)) {
  914. return;
  915. }
  916. value = $target.val() + char;
  917. value = value.replace(/[^a-zA-Z\d]/g, '');
  918. if (value.length > 15) {
  919. return false;
  920. }
  921. };
  922.  
  923. formatOntarioDriversLicenseNumber = function(e) {
  924. var $target, char, old_val, val;
  925.  
  926. char = String.fromCharCode(e.which);
  927. if (!/^[a-zA-Z\d]+$/.test(char)) {
  928. return;
  929. }
  930. $target = $(e.currentTarget);
  931. old_val = $target.val();
  932. val = old_val + char.toUpperCase();
  933. if (old_val === '') {
  934. e.preventDefault();
  935. if (/^[A-Za-z]$/.test(val)) {
  936. return $target.val(val);
  937. }
  938. } else if (/^[A-Za-z]\d{0,3}$/.test(old_val)) {
  939. e.preventDefault();
  940. if (/^[A-Za-z]\d{4}$/.test(val)) {
  941. val = "" + val + " - ";
  942. }
  943. if (/^[A-Za-z]\d{0,4}[\s|\-]*$/.test(val)) {
  944. return $target.val(val);
  945. }
  946. } else if (/^[A-Za-z]\d{4}[\s|\-]*\d{0,4}$/.test(old_val)) {
  947. e.preventDefault();
  948. if (/^[A-Za-z]\d{4}[\s|\-]*\d{5}$/.test(val)) {
  949. val = "" + val + " - ";
  950. }
  951. if (/^[A-Za-z]\d{4}[\s|\-]*\d{0,5}[\s|\-]*$/.test(val)) {
  952. return $target.val(val);
  953. }
  954. }
  955. };
  956.  
  957. formatBackOntarioDriversLicenseNumber = function(e) {
  958. var $target, value;
  959.  
  960. if (e.meta) {
  961. return;
  962. }
  963. $target = $(e.currentTarget);
  964. value = $target.val();
  965. if (e.which !== 8) {
  966. return;
  967. }
  968. if (($target.prop('selectionStart') != null) && $target.prop('selectionStart') !== value.length) {
  969. return;
  970. }
  971. if (/\d(\s|\-)+$/.test(value)) {
  972. e.preventDefault();
  973. return $target.val(value.replace(/\d(\s|\-)+$/, ''));
  974. }
  975. };
  976.  
  977. formatPasteOntarioDriversLicenseNumber = function(e) {
  978. var _this = this;
  979.  
  980. return setTimeout(function() {
  981. var $target, first5, full, last5, middle5, val, _ref;
  982.  
  983. $target = $(e.currentTarget);
  984. val = $target.val();
  985. _ref = val.match(/^([A-Za-z\d]{5})[\s|\-]*(\d{5})[\s|\-]*(\d{5})$/), full = _ref[0], first5 = _ref[1], middle5 = _ref[2], last5 = _ref[3];
  986. return $target.val("" + first5 + " - " + middle5 + " - " + last5);
  987. });
  988. };
  989.  
  990. $.formance.fn.format_ontario_drivers_license_number = function() {
  991. this.formance('restrictAlphaNumeric');
  992. this.on('keypress', restrictOntarioDriversLicenseNumber);
  993. this.on('keypress', formatOntarioDriversLicenseNumber);
  994. this.on('keydown', formatBackOntarioDriversLicenseNumber);
  995. this.on('paste', formatPasteOntarioDriversLicenseNumber);
  996. return this;
  997. };
  998.  
  999. $.formance.fn.validate_ontario_drivers_license_number = function() {
  1000. var regex, val;
  1001.  
  1002. val = $(this).val();
  1003. if (val == null) {
  1004. return false;
  1005. }
  1006. val = val.replace(/[\s|\-]/g, '');
  1007. if (!/^[a-zA-Z\d]+$/.test()) {
  1008. return false;
  1009. }
  1010. regex = /^[A-Za-z]\d{4}[\s|\-]*\d{5}[\s|\-]*\d{5}$/;
  1011. return regex.test(val);
  1012. };
  1013.  
  1014. $ = jQuery;
  1015.  
  1016. hasTextSelected = $.formance.fn.hasTextSelected;
  1017.  
  1018. restrictOntarioOutdoorsCardNumber = function(e) {
  1019. var $target, digit, value;
  1020.  
  1021. $target = $(e.currentTarget);
  1022. digit = String.fromCharCode(e.which);
  1023. if (!/^\d+$/.test(digit)) {
  1024. return;
  1025. }
  1026. if (hasTextSelected($target)) {
  1027. return;
  1028. }
  1029. value = $target.val() + digit;
  1030. value = value.replace(/\D/g, '');
  1031. if (value.length > 15) {
  1032. return false;
  1033. }
  1034. };
  1035.  
  1036. formatOntarioOutdoorsCardNumber = function(e) {
  1037. var $target, digit, old_val, val;
  1038.  
  1039. digit = String.fromCharCode(e.which);
  1040. if (!/^\d+$/.test(digit)) {
  1041. return;
  1042. }
  1043. $target = $(e.currentTarget);
  1044. old_val = $target.val();
  1045. val = $target.val() + digit;
  1046. if (old_val === '') {
  1047. e.preventDefault();
  1048. val = /^7$/.test(val) ? "708158 " : "708158 " + val;
  1049. return $target.val(val);
  1050. } else if (/^\d{5}$/.test(old_val)) {
  1051. e.preventDefault();
  1052. if (/^\d{6}$/.test(val)) {
  1053. val = "" + val + " ";
  1054. }
  1055. if (/^\d{6}\s*$/.test(val)) {
  1056. return target.val(val);
  1057. }
  1058. }
  1059. };
  1060.  
  1061. formatBackOntarioOutdoorsCardNumber = function(e) {
  1062. var $target, value;
  1063.  
  1064. if (e.meta) {
  1065. return;
  1066. }
  1067. $target = $(e.currentTarget);
  1068. value = $target.val();
  1069. if (e.which !== 8) {
  1070. return;
  1071. }
  1072. if (($target.prop('selectionStart') != null) && $target.prop('selectionStart') !== value.length) {
  1073. return;
  1074. }
  1075. if (/708158\s+$/.test(value)) {
  1076. e.preventDefault();
  1077. return $target.val(value.replace(/708158\s+$/, ''));
  1078. }
  1079. };
  1080.  
  1081. formatPasteOntarioOutdoorsCardNumber = function(e) {
  1082. var _this = this;
  1083.  
  1084. return setTimeout(function() {
  1085. var $target, first6, full, last9, val, _ref;
  1086.  
  1087. $target = $(e.currentTarget);
  1088. val = $target.val();
  1089. _ref = val.match(/^(\d{6})\s*(\d{9})$/), full = _ref[0], first6 = _ref[1], last9 = _ref[2];
  1090. return $target.val("" + first6 + " " + last9);
  1091. });
  1092. };
  1093.  
  1094. $.formance.fn.format_ontario_outdoors_card_number = function() {
  1095. this.formance('restrictNumeric');
  1096. this.on('keypress', restrictOntarioOutdoorsCardNumber);
  1097. this.on('keypress', formatOntarioOutdoorsCardNumber);
  1098. this.on('keydown', formatBackOntarioOutdoorsCardNumber);
  1099. this.on('paste', formatPasteOntarioOutdoorsCardNumber);
  1100. return this;
  1101. };
  1102.  
  1103. $.formance.fn.validate_ontario_outdoors_card_number = function() {
  1104. var regex, val;
  1105.  
  1106. val = $(this).val();
  1107. if (val == null) {
  1108. return false;
  1109. }
  1110. val = val.replace(/\s/g, '');
  1111. if (!/^\d+$/.test(val)) {
  1112. return false;
  1113. }
  1114. regex = /^708158\s*\d{9}$/;
  1115. return regex.test(val);
  1116. };
  1117.  
  1118. $ = jQuery;
  1119.  
  1120. hasTextSelected = $.formance.fn.hasTextSelected;
  1121.  
  1122. restrictOntarioPhotoHealthCardNumber = function(e) {
  1123. var $target, char, value;
  1124.  
  1125. $target = $(e.currentTarget);
  1126. char = String.fromCharCode(e.which);
  1127. if (!/^[a-zA-Z\d]+$/.test(char)) {
  1128. return;
  1129. }
  1130. if (hasTextSelected($target)) {
  1131. return;
  1132. }
  1133. value = $target.val() + char;
  1134. value = value.replace(/[^a-zA-Z\d]/g, '');
  1135. if (value.length > 12) {
  1136. return false;
  1137. }
  1138. };
  1139.  
  1140. formatOntarioPhotoHealthCardNumber = function(e) {
  1141. var $target, char, old_val, val;
  1142.  
  1143. char = String.fromCharCode(e.which);
  1144. if (!/^[a-zA-Z\d]+$/.test(char)) {
  1145. return;
  1146. }
  1147. $target = $(e.currentTarget);
  1148. old_val = $target.val();
  1149. val = old_val + char.toUpperCase();
  1150. if (/^\d{0,3}$/.test(old_val)) {
  1151. e.preventDefault();
  1152. if (/^\d{4}$/.test(val)) {
  1153. val = "" + val + " - ";
  1154. }
  1155. if (/^\d{0,4}[\s|\-]*$/.test(val)) {
  1156. return $target.val(val);
  1157. }
  1158. } else if (/^\d{4}[\s|\-]*\d{0,2}$/.test(old_val)) {
  1159. e.preventDefault();
  1160. if (/^\d{4}[\s|\-]*\d{3}$/.test(val)) {
  1161. val = "" + val + " - ";
  1162. }
  1163. if (/^\d{4}[\s|\-]*\d{0,3}[\s|\-]*$/.test(val)) {
  1164. return $target.val(val);
  1165. }
  1166. } else if (/^\d{4}[\s|\-]*\d{3}[\s|\-]*\d{0,2}$/.test(old_val)) {
  1167. e.preventDefault();
  1168. if (/^\d{4}[\s|\-]*\d{3}[\s|\-]*\d{3}$/.test(val)) {
  1169. val = "" + val + " - ";
  1170. }
  1171. if (/^\d{4}[\s|\-]*\d{3}[\s|\-]*\d{0,3}[\s|\-]*$/.test(val)) {
  1172. return $target.val(val);
  1173. }
  1174. } else if (/^\d{4}[\s|\-]*\d{3}[\s|\-]*\d{3}[\s|\-]*[A-Za-z]{0,1}$/.test(old_val)) {
  1175. e.preventDefault();
  1176. if (/^\d{4}[\s|\-]*\d{3}[\s|\-]*\d{3}[\s|\-]*[A-Za-z]{0,2}$/.test(val)) {
  1177. return $target.val(val);
  1178. }
  1179. }
  1180. };
  1181.  
  1182. formatBackOntarioPhotoHealthCardNumber = function(e) {
  1183. var $target, value;
  1184.  
  1185. if (e.meta) {
  1186. return;
  1187. }
  1188. $target = $(e.currentTarget);
  1189. value = $target.val();
  1190. if (e.which !== 8) {
  1191. return;
  1192. }
  1193. if (($target.prop('selectionStart') != null) && $target.prop('selectionStart') !== value.length) {
  1194. return;
  1195. }
  1196. if (/\d(\s|\-)+$/.test(value)) {
  1197. e.preventDefault();
  1198. return $target.val(value.replace(/\d(\s|\-)+$/, ''));
  1199. }
  1200. };
  1201.  
  1202. formatPasteOntarioPhotoHealthCardNumber = function(e) {
  1203. var _this = this;
  1204.  
  1205. return setTimeout(function() {
  1206. var $target, first4, full, last2, second3, third3, val, _ref;
  1207.  
  1208. $target = $(e.currentTarget);
  1209. val = $target.val();
  1210. _ref = val.match(/^(\d{4})[\s|\-]*(\d{3})[\s|\-]*(\d{3})[\s|\-]*([A-Za-z]{2})$/), full = _ref[0], first4 = _ref[1], second3 = _ref[2], third3 = _ref[3], last2 = _ref[4];
  1211. return $target.val("" + first4 + " - " + second3 + " - " + third3 + " - " + last2);
  1212. });
  1213. };
  1214.  
  1215. $.formance.fn.format_ontario_photo_health_card_number = function() {
  1216. this.formance('restrictAlphaNumeric');
  1217. this.on('keypress', restrictOntarioPhotoHealthCardNumber);
  1218. this.on('keypress', formatOntarioPhotoHealthCardNumber);
  1219. this.on('keydown', formatBackOntarioPhotoHealthCardNumber);
  1220. this.on('paste', formatPasteOntarioPhotoHealthCardNumber);
  1221. return this;
  1222. };
  1223.  
  1224. $.formance.fn.validate_ontario_photo_health_card_number = function() {
  1225. var regex, val;
  1226.  
  1227. val = $(this).val();
  1228. if (val == null) {
  1229. return false;
  1230. }
  1231. val = val.replace(/[\s|\-]/g, '');
  1232. if (!/^[a-zA-Z\d]+$/.test()) {
  1233. return false;
  1234. }
  1235. regex = /^\d{4}[\s|\-]*\d{3}[\s|\-]*\d{3}[\s|\-]*[A-Za-z]{2}$/;
  1236. return regex.test(val);
  1237. };
  1238.  
  1239. $ = jQuery;
  1240.  
  1241. hasTextSelected = $.formance.fn.hasTextSelected;
  1242.  
  1243. reFormatPhoneNumber = function(phoneNumberString) {
  1244. var areaCode, first3, last4, phoneNumber, text, _ref;
  1245. phoneNumber = phoneNumberString.replace(/\D/g, '').match(/^(\d{0,2})?(\d{0,4})?(\d{0,5})?$/);
  1246. _ref = phoneNumber, phoneNumber = _ref[0], areaCode = _ref[1], first3 = _ref[2], last4 = _ref[3];
  1247. text = '';
  1248. if (areaCode != null) {
  1249. text += "(" + areaCode;
  1250. }
  1251. if ((areaCode != null ? areaCode.length : void 0) === 2) {
  1252. text += ") ";
  1253. }
  1254. if (first3 != null) {
  1255. text += "" + (last4 != undefined ? (last4.length == 5 ? first3+last4.charAt(0) : first3) : first3);
  1256. }
  1257. if ((first3 != null ? first3.length : void 0) === 4) {
  1258. text += "-";
  1259. }
  1260. if (last4 != null) {
  1261. text += "" + ((last4.length == 5) ? last4.substr(1) : last4);
  1262. }
  1263. return text;
  1264. };
  1265.  
  1266. restrictPhoneNumber = function(e) {
  1267. var $target, digit, value;
  1268.  
  1269. $target = $(e.currentTarget);
  1270. digit = String.fromCharCode(e.which);
  1271. if (!/^\d+$/.test(digit)) {
  1272. return;
  1273. }
  1274. if (hasTextSelected($target)) {
  1275. return;
  1276. }
  1277. value = $target.val() + digit;
  1278. value = value.replace(/\D/g, '');
  1279. if (value.length > 11) {
  1280. return false;
  1281. }
  1282. };
  1283.  
  1284. formatPhoneNumber = function(e) {
  1285. var $target, digit, text, val;
  1286.  
  1287. digit = String.fromCharCode(e.which);
  1288. if (!/^\d+$/.test(digit)) {
  1289. return;
  1290. }
  1291.  
  1292. $target = $(e.currentTarget);
  1293. val = $target.val() + digit;
  1294. if (val.replace(/\D/g, '').length > 11) {
  1295. return;
  1296. }
  1297. text = reFormatPhoneNumber(val);
  1298. e.preventDefault();
  1299. return $target.val(text);
  1300. };
  1301.  
  1302. formatBackPhoneNumber = function(e) {
  1303. var $target, value;
  1304.  
  1305. if (e.meta) {
  1306. return;
  1307. }
  1308. $target = $(e.currentTarget);
  1309. value = $target.val();
  1310. if (e.which !== 8) {
  1311. return;
  1312. }
  1313. if (($target.prop('selectionStart') != null) && $target.prop('selectionStart') !== value.length) {
  1314. return;
  1315. }
  1316. if (/\(\d$/.test(value)) {
  1317. e.preventDefault();
  1318. return $target.val('');
  1319. } else if (/\d\)(\s)+$/.test(value)) {
  1320. e.preventDefault();
  1321. return $target.val(value.replace(/\d\)(\s)*$/, ''));
  1322. } else if (/\d(\s|\-)+$/.test(value)) {
  1323. e.preventDefault();
  1324. return $target.val(value.replace(/\d(\s|\-)+$/, ''));
  1325. }
  1326. };
  1327.  
  1328. formatPastePhoneNumber = function(e) {
  1329. var _this = this;
  1330.  
  1331. return setTimeout(function() {
  1332. var $target, text, val;
  1333.  
  1334. $target = $(e.currentTarget);
  1335. val = $target.val();
  1336. text = reFormatPhoneNumber(val);
  1337. return $target.val(text);
  1338. });
  1339. };
  1340.  
  1341. $.formance.fn.format_phone_number = function() {
  1342. this.formance('restrictNumeric');
  1343. this.on('keypress', restrictPhoneNumber);
  1344. this.on('keypress', formatPhoneNumber);
  1345. this.on('keydown', formatBackPhoneNumber);
  1346. this.on('paste', formatPastePhoneNumber);
  1347. return this;
  1348. };
  1349.  
  1350. $.formance.fn.validate_phone_number = function() {
  1351. var val;
  1352.  
  1353. val = $(this).val();
  1354. if (val == null) {
  1355. return false;
  1356. }
  1357. val = val.replace(/\(|\)|\s+|-/g, '');
  1358. if (!/^\d+$/.test(val)) {
  1359. return false;
  1360. }
  1361. return (val.replace(/\D/g, '').length === 10 || val.replace(/\D/g, '').length === 11);
  1362. };
  1363.  
  1364. $ = jQuery;
  1365.  
  1366. hasTextSelected = $.formance.fn.hasTextSelected;
  1367.  
  1368. restrictPostalCode = function(e) {
  1369. var $target, char, value;
  1370.  
  1371. $target = $(e.currentTarget);
  1372. char = String.fromCharCode(e.which);
  1373. if (!/^[a-zA-Z\d]+$/.test(char)) {
  1374. return;
  1375. }
  1376. if (hasTextSelected($target)) {
  1377. return;
  1378. }
  1379. value = $target.val() + char;
  1380. value = value.replace(/[^a-zA-Z\d]/g, '');
  1381. if (value.length > 6) {
  1382. return false;
  1383. }
  1384. };
  1385.  
  1386. formatPostalCode = function(e) {
  1387. var $target, char, old_val, val;
  1388.  
  1389. char = String.fromCharCode(e.which);
  1390. if (!/^[a-zA-Z\d]+$/.test(char)) {
  1391. return;
  1392. }
  1393. $target = $(e.currentTarget);
  1394. old_val = $target.val();
  1395. val = old_val + char.toUpperCase();
  1396. if (old_val === '') {
  1397. e.preventDefault();
  1398. if (/^[ABCEFGHJKLMNPRSTVXY]$/.test(val)) {
  1399. return $target.val(val);
  1400. }
  1401. } else if (/^[ABCEFGHJKLMNPRSTVXY]$/.test(old_val)) {
  1402. e.preventDefault();
  1403. if (/^[ABCEFGHJKLMNPRSTVXY][0-9]$/.test(val)) {
  1404. return $target.val(val);
  1405. }
  1406. } else if (/^[ABCEFGHJKLMNPRSTVXY][0-9]$/.test(old_val)) {
  1407. e.preventDefault();
  1408. if (/^[ABCEFGHJKLMNPRSTVXY][0-9][ABCEFGHJKLMNPRSTVWXYZ]$/.test(val)) {
  1409. return $target.val("" + val + " ");
  1410. }
  1411. } else if (/^[ABCEFGHJKLMNPRSTVXY][0-9][ABCEFGHJKLMNPRSTVWXYZ]\s$/.test(old_val)) {
  1412. e.preventDefault();
  1413. if (/^[ABCEFGHJKLMNPRSTVXY][0-9][ABCEFGHJKLMNPRSTVWXYZ]\s?[0-9]$/.test(val)) {
  1414. return $target.val(val);
  1415. }
  1416. } else if (/^[ABCEFGHJKLMNPRSTVXY][0-9][ABCEFGHJKLMNPRSTVWXYZ]\s?[0-9]$/.test(old_val)) {
  1417. e.preventDefault();
  1418. if (/^[ABCEFGHJKLMNPRSTVXY][0-9][ABCEFGHJKLMNPRSTVWXYZ]\s?[0-9][ABCEFGHJKLMNPRSTVWXYZ]$/.test(val)) {
  1419. return $target.val(val);
  1420. }
  1421. } else if (/^[ABCEFGHJKLMNPRSTVXY][0-9][ABCEFGHJKLMNPRSTVWXYZ]\s?[0-9][ABCEFGHJKLMNPRSTVWXYZ]$/.test(old_val)) {
  1422. e.preventDefault();
  1423. if (/^[ABCEFGHJKLMNPRSTVXY][0-9][ABCEFGHJKLMNPRSTVWXYZ]\s?[0-9][ABCEFGHJKLMNPRSTVWXYZ][0-9]$/.test(val)) {
  1424. return $target.val(val);
  1425. }
  1426. }
  1427. };
  1428.  
  1429. formatBackPostalCode = function(e) {
  1430. var $target, value;
  1431.  
  1432. if (e.meta) {
  1433. return;
  1434. }
  1435. $target = $(e.currentTarget);
  1436. value = $target.val();
  1437. if (e.which !== 8) {
  1438. return;
  1439. }
  1440. if (($target.prop('selectionStart') != null) && $target.prop('selectionStart') !== value.length) {
  1441. return;
  1442. }
  1443. if (/[ABCEFGHJKLMNPRSTVWXYZ](\s)+$/.test(value)) {
  1444. e.preventDefault();
  1445. return $target.val(value.replace(/[ABCEFGHJKLMNPRSTVWXYZ](\s)*$/, ''));
  1446. }
  1447. };
  1448.  
  1449. formatPastePostalCode = function(e) {
  1450. var _this = this;
  1451.  
  1452. return setTimeout(function() {
  1453. var $target, first_part, full, second_part, val, _ref;
  1454.  
  1455. $target = $(e.currentTarget);
  1456. val = $target.val();
  1457. _ref = val.match(/^([ABCEFGHJKLMNPRSTVXY][0-9][ABCEFGHJKLMNPRSTVWXYZ])\s?([0-9][ABCEFGHJKLMNPRSTVWXYZ][0-9])$/), full = _ref[0], first_part = _ref[1], second_part = _ref[2];
  1458. return $target.val("" + first_part + " " + second_part);
  1459. });
  1460. };
  1461.  
  1462. $.formance.fn.format_postal_code = function() {
  1463. this.formance('restrictAlphaNumeric');
  1464. this.on('keypress', restrictPostalCode);
  1465. this.on('keypress', formatPostalCode);
  1466. this.on('keydown', formatBackPostalCode);
  1467. this.on('paste', formatPastePostalCode);
  1468. return this;
  1469. };
  1470.  
  1471. $.formance.fn.validate_postal_code = function() {
  1472. var val;
  1473.  
  1474. val = $(this).val();
  1475. if (val == null) {
  1476. return false;
  1477. }
  1478. val = val.replace(/\s+/g, '');
  1479. if (!/^[a-zA-Z\d]+$/.test(val)) {
  1480. return false;
  1481. }
  1482. val = val.replace(/[^a-zA-Z\d]/g, '');
  1483. return /^[ABCEFGHJKLMNPRSTVXY][0-9][ABCEFGHJKLMNPRSTVWXYZ]\s?[0-9][ABCEFGHJKLMNPRSTVWXYZ][0-9]$/.test(val.toUpperCase());
  1484. };
  1485.  
  1486. $ = jQuery;
  1487.  
  1488. hasTextSelected = $.formance.fn.hasTextSelected;
  1489.  
  1490. restrictTimeYYMM = function(e) {
  1491. var $target, digit, value;
  1492.  
  1493. $target = $(e.currentTarget);
  1494. digit = String.fromCharCode(e.which);
  1495. if (!/^\d+$/.test(digit)) {
  1496. return;
  1497. }
  1498. if (hasTextSelected($target)) {
  1499. return;
  1500. }
  1501. value = $target.val() + digit;
  1502. value = value.replace(/\D/g, '');
  1503. if (value.length > 4) {
  1504. return false;
  1505. }
  1506. };
  1507.  
  1508. formatTimeYYMM = function(e) {
  1509. var $target, digit, old_val, val;
  1510.  
  1511. digit = String.fromCharCode(e.which);
  1512. if (!/^\d+$/.test(digit)) {
  1513. return;
  1514. }
  1515. $target = $(e.currentTarget);
  1516. old_val = $target.val();
  1517. val = old_val + digit;
  1518. if (/^\d{2}$/.test(val)) {
  1519. e.preventDefault();
  1520. return $target.val("" + val + " / ");
  1521. } else if (/^\d{2}\s\/\s\d{1}$/.test(val) && (digit !== '0' && digit !== '1')) {
  1522. e.preventDefault();
  1523. return $target.val("" + old_val + "0" + digit);
  1524. }
  1525. };
  1526.  
  1527. formatForwardTimeYYMM = function(e) {
  1528. var $target, digit, val;
  1529.  
  1530. digit = String.fromCharCode(e.which);
  1531. if (!/^\d+$/.test(digit)) {
  1532. return;
  1533. }
  1534. $target = $(e.currentTarget);
  1535. val = $target.val();
  1536. if (/^\d{2}$/.test(val)) {
  1537. return $target.val("" + val + " / ");
  1538. }
  1539. };
  1540.  
  1541. formatForwardSlashTimeYYMM = function(e) {
  1542. var $target, parse_year, slash, val;
  1543.  
  1544. slash = String.fromCharCode(e.which);
  1545. if (slash !== '/') {
  1546. return;
  1547. }
  1548. $target = $(e.currentTarget);
  1549. val = $target.val();
  1550. parse_year = /^(\d)$/;
  1551. if (parse_year.test(val) && val.length === 2 || val.length === 1) {
  1552. return $target.val("0" + val + " / ");
  1553. }
  1554. };
  1555.  
  1556. formatBackTimeYYMM = function(e) {
  1557. var $target, value;
  1558.  
  1559. if (e.meta) {
  1560. return;
  1561. }
  1562. $target = $(e.currentTarget);
  1563. value = $target.val();
  1564. if (e.which !== 8) {
  1565. return;
  1566. }
  1567. if (($target.prop('selectionStart') != null) && $target.prop('selectionStart') !== value.length) {
  1568. return;
  1569. }
  1570. if (/\d(\s|\/)+$/.test(value)) {
  1571. e.preventDefault();
  1572. return $target.val(value.replace(/\d(\s|\/)*$/, ''));
  1573. } else if (/\s\/\s?\d?$/.test(value)) {
  1574. e.preventDefault();
  1575. return $target.val(value.replace(/\s\/\s?\d?$/, ''));
  1576. }
  1577. };
  1578.  
  1579. $.formance.fn.format_time_yy_mm = function() {
  1580. this.formance("restrictNumeric");
  1581. this.on("keypress", restrictTimeYYMM);
  1582. this.on("keypress", formatTimeYYMM);
  1583. this.on("keypress", formatForwardTimeYYMM);
  1584. this.on("keypress", formatForwardSlashTimeYYMM);
  1585. this.on("keydown", formatBackTimeYYMM);
  1586. return this;
  1587. };
  1588.  
  1589. parseTimeYYMM = function(time_string) {
  1590. var months, years, _ref;
  1591.  
  1592. _ref = time_string != null ? time_string.replace(/\s/g, '').split('/', 2) : [NaN, NaN], years = _ref[0], months = _ref[1];
  1593. months = parseInt(months, 10);
  1594. years = parseInt(years, 10);
  1595. return {
  1596. years: years,
  1597. months: months
  1598. };
  1599. };
  1600.  
  1601. $.formance.fn.val_time_yy_mm = function() {
  1602. var time;
  1603.  
  1604. time = parseTimeYYMM(this.val());
  1605. if ((time.years == null) || isNaN(time.years)) {
  1606. return false;
  1607. }
  1608. if ((time.months == null) || isNaN(time.months)) {
  1609. return false;
  1610. }
  1611. return time;
  1612. };
  1613.  
  1614. $.formance.fn.validate_time_yy_mm = function() {
  1615. var time, time_dict, time_yymm;
  1616.  
  1617. time_dict = parseTimeYYMM(this.val());
  1618. time = this.formance("val_time_yy_mm");
  1619. time_yymm = $(this).val();
  1620. if (time.months !== time_dict.months) {
  1621. return false;
  1622. }
  1623. if (time.years !== time_dict.years) {
  1624. return false;
  1625. }
  1626. if (/^(\d{1}[\d{1}]*)[\s\/]*(\d{1}[\d{1}]*)[\s\/]*$/.test(time_yymm)) {
  1627. return true;
  1628. }
  1629. return false;
  1630. };
  1631.  
  1632. $ = jQuery;
  1633.  
  1634. hasTextSelected = $.formance.fn.hasTextSelected;
  1635.  
  1636. restrictUkSortCode = function(e) {
  1637. var $target, digit, value;
  1638.  
  1639. $target = $(e.currentTarget);
  1640. digit = String.fromCharCode(e.which);
  1641. if (!/^\d+$/.test(digit)) {
  1642. return;
  1643. }
  1644. if (hasTextSelected($target)) {
  1645. return;
  1646. }
  1647. value = $target.val() + digit;
  1648. value = value.replace(/\D/g, '');
  1649. if (value.length > 6) {
  1650. return false;
  1651. }
  1652. };
  1653.  
  1654. formatForwardUkSortCode = function(e) {
  1655. var $target, digit, val;
  1656.  
  1657. digit = String.fromCharCode(e.which);
  1658. if (!/^\d+$/.test(digit)) {
  1659. return;
  1660. }
  1661. $target = $(e.currentTarget);
  1662. val = $target.val();
  1663. if (/^\d{2}$/.test(val) || /^\d{2}\s\-\s\d{2}$/.test(val)) {
  1664. return $target.val("" + val + " - ");
  1665. }
  1666. };
  1667.  
  1668. formatBackUkSortCode = function(e) {
  1669. var $target, value;
  1670.  
  1671. if (e.meta) {
  1672. return;
  1673. }
  1674. $target = $(e.currentTarget);
  1675. value = $target.val();
  1676. if (e.which !== 8) {
  1677. return;
  1678. }
  1679. if (($target.prop('selectionStart') != null) && $target.prop('selectionStart') !== value.length) {
  1680. return;
  1681. }
  1682. if (/\d(\s|\-)+$/.test(value)) {
  1683. e.preventDefault();
  1684. return $target.val(value.replace(/\d(\s|\-)+$/, ''));
  1685. } else if (/\s\-\s?\d?$/.test(value)) {
  1686. e.preventDefault();
  1687. return $target.val(value.replace(/\s\-\s?\d?$/, ''));
  1688. }
  1689. };
  1690.  
  1691. formatUkSortCode = function(e) {
  1692. var $target, digit, old_val, val;
  1693.  
  1694. digit = String.fromCharCode(e.which);
  1695. if (!/^\d+$/.test(digit)) {
  1696. return;
  1697. }
  1698. $target = $(e.currentTarget);
  1699. old_val = $target.val();
  1700. val = old_val + digit;
  1701. if (/^\d{2}$/.test(val)) {
  1702. e.preventDefault();
  1703. return $target.val("" + val + " - ");
  1704. } else if (/^\d{2}\s\-\s\d{2}$/.test(val)) {
  1705. e.preventDefault();
  1706. return $target.val("" + val + " - ");
  1707. }
  1708. };
  1709.  
  1710. $.formance.fn.format_uk_sort_code = function(e) {
  1711. this.formance("restrictNumeric");
  1712. this.on("keypress", restrictUkSortCode);
  1713. this.on("keypress", formatUkSortCode);
  1714. this.on("keypress", formatForwardUkSortCode);
  1715. this.on("keydown", formatBackUkSortCode);
  1716. return this;
  1717. };
  1718.  
  1719. $.formance.fn.validate_uk_sort_code = function() {
  1720. var sortCode;
  1721.  
  1722. sortCode = $(this).val();
  1723. if (/^(\d{2})[\s\-]*(\d{2})[\s\-]*(\d{2})[\s]*$/.test(sortCode)) {
  1724. return true;
  1725. }
  1726. return false;
  1727. };
  1728.  
  1729. $ = jQuery;
  1730.  
  1731. hasTextSelected = $.formance.fn.hasTextSelected;
  1732.  
  1733. restrictDateYYYYMMDD = function(e) {
  1734. var $target, digit, value;
  1735.  
  1736. $target = $(e.currentTarget);
  1737. digit = String.fromCharCode(e.which);
  1738. if (!/^\d+$/.test(digit)) {
  1739. return;
  1740. }
  1741. if (hasTextSelected($target)) {
  1742. return;
  1743. }
  1744. value = $target.val() + digit;
  1745. value = value.replace(/\D/g, '');
  1746. if (value.length > 8) {
  1747. return false;
  1748. }
  1749. };
  1750.  
  1751. formatDateYYYYMMDD = function(e) {
  1752. var $target, digit, old_val, val;
  1753.  
  1754. digit = String.fromCharCode(e.which);
  1755. if (!/^\d+$/.test(digit)) {
  1756. return;
  1757. }
  1758. $target = $(e.currentTarget);
  1759. old_val = $target.val();
  1760. val = old_val + digit;
  1761. if (/^\d{4}$/.test(val)) {
  1762. e.preventDefault();
  1763. return $target.val("" + val + " / ");
  1764. } else if (/^\d{4}\s\/\s\d$/.test(val) && (digit !== '0' && digit !== '1')) {
  1765. e.preventDefault();
  1766. return $target.val("" + old_val + "0" + digit + " / ");
  1767. } else if (/^\d{4}\s\/\s\d{2}$/.test(val)) {
  1768. e.preventDefault();
  1769. return $target.val("" + val + " / ");
  1770. } else if (/^\d{4}\s\/\s\d{2}\s\/\s\d$/.test(val) && (digit !== '0' && digit !== '1' && digit !== '2' && digit !== '3')) {
  1771. e.preventDefault();
  1772. return $target.val("" + old_val + "0" + digit);
  1773. }
  1774. };
  1775.  
  1776. formatForwardDateYYYYMMDD = function(e) {
  1777. var $target, digit, val;
  1778.  
  1779. digit = String.fromCharCode(e.which);
  1780. if (!/^\d+$/.test(digit)) {
  1781. return;
  1782. }
  1783. $target = $(e.currentTarget);
  1784. val = $target.val();
  1785. if (/^\d{4}$/.test(val) || /^\d{4}\s\/\s\d{2}$/.test(val)) {
  1786. return $target.val("" + val + " / ");
  1787. }
  1788. };
  1789.  
  1790. formatForwardSlashDateYYYYMMDD = function(e) {
  1791. var $target, date, month, parse_month, slash, val, year, _ref;
  1792.  
  1793. slash = String.fromCharCode(e.which);
  1794. if (slash !== '/') {
  1795. return;
  1796. }
  1797. $target = $(e.currentTarget);
  1798. val = $target.val();
  1799. parse_month = /^(\d{4})\s\/\s(\d)$/;
  1800. if (parse_month.test(val)) {
  1801. _ref = val.match(parse_month), date = _ref[0], year = _ref[1], month = _ref[2];
  1802. if (month !== '0') {
  1803. return $target.val("" + year + " / 0" + month + " / ");
  1804. }
  1805. }
  1806. };
  1807.  
  1808. formatBackDateYYYYMMDD = function(e) {
  1809. var $target, value;
  1810.  
  1811. if (e.meta) {
  1812. return;
  1813. }
  1814. $target = $(e.currentTarget);
  1815. value = $target.val();
  1816. if (e.which !== 8) {
  1817. return;
  1818. }
  1819. if (($target.prop('selectionStart') != null) && $target.prop('selectionStart') !== value.length) {
  1820. return;
  1821. }
  1822. if (/\d(\s|\/)+$/.test(value)) {
  1823. e.preventDefault();
  1824. return $target.val(value.replace(/\d(\s|\/)*$/, ''));
  1825. } else if (/\s\/\s?\d?$/.test(value)) {
  1826. e.preventDefault();
  1827. return $target.val(value.replace(/\s\/\s?\d?$/, ''));
  1828. }
  1829. };
  1830.  
  1831. $.formance.fn.format_yyyy_mm_dd = function() {
  1832. this.formance('restrictNumeric');
  1833. this.on('keypress', restrictDateYYYYMMDD);
  1834. this.on('keypress', formatDateYYYYMMDD);
  1835. this.on('keypress', formatForwardSlashDateYYYYMMDD);
  1836. this.on('keypress', formatForwardDateYYYYMMDD);
  1837. this.on('keydown', formatBackDateYYYYMMDD);
  1838. return this;
  1839. };
  1840.  
  1841. parse_date_yyyy_mm_dd = function(date_string) {
  1842. var day, month, year, _ref;
  1843.  
  1844. _ref = date_string != null ? date_string.replace(/\s/g, '').split('/', 3) : [NaN, NaN, NaN], year = _ref[0], month = _ref[1], day = _ref[2];
  1845. if (!((year != null) && year.length === 4)) {
  1846. year = NaN;
  1847. }
  1848. day = parseInt(day, 10);
  1849. month = parseInt(month, 10);
  1850. year = parseInt(year, 10);
  1851. return {
  1852. day: day,
  1853. month: month,
  1854. year: year
  1855. };
  1856. };
  1857.  
  1858. $.formance.fn.val_yyyy_mm_dd = function() {
  1859. var date;
  1860.  
  1861. date = parse_date_yyyy_mm_dd(this.val());
  1862. if ((date.day == null) || isNaN(date.day)) {
  1863. return false;
  1864. }
  1865. if ((date.month == null) || isNaN(date.month)) {
  1866. return false;
  1867. }
  1868. if ((date.year == null) || isNaN(date.year)) {
  1869. return false;
  1870. }
  1871. return new Date(date.year, date.month - 1, date.day);
  1872. };
  1873.  
  1874. $.formance.fn.validate_yyyy_mm_dd = function() {
  1875. var date, date_dict;
  1876.  
  1877. date_dict = parse_date_yyyy_mm_dd(this.val());
  1878. date = this.formance('val_yyyy_mm_dd');
  1879. if (!((date != null) && date instanceof Date)) {
  1880. return false;
  1881. }
  1882. if (date.getDate() !== date_dict.day) {
  1883. return false;
  1884. }
  1885. if (date.getMonth() + 1 !== date_dict.month) {
  1886. return false;
  1887. }
  1888. if (date.getFullYear() !== date_dict.year) {
  1889. return false;
  1890. }
  1891. return true;
  1892. };
  1893.  
  1894. }).call(this);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement