inputmask.phone.extensions.js 1.7 KB

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