inputmask.phone.extensions.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. Input Mask plugin extensions
  3. http://github.com/RobinHerbots/jquery.inputmask
  4. Copyright (c) 2010 - Robin Herbots
  5. Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
  6. Version: 0.0.0-dev
  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 (factory) {
  17. if (typeof define === "function" && define.amd) {
  18. define(["jquery", "inputmask"], factory);
  19. } else if (typeof exports === "object") {
  20. module.exports = factory(require("jquery"), require("./inputmask"));
  21. } else {
  22. factory(window.dependencyLib || jQuery, window.Inputmask);
  23. }
  24. }
  25. (function ($, Inputmask) {
  26. Inputmask.extendAliases({
  27. "phone": {
  28. url: "phone-codes/phone-codes.js",
  29. countrycode: "",
  30. phoneCodeCache: {},
  31. mask: function (opts) {
  32. if (opts.phoneCodeCache[opts.url] === undefined) {
  33. var maskList = [];
  34. opts.definitions["#"] = opts.definitions["9"];
  35. $.ajax({
  36. url: opts.url,
  37. async: false,
  38. type: "get",
  39. dataType: "json",
  40. success: function (response) {
  41. maskList = response;
  42. },
  43. error: function (xhr, ajaxOptions, thrownError) {
  44. alert(thrownError + " - " + opts.url);
  45. }
  46. });
  47. opts.phoneCodeCache[opts.url] = maskList.sort(function (a, b) {
  48. return (a.mask || a) < (b.mask || b) ? -1 : 1;
  49. });
  50. }
  51. return opts.phoneCodeCache[opts.url];
  52. },
  53. keepStatic: false,
  54. nojumps: true,
  55. nojumpsThreshold: 1,
  56. onBeforeMask: function (value, opts) {
  57. var processedValue = value.replace(/^0{1,2}/, "").replace(/[\s]/g, "");
  58. if (processedValue.indexOf(opts.countrycode) > 1 || processedValue.indexOf(opts.countrycode) === -1) {
  59. processedValue = "+" + opts.countrycode + processedValue;
  60. }
  61. return processedValue;
  62. }
  63. },
  64. "phonebe": {
  65. alias: "phone",
  66. url: "phone-codes/phone-be.js",
  67. countrycode: "32",
  68. nojumpsThreshold: 4
  69. }
  70. });
  71. return Inputmask;
  72. }));