inputmask.phone.extensions.js 2.0 KB

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