jquery.inputmask.regex.extensions.js 2.1 KB

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