jquery.inputmask.phone.extensions.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. Phone extension.
  8. When using this extension make sure you specify the correct url to get the masks
  9. $(selector).inputmask("phone", {
  10. url: "Scripts/jquery.inputmask/phone-codes/phone-codes.json",
  11. onKeyValidation: function () { //show some metadata in the console
  12. console.log($(this).inputmask("getmetadata")["cd"]);
  13. }
  14. });
  15. */
  16. (function ($) {
  17. $.extend($.inputmask.defaults.aliases, {
  18. 'phone': {
  19. url: "phone-codes/phone-codes.js",
  20. maskInit: "+pp(pp)pppppppp",
  21. mask: function (opts) {
  22. opts.definitions = {
  23. 'p': {
  24. validator: function () { return false; },
  25. cardinality: 1
  26. },
  27. '#': {
  28. validator: "[0-9]",
  29. cardinality: 1
  30. }
  31. };
  32. var maskList = [];
  33. $.ajax({
  34. url: opts.url,
  35. async: false,
  36. dataType: 'json',
  37. success: function (response) {
  38. maskList = response;
  39. }
  40. });
  41. maskList = maskList.sort(function (a, b) {
  42. return (a["mask"] || a) < (b["mask"] || b) ? -1 : 1;
  43. });
  44. maskList.splice(0, 0, opts.maskInit);
  45. return maskList;
  46. },
  47. nojumps: true,
  48. nojumpsThreshold: 1
  49. },
  50. 'phonebe': {
  51. alias: "phone",
  52. url: "phone-codes/phone-be.js",
  53. maskInit: "+32(pp)pppppppp",
  54. nojumpsThreshold: 4,
  55. onBeforeMask: function(value, opts) {
  56. var processedValue = value.replace(/^0/g, "");
  57. if (processedValue.indexOf("32") > 1 || processedValue.indexOf("32") == -1) {
  58. processedValue = "32" + processedValue;
  59. }
  60. return processedValue;
  61. }
  62. }
  63. });
  64. return $.fn.inputmask;
  65. })(jQuery);