jquery.inputmask.regex.extensions.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. Input Mask plugin extensions
  3. http://github.com/RobinHerbots/jquery.inputmask
  4. Copyright (c) 2010 - 2013 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. /* EXPERIMENTAL */
  11. (function ($) {
  12. $.extend($.inputmask.defaults.aliases, { // $(selector).inputmask("Regex", { regex: "[0-9]*"}
  13. 'Regex': {
  14. mask: "r",
  15. greedy: false,
  16. repeat: "*",
  17. regex: null,
  18. regexSplit: null,
  19. definitions: {
  20. 'r': {
  21. validator: function (chrs, buffer, pos, strict, opts) {
  22. function analyseRegex() { //ENHANCE ME
  23. var regexSplitRegex = "\\[.*?\]\\*";
  24. opts.regexSplit = opts.regex.match(new RegExp(regexSplitRegex, "g"));
  25. //if (opts.regex.indexOf("*") != (opts.regex.length - 1)) {
  26. // opts.regex += "{1}";
  27. //}
  28. //opts.regexSplit.push(opts.regex);
  29. }
  30. if (opts.regexSplit == null) {
  31. analyseRegex();
  32. }
  33. var cbuffer = buffer.slice(), regexPart = "", isValid = false;
  34. cbuffer.splice(pos, 0, chrs);
  35. var bufferStr = cbuffer.join('');
  36. for (var i = 0; i < opts.regexSplit.length; i++) {
  37. regexPart += opts.regexSplit[i];
  38. var exp = new RegExp("^" + regexPart + "$");
  39. isValid = exp.test(bufferStr);
  40. console.log(bufferStr + ' ' + isValid + ' ' + regexPart);
  41. if (isValid) break;
  42. }
  43. return isValid;
  44. },
  45. cardinality: 1
  46. }
  47. }
  48. }
  49. });
  50. })(jQuery);