inputmask.regex.extensions.js 12 KB

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