jquery.inputmask.regex.extensions.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. opts.regexSplit = [];
  23. if (opts.regex.indexOf("*") != (opts.regex.length - 1)) {
  24. opts.regex += "{1}";
  25. }
  26. opts.regexSplit.push(opts.regex);
  27. }
  28. if (opts.regexSplit == null) {
  29. analyseRegex();
  30. }
  31. var cbuffer = buffer.slice(), regexPart = "", isValid = false;
  32. cbuffer.splice(pos, 0, chrs);
  33. var bufferStr = cbuffer.join('');
  34. for (var i = 0; i < opts.regexSplit.length; i++) {
  35. regexPart += opts.regexSplit[i];
  36. var exp = new RegExp("^" + regexPart + "$");
  37. isValid = exp.test(bufferStr);
  38. console.log(bufferStr + ' ' + isValid);
  39. if (isValid) break;
  40. }
  41. return isValid;
  42. },
  43. cardinality: 1
  44. }
  45. }
  46. }
  47. });
  48. })(jQuery);