jquery.inputmask.numeric.extensions.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. Optional extensions on the jquery.inputmask base
  8. */
  9. (function ($) {
  10. //number aliases
  11. $.extend($.inputmask.defaults.aliases, {
  12. 'numeric': {
  13. mask: function (opts) {
  14. var mask = opts.prefix;
  15. mask += "[+]~{1," + opts.integerDigits + "}";
  16. mask += "[" + opts.radixPoint + "~{" + opts.digits + "}]";
  17. mask += opts.suffix;
  18. return mask;
  19. },
  20. placeholder: "",
  21. greedy: false,
  22. numericInput: false,
  23. isNumeric: false,
  24. digits: "2", //number of fractionalDigits
  25. groupSeparator: "",//",", // | "."
  26. radixPoint: ".",
  27. groupSize: 3,
  28. autoGroup: false,
  29. allowPlus: true,
  30. allowMinus: true,
  31. integerDigits: "20", //number of integerDigits
  32. defaultValue: "",
  33. prefix: "",
  34. suffix: "",
  35. postFormat: function (buffer, pos, reformatOnly, opts) {
  36. if (opts.groupSeparator == "") return pos;
  37. var cbuf = buffer.slice(),
  38. radixPos = $.inArray(opts.radixPoint, buffer);
  39. if (!reformatOnly) {
  40. cbuf.splice(pos, 0, "?"); //set position indicator
  41. }
  42. var bufVal = cbuf.join('');
  43. if (opts.autoGroup || (reformatOnly && bufVal.indexOf(opts.groupSeparator) != -1)) {
  44. var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator);
  45. bufVal = bufVal.replace(new RegExp(escapedGroupSeparator, "g"), '');
  46. var radixSplit = bufVal.split(opts.radixPoint);
  47. bufVal = radixSplit[0];
  48. var reg = new RegExp('([-\+]?[\\d\?]+)([\\d\?]{' + opts.groupSize + '})');
  49. while (reg.test(bufVal)) {
  50. bufVal = bufVal.replace(reg, '$1' + opts.groupSeparator + '$2');
  51. bufVal = bufVal.replace(opts.groupSeparator + opts.groupSeparator, opts.groupSeparator);
  52. }
  53. if (radixSplit.length > 1)
  54. bufVal += opts.radixPoint + radixSplit[1];
  55. }
  56. buffer.length = bufVal.length; //align the length
  57. for (var i = 0, l = bufVal.length; i < l; i++) {
  58. buffer[i] = bufVal.charAt(i);
  59. }
  60. var newPos = $.inArray("?", buffer);
  61. if (!reformatOnly) buffer.splice(newPos, 1);
  62. return reformatOnly ? pos : newPos;
  63. },
  64. regex: {
  65. signed: function (opts) { }
  66. },
  67. definitions: {
  68. '~': {
  69. validator: function (chrs, buffer, pos, strict, opts) {
  70. if (chrs === "-") {
  71. //negate value
  72. }
  73. var isValid = new RegExp("[0-9]").test(chrs);
  74. return isValid;
  75. },
  76. cardinality: 1,
  77. prevalidator: null
  78. },
  79. '+': {
  80. validator: function (chrs, buffer, pos, strict, opts) {
  81. var signed = "[";
  82. if (opts.allowMinus === true) signed += "-";
  83. if (opts.allowPlus === true) signed += "\+";
  84. signed += "]";
  85. var isValid = new RegExp(signed).test(chrs);
  86. return isValid;
  87. },
  88. cardinality: 1,
  89. prevalidator: null
  90. }
  91. },
  92. insertMode: true,
  93. autoUnmask: false
  94. },
  95. 'decimal': {
  96. mask: "~",
  97. placeholder: "",
  98. repeat: "*",
  99. greedy: false,
  100. numericInput: false,
  101. isNumeric: true,
  102. digits: "*", //number of fractionalDigits
  103. groupSeparator: "",//",", // | "."
  104. radixPoint: ".",
  105. groupSize: 3,
  106. autoGroup: false,
  107. allowPlus: true,
  108. allowMinus: true,
  109. //todo
  110. integerDigits: "*", //number of integerDigits
  111. defaultValue: "",
  112. prefix: "",
  113. suffix: "",
  114. postFormat: function (buffer, pos, reformatOnly, opts) {
  115. if (opts.groupSeparator == "") return pos;
  116. var cbuf = buffer.slice(),
  117. radixPos = $.inArray(opts.radixPoint, buffer);
  118. if (!reformatOnly) {
  119. cbuf.splice(pos, 0, "?"); //set position indicator
  120. }
  121. var bufVal = cbuf.join('');
  122. if (opts.autoGroup || (reformatOnly && bufVal.indexOf(opts.groupSeparator) != -1)) {
  123. var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator);
  124. bufVal = bufVal.replace(new RegExp(escapedGroupSeparator, "g"), '');
  125. var radixSplit = bufVal.split(opts.radixPoint);
  126. bufVal = radixSplit[0];
  127. var reg = new RegExp('([-\+]?[\\d\?]+)([\\d\?]{' + opts.groupSize + '})');
  128. while (reg.test(bufVal)) {
  129. bufVal = bufVal.replace(reg, '$1' + opts.groupSeparator + '$2');
  130. bufVal = bufVal.replace(opts.groupSeparator + opts.groupSeparator, opts.groupSeparator);
  131. }
  132. if (radixSplit.length > 1)
  133. bufVal += opts.radixPoint + radixSplit[1];
  134. }
  135. buffer.length = bufVal.length; //align the length
  136. for (var i = 0, l = bufVal.length; i < l; i++) {
  137. buffer[i] = bufVal.charAt(i);
  138. }
  139. var newPos = $.inArray("?", buffer);
  140. if (!reformatOnly) buffer.splice(newPos, 1);
  141. return reformatOnly ? pos : newPos;
  142. },
  143. regex: {
  144. number: function (opts) {
  145. var escapedRadixPoint = $.inputmask.escapeRegex.call(this, opts.radixPoint);
  146. var digitExpression = isNaN(opts.digits) ? opts.digits : '{0,' + opts.digits + '}';
  147. var integerExpression = isNaN(opts.integerDigits) ? opts.integerDigits : '{1,' + opts.integerDigits + '}';
  148. var signedExpression = opts.allowPlus || opts.allowMinus ? "[" + (opts.allowPlus ? "\+" : "") + (opts.allowMinus ? "-" : "") + "]?" : "";
  149. var currentRegExp = "^" + signedExpression + "\\d" + integerExpression + "(" + escapedRadixPoint + "\\d" + digitExpression + ")?$";
  150. return new RegExp(currentRegExp);
  151. }
  152. },
  153. onKeyDown: function (e, buffer, opts) {
  154. var $input = $(this), input = this;
  155. if (e.keyCode == opts.keyCode.TAB) {
  156. var radixPosition = $.inArray(opts.radixPoint, buffer);
  157. if (radixPosition != -1) {
  158. var masksets = $input.data('_inputmask')['masksets'];
  159. var activeMasksetIndex = $input.data('_inputmask')['activeMasksetIndex'];
  160. for (var i = 1; i <= opts.digits && i < opts.getMaskLength(masksets[activeMasksetIndex]["_buffer"], opts.greedy, opts.repeat, buffer, opts) ; i++) {
  161. if (buffer[radixPosition + i] == undefined || buffer[radixPosition + i] == "") buffer[radixPosition + i] = "0";
  162. }
  163. return { "refreshFromBuffer": true };
  164. }
  165. } else if (e.keyCode == opts.keyCode.DELETE || e.keyCode == opts.keyCode.BACKSPACE) {
  166. opts.postFormat(buffer, 0, true, opts);
  167. input._valueSet(buffer.join(''));
  168. return { "refreshFromBuffer": true };
  169. }
  170. },
  171. definitions: {
  172. '~': { //real number
  173. validator: function (chrs, buffer, pos, strict, opts) {
  174. var iopts = $.extend({}, opts, { digits: strict ? "*" : opts.digits });
  175. if (chrs == "") return false;
  176. if (!strict && pos <= 1 && buffer[0] === '0' && new RegExp("[\\d-]").test(chrs) && buffer.join('').length == 1) { //handle first char
  177. buffer[0] = "";
  178. return { "pos": 0 };
  179. }
  180. var cbuf = strict ? buffer.slice(0, pos) : buffer.slice();
  181. cbuf.splice(pos, 0, chrs);
  182. var bufferStr = cbuf.join('');
  183. //strip groupseparator
  184. var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator);
  185. bufferStr = bufferStr.replace(new RegExp(escapedGroupSeparator, "g"), '');
  186. if (strict && bufferStr.lastIndexOf(opts.radixPoint) == bufferStr.length - 1) {
  187. var escapedRadixPoint = $.inputmask.escapeRegex.call(this, opts.radixPoint);
  188. bufferStr = bufferStr.replace(new RegExp(escapedRadixPoint, "g"), '');
  189. }
  190. if (!strict && bufferStr == "") return false;
  191. var isValid = opts.regex.number(iopts).test(bufferStr);
  192. if (!isValid) {
  193. //let's help the regex a bit
  194. bufferStr += "0";
  195. isValid = opts.regex.number(iopts).test(bufferStr);
  196. if (!isValid) {
  197. //make a valid group
  198. var lastGroupSeparator = bufferStr.lastIndexOf(opts.groupSeparator);
  199. for (var i = bufferStr.length - lastGroupSeparator; i <= 3; i++) {
  200. bufferStr += "0";
  201. }
  202. isValid = opts.regex.number(iopts).test(bufferStr);
  203. if (!isValid && !strict) {
  204. if (chrs == opts.radixPoint) {
  205. isValid = opts.regex.number(iopts).test("0" + bufferStr + "0");
  206. if (isValid) {
  207. buffer[pos] = "0";
  208. pos++;
  209. return { "pos": pos };
  210. }
  211. }
  212. }
  213. }
  214. }
  215. if (isValid != false && !strict && chrs != opts.radixPoint) {
  216. var newPos = opts.postFormat(buffer, pos, (chrs == "-" || chrs == "+") ? true : false, opts);
  217. return { "pos": newPos, "refreshFromBuffer": true };
  218. }
  219. return isValid;
  220. },
  221. cardinality: 1,
  222. prevalidator: null
  223. }
  224. },
  225. insertMode: true,
  226. autoUnmask: false
  227. },
  228. 'integer': {
  229. regex: {
  230. number: function (opts) {
  231. var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator);
  232. var signedExpression = opts.allowPlus || opts.allowMinus ? "[" + (opts.allowPlus ? "\+" : "") + (opts.allowMinus ? "-" : "") + "]?" : "";
  233. return new RegExp("^" + signedExpression + "(\\d+|\\d{1," + opts.groupSize + "}((" + escapedGroupSeparator + "\\d{" + opts.groupSize + "})?)+)$");
  234. }
  235. },
  236. alias: "decimal"
  237. }
  238. });
  239. })(jQuery);