inputmask.regex.extensions.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. Input Mask plugin extensions
  3. http://github.com/RobinHerbots/jquery.inputmask
  4. Copyright (c) 2010 - Robin Herbots
  5. Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
  6. Version: 0.0.0-dev
  7. Regex extensions on the jquery.inputmask base
  8. Allows for using regular expressions as a mask
  9. */
  10. (function($) {
  11. Inputmask.extendAliases({ // $(selector).inputmask("Regex", { regex: "[0-9]*"}
  12. "Regex": {
  13. mask: "r",
  14. greedy: false,
  15. repeat: "*",
  16. regex: null,
  17. regexTokens: null,
  18. //Thx to https://github.com/slevithan/regex-colorizer for the tokenizer regex
  19. 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,
  20. quantifierFilter: /[0-9]+[^,]/,
  21. isComplete: function(buffer, opts) {
  22. return new RegExp(opts.regex).test(buffer.join(""));
  23. },
  24. definitions: {
  25. "r": {
  26. validator: function(chrs, maskset, pos, strict, opts) {
  27. var cbuffer = maskset.buffer.slice(),
  28. regexPart = "",
  29. isValid = false,
  30. openGroupCount = 0,
  31. groupToken;
  32. function RegexToken(isGroup, isQuantifier) {
  33. this.matches = [];
  34. this.isGroup = isGroup || false;
  35. this.isQuantifier = isQuantifier || false;
  36. this.quantifier = {
  37. min: 1,
  38. max: 1
  39. };
  40. this.repeaterPart = undefined;
  41. }
  42. function analyseRegex() {
  43. var currentToken = new RegexToken(),
  44. match, m, opengroups = [];
  45. opts.regexTokens = [];
  46. // The tokenizer regex does most of the tokenization grunt work
  47. while (match = opts.tokenizer.exec(opts.regex)) {
  48. m = match[0];
  49. switch (m.charAt(0)) {
  50. case "(": // Group opening
  51. opengroups.push(new RegexToken(true));
  52. break;
  53. case ")": // Group closing
  54. groupToken = opengroups.pop();
  55. if (opengroups.length > 0)
  56. opengroups[opengroups.length - 1].matches.push(groupToken);
  57. else
  58. currentToken.matches.push(groupToken);
  59. break;
  60. case "{":
  61. case "+":
  62. case "*": //Quantifier
  63. var quantifierToken = new RegexToken(false, true);
  64. m = m.replace(/[{}]/g, "");
  65. var mq = m.split(","),
  66. mq0 = isNaN(mq[0]) ? mq[0] : parseInt(mq[0]),
  67. mq1 = mq.length === 1 ? mq0 : (isNaN(mq[1]) ? mq[1] : parseInt(mq[1]));
  68. quantifierToken.quantifier = {
  69. min: mq0,
  70. max: mq1
  71. };
  72. if (opengroups.length > 0) {
  73. var matches = opengroups[opengroups.length - 1].matches;
  74. match = matches.pop();
  75. if (!match.isGroup) {
  76. groupToken = new RegexToken(true);
  77. groupToken.matches.push(match);
  78. match = groupToken;
  79. }
  80. matches.push(match);
  81. matches.push(quantifierToken);
  82. } else {
  83. match = currentToken.matches.pop();
  84. if (!match.isGroup) {
  85. groupToken = new RegexToken(true);
  86. groupToken.matches.push(match);
  87. match = groupToken;
  88. }
  89. currentToken.matches.push(match);
  90. currentToken.matches.push(quantifierToken);
  91. }
  92. break;
  93. default:
  94. if (opengroups.length > 0) {
  95. opengroups[opengroups.length - 1].matches.push(m);
  96. } else {
  97. currentToken.matches.push(m);
  98. }
  99. break;
  100. }
  101. }
  102. if (currentToken.matches.length > 0)
  103. opts.regexTokens.push(currentToken);
  104. }
  105. function validateRegexToken(token, fromGroup) {
  106. var isvalid = false;
  107. if (fromGroup) {
  108. regexPart += "(";
  109. openGroupCount++;
  110. }
  111. for (var mndx = 0; mndx < token.matches.length; mndx++) {
  112. var matchToken = token.matches[mndx];
  113. if (matchToken.isGroup === true) {
  114. isvalid = validateRegexToken(matchToken, true);
  115. } else if (matchToken.isQuantifier === true) {
  116. var crrntndx = $.inArray(matchToken, token.matches),
  117. matchGroup = token.matches[crrntndx - 1];
  118. var regexPartBak = regexPart;
  119. if (isNaN(matchToken.quantifier.max)) {
  120. while (matchToken.repeaterPart && matchToken.repeaterPart !== regexPart && matchToken.repeaterPart.length > regexPart.length) {
  121. isvalid = validateRegexToken(matchGroup, true);
  122. if (isvalid) break;
  123. }
  124. isvalid = isvalid || validateRegexToken(matchGroup, true);
  125. if (isvalid) matchToken.repeaterPart = regexPart;
  126. regexPart = regexPartBak + matchToken.quantifier.max;
  127. } else {
  128. for (var i = 0, qm = matchToken.quantifier.max - 1; i < qm; i++) {
  129. isvalid = validateRegexToken(matchGroup, true);
  130. if (isvalid) break;
  131. }
  132. regexPart = regexPartBak + "{" + matchToken.quantifier.min + "," + matchToken.quantifier.max + "}";
  133. }
  134. } else if (matchToken.matches !== undefined) {
  135. for (var k = 0; k < matchToken.length; k++) {
  136. isvalid = validateRegexToken(matchToken[k], fromGroup);
  137. if (isvalid) break;
  138. }
  139. } else {
  140. var testExp;
  141. if (matchToken.charAt(0) == "[") {
  142. testExp = regexPart;
  143. testExp += matchToken;
  144. for (var j = 0; j < openGroupCount; j++) {
  145. testExp += ")";
  146. }
  147. var exp = new RegExp("^(" + testExp + ")$");
  148. isvalid = exp.test(bufferStr);
  149. } else {
  150. for (var l = 0, tl = matchToken.length; l < tl; l++) {
  151. if (matchToken.charAt(l) === "\\") continue;
  152. testExp = regexPart;
  153. testExp += matchToken.substr(0, l + 1);
  154. testExp = testExp.replace(/\|$/, "");
  155. for (var j = 0; j < openGroupCount; j++) {
  156. testExp += ")";
  157. }
  158. var exp = new RegExp("^(" + testExp + ")$");
  159. isvalid = exp.test(bufferStr);
  160. if (isvalid) break;
  161. }
  162. }
  163. regexPart += matchToken;
  164. }
  165. if (isvalid) break;
  166. }
  167. if (fromGroup) {
  168. regexPart += ")";
  169. openGroupCount--;
  170. }
  171. return isvalid;
  172. }
  173. if (opts.regexTokens === null)
  174. analyseRegex();
  175. cbuffer.splice(pos, 0, chrs);
  176. var bufferStr = cbuffer.join("");
  177. for (var i = 0; i < opts.regexTokens.length; i++) {
  178. var regexToken = opts.regexTokens[i];
  179. isValid = validateRegexToken(regexToken, regexToken.isGroup);
  180. if (isValid) break;
  181. }
  182. return isValid;
  183. },
  184. cardinality: 1
  185. }
  186. }
  187. }
  188. });
  189. return Inputmask;
  190. })(jQuery);