jquery.inputmask.numeric.extensions.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. integerPart: function (opts) { return new RegExp('[-\+]?\\d+'); }
  66. },
  67. definitions: {
  68. '~': {
  69. validator: function (chrs, buffer, pos, strict, opts) {
  70. if (!strict && chrs === "-") {
  71. var matchRslt = buffer.join('').match(opts.regex.integerPart(opts));
  72. if (matchRslt.length > 0) {
  73. if (buffer[matchRslt.index] == "+") {
  74. buffer.splice(matchRslt.index, 1);
  75. return { "pos": matchRslt.index, "c": "-", "refreshFromBuffer": true };
  76. } else if (buffer[matchRslt.index] == "-") {
  77. buffer.splice(matchRslt.index, 1);
  78. return { "refreshFromBuffer": true };
  79. } else {
  80. return { "pos": matchRslt.index, "c": "-" };
  81. }
  82. }
  83. }
  84. var isValid = new RegExp("[0-9]").test(chrs);
  85. return isValid;
  86. },
  87. cardinality: 1,
  88. prevalidator: null
  89. },
  90. '+': {
  91. validator: function (chrs, buffer, pos, strict, opts) {
  92. var signed = "[";
  93. if (opts.allowMinus === true) signed += "-";
  94. if (opts.allowPlus === true) signed += "\+";
  95. signed += "]";
  96. var isValid = new RegExp(signed).test(chrs);
  97. return isValid;
  98. },
  99. cardinality: 1,
  100. prevalidator: null
  101. }
  102. },
  103. insertMode: true,
  104. autoUnmask: false
  105. },
  106. 'decimal': {
  107. mask: "~",
  108. placeholder: "",
  109. repeat: "*",
  110. greedy: false,
  111. numericInput: false,
  112. isNumeric: true,
  113. digits: "*", //number of fractionalDigits
  114. groupSeparator: "",//",", // | "."
  115. radixPoint: ".",
  116. groupSize: 3,
  117. autoGroup: false,
  118. allowPlus: true,
  119. allowMinus: true,
  120. //todo
  121. integerDigits: "*", //number of integerDigits
  122. defaultValue: "",
  123. prefix: "",
  124. suffix: "",
  125. postFormat: function (buffer, pos, reformatOnly, opts) {
  126. if (opts.groupSeparator == "") return pos;
  127. var cbuf = buffer.slice(),
  128. radixPos = $.inArray(opts.radixPoint, buffer);
  129. if (!reformatOnly) {
  130. cbuf.splice(pos, 0, "?"); //set position indicator
  131. }
  132. var bufVal = cbuf.join('');
  133. if (opts.autoGroup || (reformatOnly && bufVal.indexOf(opts.groupSeparator) != -1)) {
  134. var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator);
  135. bufVal = bufVal.replace(new RegExp(escapedGroupSeparator, "g"), '');
  136. var radixSplit = bufVal.split(opts.radixPoint);
  137. bufVal = radixSplit[0];
  138. var reg = new RegExp('([-\+]?[\\d\?]+)([\\d\?]{' + opts.groupSize + '})');
  139. while (reg.test(bufVal)) {
  140. bufVal = bufVal.replace(reg, '$1' + opts.groupSeparator + '$2');
  141. bufVal = bufVal.replace(opts.groupSeparator + opts.groupSeparator, opts.groupSeparator);
  142. }
  143. if (radixSplit.length > 1)
  144. bufVal += opts.radixPoint + radixSplit[1];
  145. }
  146. buffer.length = bufVal.length; //align the length
  147. for (var i = 0, l = bufVal.length; i < l; i++) {
  148. buffer[i] = bufVal.charAt(i);
  149. }
  150. var newPos = $.inArray("?", buffer);
  151. if (!reformatOnly) buffer.splice(newPos, 1);
  152. return reformatOnly ? pos : newPos;
  153. },
  154. regex: {
  155. number: function (opts) {
  156. var escapedRadixPoint = $.inputmask.escapeRegex.call(this, opts.radixPoint);
  157. var digitExpression = isNaN(opts.digits) ? opts.digits : '{0,' + opts.digits + '}';
  158. var integerExpression = isNaN(opts.integerDigits) ? opts.integerDigits : '{1,' + opts.integerDigits + '}';
  159. var signedExpression = opts.allowPlus || opts.allowMinus ? "[" + (opts.allowPlus ? "\+" : "") + (opts.allowMinus ? "-" : "") + "]?" : "";
  160. var currentRegExp = "^" + signedExpression + "\\d" + integerExpression + "(" + escapedRadixPoint + "\\d" + digitExpression + ")?$";
  161. return new RegExp(currentRegExp);
  162. }
  163. },
  164. onKeyDown: function (e, buffer, opts) {
  165. var $input = $(this), input = this;
  166. if (e.keyCode == opts.keyCode.TAB) {
  167. var radixPosition = $.inArray(opts.radixPoint, buffer);
  168. if (radixPosition != -1) {
  169. var masksets = $input.data('_inputmask')['masksets'];
  170. var activeMasksetIndex = $input.data('_inputmask')['activeMasksetIndex'];
  171. for (var i = 1; i <= opts.digits && i < opts.getMaskLength(masksets[activeMasksetIndex]["_buffer"], opts.greedy, opts.repeat, buffer, opts) ; i++) {
  172. if (buffer[radixPosition + i] == undefined || buffer[radixPosition + i] == "") buffer[radixPosition + i] = "0";
  173. }
  174. return { "refreshFromBuffer": true };
  175. }
  176. } else if (e.keyCode == opts.keyCode.DELETE || e.keyCode == opts.keyCode.BACKSPACE) {
  177. opts.postFormat(buffer, 0, true, opts);
  178. input._valueSet(buffer.join(''));
  179. return { "refreshFromBuffer": true };
  180. }
  181. },
  182. definitions: {
  183. '~': { //real number
  184. validator: function (chrs, buffer, pos, strict, opts) {
  185. var iopts = $.extend({}, opts, { digits: strict ? "*" : opts.digits });
  186. if (chrs == "") return false;
  187. if (!strict && pos <= 1 && buffer[0] === '0' && new RegExp("[\\d-]").test(chrs) && buffer.join('').length == 1) { //handle first char
  188. buffer[0] = "";
  189. return { "pos": 0 };
  190. }
  191. var cbuf = strict ? buffer.slice(0, pos) : buffer.slice();
  192. cbuf.splice(pos, 0, chrs);
  193. var bufferStr = cbuf.join('');
  194. //strip groupseparator
  195. var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator);
  196. bufferStr = bufferStr.replace(new RegExp(escapedGroupSeparator, "g"), '');
  197. if (strict && bufferStr.lastIndexOf(opts.radixPoint) == bufferStr.length - 1) {
  198. var escapedRadixPoint = $.inputmask.escapeRegex.call(this, opts.radixPoint);
  199. bufferStr = bufferStr.replace(new RegExp(escapedRadixPoint, "g"), '');
  200. }
  201. if (!strict && bufferStr == "") return false;
  202. var isValid = opts.regex.number(iopts).test(bufferStr);
  203. if (!isValid) {
  204. //let's help the regex a bit
  205. bufferStr += "0";
  206. isValid = opts.regex.number(iopts).test(bufferStr);
  207. if (!isValid) {
  208. //make a valid group
  209. var lastGroupSeparator = bufferStr.lastIndexOf(opts.groupSeparator);
  210. for (var i = bufferStr.length - lastGroupSeparator; i <= 3; i++) {
  211. bufferStr += "0";
  212. }
  213. isValid = opts.regex.number(iopts).test(bufferStr);
  214. if (!isValid && !strict) {
  215. if (chrs == opts.radixPoint) {
  216. isValid = opts.regex.number(iopts).test("0" + bufferStr + "0");
  217. if (isValid) {
  218. buffer[pos] = "0";
  219. pos++;
  220. return { "pos": pos };
  221. }
  222. }
  223. }
  224. }
  225. }
  226. if (isValid != false && !strict && chrs != opts.radixPoint) {
  227. var newPos = opts.postFormat(buffer, pos, (chrs == "-" || chrs == "+") ? true : false, opts);
  228. return { "pos": newPos, "refreshFromBuffer": true };
  229. }
  230. return isValid;
  231. },
  232. cardinality: 1,
  233. prevalidator: null
  234. }
  235. },
  236. insertMode: true,
  237. autoUnmask: false
  238. },
  239. 'integer': {
  240. regex: {
  241. number: function (opts) {
  242. var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator);
  243. var signedExpression = opts.allowPlus || opts.allowMinus ? "[" + (opts.allowPlus ? "\+" : "") + (opts.allowMinus ? "-" : "") + "]?" : "";
  244. return new RegExp("^" + signedExpression + "(\\d+|\\d{1," + opts.groupSize + "}((" + escapedGroupSeparator + "\\d{" + opts.groupSize + "})?)+)$");
  245. }
  246. },
  247. alias: "decimal"
  248. }
  249. });
  250. })(jQuery);