jquery.inputmask.regex.extensions.js 11 KB

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