jquery.inputmask.custom.extensions.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. $.extend($.inputmask.defaults.aliases, {
  2. 'h:s t': {
  3. mask: "h:s t",
  4. regex: {
  5. hrspre: new RegExp("[012]"), //hours pre
  6. hrs24: new RegExp("2[0-9]|1[3-9]"),
  7. hrs: new RegExp("[01][0-9]|2[0-3]"), //hours
  8. ampmpre: new RegExp("[apAP]"),
  9. ampm: new RegExp("^[a|p|A|P][m|M]")
  10. },
  11. separator: ':',
  12. transform : function(buffer, position, element, opts){
  13. return element.replace(/[\.\-\:]/gi,opts.separator[opts.separator.length - 1]);
  14. },
  15. definitions: {
  16. 'h': { //val1 ~ hours
  17. validator: function(chrs, buffer, pos, strict, opts) {
  18. var isValid = opts.regex.hrs.test(chrs);
  19. if (!strict && !isValid) {
  20. if (chrs.charAt(1) == opts.separator[opts.separator.length - 1] || "-.:".indexOf(chrs.charAt(1)) != -1 ) {
  21. isValid = opts.regex.hrs.test("0" + chrs.charAt(0));
  22. if (isValid) {
  23. buffer[pos - 1] = "0";
  24. buffer[pos] = chrs.charAt(0);
  25. pos++;
  26. return { "pos": pos };
  27. }
  28. }
  29. }
  30. if ( isValid && opts.regex.hrs24.test(chrs) ) {
  31. var tmp = parseInt(chrs,10);
  32. if ( tmp == 24 ) {
  33. buffer[pos+5] = "a";
  34. buffer[pos+6] = "m";
  35. } else {
  36. buffer[pos+5] = "p";
  37. buffer[pos+6] = "m";
  38. }
  39. tmp = tmp - 12;
  40. if ( tmp < 10 ) {
  41. buffer[pos] = tmp.toString();
  42. buffer[pos-1] = "0";
  43. } else {
  44. buffer[pos] = tmp.toString().charAt(1);
  45. buffer[pos-1] = tmp.toString().charAt(0);
  46. }
  47. return { "pos": pos, "c" : buffer[pos] };
  48. }
  49. return isValid;
  50. },
  51. cardinality: 2,
  52. prevalidator: [{ validator: function(chrs, buffer, pos, strict, opts) {
  53. var isValid = opts.regex.hrspre.test(chrs);
  54. if (!strict && !isValid) {
  55. isValid = opts.regex.hrs.test("0" + chrs);
  56. if (isValid) {
  57. buffer[pos] = "0";
  58. pos++;
  59. return { "pos": pos };
  60. }
  61. }
  62. return isValid;
  63. }, cardinality: 1}]
  64. },
  65. 't': { //val1 ~ hours
  66. validator: function(chrs, buffer, pos, strict, opts) {
  67. var isValid = opts.regex.ampm.test(chrs);
  68. if (!strict && !isValid) {
  69. isValid = opts.regex.ampm.test(chrs+'m');
  70. if (isValid) {
  71. buffer[pos - 1] = chrs.charAt(0);
  72. buffer[pos] = "m";
  73. pos++;
  74. return pos;
  75. }
  76. }
  77. return isValid;
  78. },
  79. casing: "lower",
  80. cardinality: 2,
  81. prevalidator: [{ validator: function(chrs, buffer, pos, strict, opts) {
  82. var isValid = opts.regex.ampmpre.test(chrs);
  83. if (isValid) {
  84. isValid = opts.regex.ampm.test(chrs+"m");
  85. if (isValid) {
  86. buffer[pos] = chrs;
  87. buffer[pos+1] = 'm';
  88. return pos;
  89. }
  90. }
  91. return isValid;
  92. }, cardinality: 1}]
  93. }
  94. },
  95. insertMode: false,
  96. autoUnmask: false
  97. },
  98. 'url' : {
  99. mask: "ir",
  100. placeholder: "",
  101. separator: "",
  102. defaultPrefix: "http://",
  103. regex: {
  104. urlpre1: new RegExp("[fh]"),
  105. urlpre2: new RegExp("(ft|ht)"),
  106. urlpre3: new RegExp("(ftp|htt)"),
  107. urlpre4: new RegExp("(ftp:|http|ftps)"),
  108. urlpre5: new RegExp("(ftp:/|ftps:|http:|https)"),
  109. urlpre6: new RegExp("(ftp://|ftps:/|http:/|https:)"),
  110. urlpre7: new RegExp("(ftp://|ftps://|http://|https:/)"),
  111. urlpre8: new RegExp("(ftp://|ftps://|http://|https://)")
  112. },
  113. definitions: {
  114. 'i': {
  115. validator: function(chrs, buffer, pos, strict, opts) {
  116. return true;
  117. },
  118. cardinality: 8,
  119. prevalidator: (function(){
  120. var result = [], prefixLimit = 8;
  121. for( var i=0; i < prefixLimit; i++ ) {
  122. result[i] = (function(){
  123. var j = i;
  124. return { validator: function(chrs, buffer, pos, strict, opts) {
  125. if ( opts.regex["urlpre"+(j+1)] ) {
  126. var tmp = chrs, k;
  127. if ( ( ( j + 1 ) - chrs.length ) > 0 ) {
  128. tmp = buffer.join('').substring(0,( ( j + 1 ) - chrs.length )) + "" + tmp;
  129. }
  130. var isValid = opts.regex["urlpre"+(j+1)].test(tmp);
  131. if (!strict && !isValid) {
  132. pos = pos-j;
  133. for (k=0;k<opts.defaultPrefix.length;k++){
  134. buffer[pos] = opts.defaultPrefix[k];pos++;
  135. }
  136. for (k=0; k<tmp.length-1;k++) {
  137. buffer[pos] = tmp[k];pos++;
  138. }
  139. return { "pos": pos };
  140. }
  141. return isValid;
  142. } else {
  143. return false;
  144. }
  145. }, cardinality: j};
  146. })();
  147. }
  148. return result;
  149. })()
  150. },
  151. 'r': {
  152. validator: function(chrs, buffer, pos, strict, opts) {
  153. return true;
  154. },
  155. cardinality: 2000
  156. }
  157. },
  158. insertMode: false,
  159. autoUnmask: false
  160. }
  161. });