inputmask.phone.extensions.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. opts.definitions["#"] = opts.definitions["9"];
  34. $.ajax({
  35. url: opts.url,
  36. async: false,
  37. type: "get",
  38. dataType: "json",
  39. success: function(response) {
  40. maskList = response;
  41. },
  42. error: function(xhr, ajaxOptions, thrownError) {
  43. alert(thrownError + " - " + opts.url);
  44. }
  45. });
  46. opts.phoneCodeCache[opts.url] = maskList.sort(function(a, b) {
  47. return (a.mask || a) < (b.mask || b) ? -1 : 1;
  48. });
  49. }
  50. return opts.phoneCodeCache[opts.url];
  51. },
  52. keepStatic: false,
  53. nojumps: true,
  54. nojumpsThreshold: 1,
  55. onBeforeMask: function(value, opts) {
  56. var processedValue = value.replace(/^0{1,2}/, "").replace(/[\s]/g, "");
  57. if (processedValue.indexOf(opts.countrycode) > 1 || processedValue.indexOf(opts.countrycode) === -1) {
  58. processedValue = "+" + opts.countrycode + processedValue;
  59. }
  60. return processedValue;
  61. }
  62. },
  63. "phonebe": {
  64. alias: "phone",
  65. url: "phone-codes/phone-be.js",
  66. countrycode: "32",
  67. nojumpsThreshold: 4
  68. }
  69. });
  70. return Inputmask;
  71. }));