jquery.inputmask.regex.extensions.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. definitions: {
  22. 'r': {
  23. validator: function (chrs, buffer, pos, strict, opts) {
  24. function regexToken() {
  25. this.matches = [];
  26. this.isGroup = false;
  27. this.isQuantifier = false;
  28. this.isLiteral = false;
  29. }
  30. function analyseRegex() {
  31. var currentToken = new regexToken(), match, m, opengroups = [];
  32. opts.regexTokens = [];
  33. // The tokenizer regex does most of the tokenization grunt work
  34. while (match = opts.tokenizer.exec(opts.regex)) {
  35. m = match[0];
  36. switch (m.charAt(0)) {
  37. case "[": // Character class
  38. case "\\": // Escape or backreference
  39. if (opengroups.length > 0) {
  40. opengroups[opengroups.length - 1]["matches"].push(m);
  41. } else {
  42. currentToken.matches.push(m);
  43. }
  44. break;
  45. case "(": // Group opening
  46. if (!currentToken.isGroup && currentToken.matches.length > 0)
  47. opts.regexTokens.push(currentToken);
  48. currentToken = new regexToken();
  49. currentToken.isGroup = true;
  50. opengroups.push(currentToken);
  51. break;
  52. case ")": // Group closing
  53. var groupToken = opengroups.pop();
  54. if (opengroups.length > 0) {
  55. opengroups[opengroups.length - 1]["matches"].push(groupToken);
  56. } else {
  57. opts.regexTokens.push(groupToken);
  58. currentToken = new regexToken();
  59. }
  60. break;
  61. case "{": //Quantifier
  62. var quantifier = new regexToken();
  63. quantifier.isQuantifier = true;
  64. quantifier.matches.push(m);
  65. if (opengroups.length > 0) {
  66. opengroups[opengroups.length - 1]["matches"].push(quantifier);
  67. } else {
  68. currentToken.matches.push(quantifier);
  69. }
  70. break;
  71. default:
  72. // Vertical bar (alternator)
  73. // ^ or $ anchor
  74. // Dot (.)
  75. // Literal character sequence
  76. var literal = new regexToken();
  77. literal.isLiteral = true;
  78. literal.matches.push(m);
  79. if (opengroups.length > 0) {
  80. opengroups[opengroups.length - 1]["matches"].push(literal);
  81. } else {
  82. currentToken.matches.push(literal);
  83. }
  84. }
  85. }
  86. if (currentToken.matches.length > 0)
  87. opts.regexTokens.push(currentToken);
  88. };
  89. function validateRegexToken(token, fromGroup) {
  90. var isvalid = false;
  91. if (fromGroup) {
  92. regexPart += "(";
  93. openGroupCount++;
  94. }
  95. for (var mndx = 0; mndx < token["matches"].length; mndx++) {
  96. var matchToken = token["matches"][mndx];
  97. if (matchToken["isGroup"] == true) {
  98. isvalid = validateRegexToken(matchToken, true);
  99. } else if (matchToken["isQuantifier"] == true) {
  100. matchToken = matchToken["matches"][0];
  101. var quantifierMax = opts.quantifierFilter.exec(matchToken)[0].replace("}", "");
  102. var testExp = regexPart + "{1," + quantifierMax + "}"; //relax quantifier validation
  103. for (var j = 0; j < openGroupCount; j++) {
  104. testExp += ")";
  105. }
  106. var exp = new RegExp("^(" + testExp + ")$");
  107. isvalid = exp.test(bufferStr);
  108. regexPart += matchToken;
  109. } else if (matchToken["isLiteral"] == true) {
  110. matchToken = matchToken["matches"][0];
  111. var testExp = regexPart, openGroupCloser = "";
  112. for (var j = 0; j < openGroupCount; j++) {
  113. openGroupCloser += ")";
  114. }
  115. for (var k = 0; k < matchToken.length; k++) { //relax literal validation
  116. testExp = (testExp + matchToken[k]).replace(/\|$/, "");
  117. var exp = new RegExp("^(" + testExp + openGroupCloser + ")$");
  118. isvalid = exp.test(bufferStr);
  119. if (isvalid) break;
  120. }
  121. regexPart += matchToken;
  122. //console.log(bufferStr + " " + exp + " " + isvalid);
  123. } else {
  124. regexPart += matchToken;
  125. var testExp = regexPart.replace(/\|$/, "");
  126. for (var j = 0; j < openGroupCount; j++) {
  127. testExp += ")";
  128. }
  129. var exp = new RegExp("^(" + testExp + ")$");
  130. isvalid = exp.test(bufferStr);
  131. //console.log(bufferStr + " " + exp + " " + isvalid);
  132. }
  133. if (isvalid) break;
  134. }
  135. if (fromGroup) {
  136. regexPart += ")";
  137. openGroupCount--;
  138. }
  139. return isvalid;
  140. }
  141. if (opts.regexTokens == null) {
  142. analyseRegex();
  143. }
  144. var cbuffer = buffer.slice(), regexPart = "", isValid = false, openGroupCount = 0;
  145. cbuffer.splice(pos, 0, chrs);
  146. var bufferStr = cbuffer.join('');
  147. for (var i = 0; i < opts.regexTokens.length; i++) {
  148. var regexToken = opts.regexTokens[i];
  149. isValid = validateRegexToken(regexToken, regexToken["isGroup"]);
  150. if (isValid) break;
  151. }
  152. return isValid;
  153. },
  154. cardinality: 1
  155. }
  156. }
  157. }
  158. });
  159. })(jQuery);