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.

124 lines
7.9KB

  1. /*!
  2. * jquery.inputmask.regex.extensions.js
  3. * http://github.com/RobinHerbots/jquery.inputmask
  4. * Copyright (c) 2010 - 2015 Robin Herbots
  5. * Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
  6. * Version: 3.1.61
  7. */
  8. !function(factory) {
  9. "function" == typeof define && define.amd ? define([ "jquery", "./jquery.inputmask" ], factory) : factory(jQuery);
  10. }(function($) {
  11. return $.extend($.inputmask.defaults.aliases, {
  12. Regex: {
  13. mask: "r",
  14. greedy: !1,
  15. repeat: "*",
  16. regex: null,
  17. regexTokens: null,
  18. tokenizer: /\[\^?]?(?:[^\\\]]+|\\[\S\s]?)*]?|\\(?:0(?:[0-3][0-7]{0,2}|[4-7][0-7]?)?|[1-9][0-9]*|x[0-9A-Fa-f]{2}|u[0-9A-Fa-f]{4}|c[A-Za-z]|[\S\s]?)|\((?:\?[:=!]?)?|(?:[?*+]|\{[0-9]+(?:,[0-9]*)?\})\??|[^.?*+^${[()|\\]+|./g,
  19. quantifierFilter: /[0-9]+[^,]/,
  20. isComplete: function(buffer, opts) {
  21. return new RegExp(opts.regex).test(buffer.join(""));
  22. },
  23. definitions: {
  24. r: {
  25. validator: function(chrs, maskset, pos, strict, opts) {
  26. function regexToken(isGroup, isQuantifier) {
  27. this.matches = [], this.isGroup = isGroup || !1, this.isQuantifier = isQuantifier || !1,
  28. this.quantifier = {
  29. min: 1,
  30. max: 1
  31. }, this.repeaterPart = void 0;
  32. }
  33. function analyseRegex() {
  34. var match, m, currentToken = new regexToken(), opengroups = [];
  35. for (opts.regexTokens = []; match = opts.tokenizer.exec(opts.regex); ) switch (m = match[0],
  36. m.charAt(0)) {
  37. case "(":
  38. opengroups.push(new regexToken(!0));
  39. break;
  40. case ")":
  41. var groupToken = opengroups.pop();
  42. opengroups.length > 0 ? opengroups[opengroups.length - 1].matches.push(groupToken) : currentToken.matches.push(groupToken);
  43. break;
  44. case "{":
  45. case "+":
  46. case "*":
  47. var quantifierToken = new regexToken(!1, !0);
  48. m = m.replace(/[{}]/g, "");
  49. var mq = m.split(","), mq0 = isNaN(mq[0]) ? mq[0] : parseInt(mq[0]), mq1 = 1 == mq.length ? mq0 : isNaN(mq[1]) ? mq[1] : parseInt(mq[1]);
  50. if (quantifierToken.quantifier = {
  51. min: mq0,
  52. max: mq1
  53. }, opengroups.length > 0) {
  54. var matches = opengroups[opengroups.length - 1].matches;
  55. if (match = matches.pop(), !match.isGroup) {
  56. var groupToken = new regexToken(!0);
  57. groupToken.matches.push(match), match = groupToken;
  58. }
  59. matches.push(match), matches.push(quantifierToken);
  60. } else {
  61. if (match = currentToken.matches.pop(), !match.isGroup) {
  62. var groupToken = new regexToken(!0);
  63. groupToken.matches.push(match), match = groupToken;
  64. }
  65. currentToken.matches.push(match), currentToken.matches.push(quantifierToken);
  66. }
  67. break;
  68. default:
  69. opengroups.length > 0 ? opengroups[opengroups.length - 1].matches.push(m) : currentToken.matches.push(m);
  70. }
  71. currentToken.matches.length > 0 && opts.regexTokens.push(currentToken);
  72. }
  73. function validateRegexToken(token, fromGroup) {
  74. var isvalid = !1;
  75. fromGroup && (regexPart += "(", openGroupCount++);
  76. for (var mndx = 0; mndx < token.matches.length; mndx++) {
  77. var matchToken = token.matches[mndx];
  78. if (1 == matchToken.isGroup) isvalid = validateRegexToken(matchToken, !0); else if (1 == matchToken.isQuantifier) {
  79. var crrntndx = $.inArray(matchToken, token.matches), matchGroup = token.matches[crrntndx - 1], regexPartBak = regexPart;
  80. if (isNaN(matchToken.quantifier.max)) {
  81. for (;matchToken.repeaterPart && matchToken.repeaterPart != regexPart && matchToken.repeaterPart.length > regexPart.length && !(isvalid = validateRegexToken(matchGroup, !0)); ) ;
  82. isvalid = isvalid || validateRegexToken(matchGroup, !0), isvalid && (matchToken.repeaterPart = regexPart),
  83. regexPart = regexPartBak + matchToken.quantifier.max;
  84. } else {
  85. for (var i = 0, qm = matchToken.quantifier.max - 1; qm > i && !(isvalid = validateRegexToken(matchGroup, !0)); i++) ;
  86. regexPart = regexPartBak + "{" + matchToken.quantifier.min + "," + matchToken.quantifier.max + "}";
  87. }
  88. } else if (void 0 != matchToken.matches) for (var k = 0; k < matchToken.length && !(isvalid = validateRegexToken(matchToken[k], fromGroup)); k++) ; else {
  89. var testExp;
  90. if ("[" == matchToken.charAt(0)) {
  91. testExp = regexPart, testExp += matchToken;
  92. for (var j = 0; openGroupCount > j; j++) testExp += ")";
  93. var exp = new RegExp("^(" + testExp + ")$");
  94. isvalid = exp.test(bufferStr);
  95. } else for (var l = 0, tl = matchToken.length; tl > l; l++) if ("\\" != matchToken.charAt(l)) {
  96. testExp = regexPart, testExp += matchToken.substr(0, l + 1), testExp = testExp.replace(/\|$/, "");
  97. for (var j = 0; openGroupCount > j; j++) testExp += ")";
  98. var exp = new RegExp("^(" + testExp + ")$");
  99. if (isvalid = exp.test(bufferStr)) break;
  100. }
  101. regexPart += matchToken;
  102. }
  103. if (isvalid) break;
  104. }
  105. return fromGroup && (regexPart += ")", openGroupCount--), isvalid;
  106. }
  107. null == opts.regexTokens && analyseRegex();
  108. var cbuffer = maskset.buffer.slice(), regexPart = "", isValid = !1, openGroupCount = 0;
  109. cbuffer.splice(pos, 0, chrs);
  110. for (var bufferStr = cbuffer.join(""), i = 0; i < opts.regexTokens.length; i++) {
  111. var regexToken = opts.regexTokens[i];
  112. if (isValid = validateRegexToken(regexToken, regexToken.isGroup)) break;
  113. }
  114. return isValid;
  115. },
  116. cardinality: 1
  117. }
  118. }
  119. }
  120. }), $.fn.inputmask;
  121. });