jquery.inputmask.phone.extensions.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. Input Mask plugin extensions
  3. http://github.com/RobinHerbots/jquery.inputmask
  4. Copyright (c) 2010 - 2013 Robin Herbots
  5. Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
  6. Version: 0.0.0
  7. Phone extension based on inputmask-multi - DO NOT USE YET!! in TEST
  8. */
  9. $.extend($.inputmask.defaults.aliases, {
  10. 'phone': {
  11. url: "phone-codes.json",
  12. mask: function (opts) {
  13. function masksSort(maskList, defs, match, key) {
  14. maskList.sort(function (a, b) {
  15. var ia = 0, ib = 0;
  16. for (; (ia < a[key].length && ib < b[key].length) ;) {
  17. var cha = a[key].charAt(ia);
  18. var chb = b[key].charAt(ib);
  19. if (!match.test(cha)) {
  20. ia++;
  21. continue;
  22. }
  23. if (!match.test(chb)) {
  24. ib++;
  25. continue;
  26. }
  27. if ($.inArray(cha, defs) != -1 && $.inArray(chb, defs) == -1) {
  28. return 1;
  29. }
  30. if ($.inArray(cha, defs) == -1 && $.inArray(chb, defs) != -1) {
  31. return -1;
  32. }
  33. if ($.inArray(cha, defs) == -1 && $.inArray(chb, defs) == -1) {
  34. if (cha != chb) {
  35. return cha < chb ? -1 : 1;
  36. }
  37. }
  38. ia++;
  39. ib++;
  40. }
  41. for (; (ia < a[key].length || ib < b[key].length) ;) {
  42. if (ia < a[key].length && !match.test(a[key].charAt(ia))) {
  43. ia++;
  44. continue;
  45. }
  46. if (ib < b[key].length && !match.test(b[key].charAt(ib))) {
  47. ib++;
  48. continue;
  49. }
  50. if (ia < a[key].length) {
  51. return 1;
  52. }
  53. if (ib < b[key].length) {
  54. return -1;
  55. }
  56. }
  57. return 0;
  58. });
  59. return maskList;
  60. }
  61. var maskList = [];
  62. $.ajax({
  63. url: opts.url,
  64. async: false,
  65. dataType: 'json',
  66. success: function (response) {
  67. maskList = response;
  68. }
  69. });
  70. return masksSort(maskList, ['#'], /[0-9]|#/, "mask");
  71. }
  72. }
  73. });