jquery.inputmask.phone.extensions.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. Input Mask plugin extensions
  3. http://github.com/RobinHerbots/jquery.inputmask
  4. Copyright (c) 2010 - 2014 Robin Herbots
  5. Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
  6. Version: 0.0.0
  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. $.extend($.inputmask.defaults.aliases, {
  18. 'phone': {
  19. url: "phone-codes/phone-codes.js",
  20. maskInit: "+pp(pp)pppppppp",
  21. countrycode: "",
  22. mask: function (opts) {
  23. opts.definitions = {
  24. 'p': {
  25. validator: function () { return false; },
  26. cardinality: 1
  27. },
  28. '#': {
  29. validator: "[0-9]",
  30. cardinality: 1
  31. }
  32. };
  33. var maskList = [];
  34. $.ajax({
  35. url: opts.url,
  36. async: false,
  37. dataType: 'json',
  38. success: function (response) {
  39. maskList = response;
  40. },
  41. error: function (xhr, ajaxOptions, thrownError) {
  42. alert(thrownError + " - " + opts.url);
  43. }
  44. });
  45. maskList = maskList.sort(function (a, b) {
  46. return (a["mask"] || a) < (b["mask"] || b) ? -1 : 1;
  47. });
  48. if (opts.countrycode != "") {
  49. opts.maskInit = "+" + opts.countrycode + opts.maskInit.substring(3);
  50. }
  51. maskList.splice(0, 0, opts.maskInit);
  52. return maskList;
  53. },
  54. nojumps: true,
  55. nojumpsThreshold: 1,
  56. onBeforeMask: function (value, opts) {
  57. var processedValue = value.replace(/^0/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 $.fn.inputmask;
  72. })(jQuery);