inputmask.phone.extensions.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. }
  48. return processedValue;
  49. }
  50. },
  51. 'phonebe': {
  52. alias: "phone",
  53. url: "phone-codes/phone-be.js",
  54. countrycode: "32",
  55. nojumpsThreshold: 4
  56. }
  57. });
  58. return inputmask;
  59. })(jQuery);