| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- /*
- Input Mask plugin extensions
- http://github.com/RobinHerbots/jquery.inputmask
- Copyright (c) 2010 - 2014 Robin Herbots
- Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
- Version: 0.0.0
- Optional extensions on the jquery.inputmask base
- */
- (function (factory) {
- if (typeof define === 'function' && define.amd) {
- define("jquery.inputmask.numeric.extensions", ['jquery', 'jquery.inputmask'], factory);
- } else {
- factory(jQuery);
- }
- }(function ($) {
- //number aliases
- $.extend($.inputmask.defaults.aliases, {
- 'numeric': {
- mask: function (opts) {
- if (opts.repeat !== 0 && isNaN(opts.integerDigits)) {
- opts.integerDigits = opts.repeat;
- }
- opts.repeat = 0;
- opts.autoGroup = opts.autoGroup && opts.groupSeparator != "";
- if (opts.autoGroup && isFinite(opts.integerDigits)) {
- var seps = Math.floor(opts.integerDigits / opts.groupSize);
- var mod = opts.integerDigits % opts.groupSize;
- opts.integerDigits += mod == 0 ? seps - 1 : seps;
- }
- opts.definitions[":"].placeholder = opts.radixPoint;
- var mask = opts.prefix;
- mask += "[+]";
- mask += "~{1," + opts.integerDigits + "}";
- if (opts.digits != undefined && (isNaN(opts.digits) || parseInt(opts.digits) > 0)) {
- if (opts.digitsOptional)
- mask += "[" + ":" + "~{" + opts.digits + "}]";
- else mask += ":" + "~{" + opts.digits + "}";
- }
- mask += opts.suffix;
- return mask;
- },
- placeholder: "",
- greedy: false,
- digits: "*", //number of fractionalDigits
- digitsOptional: true,
- groupSeparator: "",//",", // | "."
- radixPoint: ".",
- groupSize: 3,
- autoGroup: false,
- allowPlus: true,
- allowMinus: true,
- integerDigits: "+", //number of integerDigits
- prefix: "",
- suffix: "",
- rightAlign: true,
- postFormat: function (buffer, pos, reformatOnly, opts) { //this needs to be removed // this is crap
- var needsRefresh = false, charAtPos = buffer[pos];
- if (opts.groupSeparator == "" ||
- ($.inArray(opts.radixPoint, buffer) != -1 && pos >= $.inArray(opts.radixPoint, buffer)) ||
- new RegExp('[-\+]').test(charAtPos)
- ) return { pos: pos };
- var cbuf = buffer.slice();
- if (charAtPos == opts.groupSeparator) {
- cbuf.splice(pos--, 1);
- charAtPos = cbuf[pos];
- }
- if (reformatOnly) cbuf[pos] = "?"; else cbuf.splice(pos, 0, "?"); //set position indicator
- var bufVal = cbuf.join('');
- if (opts.autoGroup || (reformatOnly && bufVal.indexOf(opts.groupSeparator) != -1)) {
- var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator);
- needsRefresh = bufVal.indexOf(opts.groupSeparator) == 0;
- bufVal = bufVal.replace(new RegExp(escapedGroupSeparator, "g"), '');
- var radixSplit = bufVal.split(opts.radixPoint);
- bufVal = radixSplit[0];
- if (bufVal != (opts.prefix + "?0") && bufVal.length >= (opts.groupSize + opts.prefix.length)) {
- needsRefresh = true;
- var reg = new RegExp('([-\+]?[\\d\?]+)([\\d\?]{' + opts.groupSize + '})');
- while (reg.test(bufVal)) {
- bufVal = bufVal.replace(reg, '$1' + opts.groupSeparator + '$2');
- bufVal = bufVal.replace(opts.groupSeparator + opts.groupSeparator, opts.groupSeparator);
- }
- }
- if (radixSplit.length > 1)
- bufVal += opts.radixPoint + radixSplit[1];
- }
- buffer.length = bufVal.length; //align the length
- for (var i = 0, l = bufVal.length; i < l; i++) {
- buffer[i] = bufVal.charAt(i);
- }
- var newPos = $.inArray("?", buffer);
- if (reformatOnly) buffer[newPos] = charAtPos; else buffer.splice(newPos, 1);
- return { pos: newPos, "refreshFromBuffer": needsRefresh };
- },
- onKeyDown: function (e, buffer, caretPos, opts) {
- if (opts.autoGroup && (e.keyCode == opts.keyCode.DELETE || e.keyCode == opts.keyCode.BACKSPACE)) {
- var rslt = opts.postFormat(buffer, caretPos - 1, true, opts);
- rslt.caret = rslt.pos + 1;
- return rslt;
- }
- },
- onKeyPress: function (e, buffer, caretPos, opts) {
- if (opts.autoGroup /*&& String.fromCharCode(k) == opts.radixPoint*/) {
- var rslt = opts.postFormat(buffer, caretPos - 1, true, opts);
- rslt.caret = rslt.pos + 1;
- return rslt;
- }
- },
- regex: {
- integerPart: function (opts) { return new RegExp('[-\+]?\\d+'); }
- },
- negationhandler: function (chrs, buffer, pos, strict, opts) {
- if (!strict && opts.allowMinus && chrs === "-") {
- var matchRslt = buffer.join('').match(opts.regex.integerPart(opts));
- if (matchRslt.length > 0) {
- if (buffer[matchRslt.index] == "+") {
- return { "pos": matchRslt.index, "c": "-", "remove": matchRslt.index, "caret": pos };
- } else if (buffer[matchRslt.index] == "-") {
- return { "remove": matchRslt.index, "caret": pos - 1 };
- } else {
- return { "pos": matchRslt.index, "c": "-", "caret": pos + 1 };
- }
- }
- }
- return false;
- },
- definitions: {
- '~': {
- validator: function (chrs, maskset, pos, strict, opts) {
- var isValid = opts.negationhandler(chrs, maskset.buffer, pos, strict, opts);
- if (!isValid) {
- isValid = strict ? new RegExp("[0-9" + $.inputmask.escapeRegex.call(this, opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs);
- if (isValid === true) isValid = { pos: pos };
- if (isValid != false && !strict) {
- //handle 0 for integerpart
- var matchRslt = maskset.buffer.join('').match(opts.regex.integerPart(opts)), radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
- if (matchRslt) {
- if (matchRslt["0"].indexOf("0") == 0 && pos >= opts.prefix.length) {
- if (radixPosition == -1 || (pos <= radixPosition && maskset["validPositions"][radixPosition] == undefined)) {
- maskset.buffer.splice(matchRslt.index, 1);
- pos = pos > matchRslt.index ? pos - 1 : matchRslt.index;
- $.extend(isValid, { "pos": pos, "remove": matchRslt.index });
- } else if (pos > matchRslt.index && pos <= radixPosition) {
- maskset.buffer.splice(matchRslt.index, 1);
- pos = pos > matchRslt.index ? pos - 1 : matchRslt.index;
- $.extend(isValid, { "pos": pos, "remove": matchRslt.index });
- }
- } else if (chrs == "0" && pos <= matchRslt.index) {
- return false;
- }
- }
- //handle overwrite when fixed precision
- if (opts.digitsOptional === false && pos > radixPosition) {
- return { "pos": pos, "remove": pos };
- }
- }
- }
- return isValid;
- },
- cardinality: 1,
- prevalidator: null
- },
- '+': {
- validator: function (chrs, maskset, pos, strict, opts) {
- var signed = "[";
- if (opts.allowMinus === true) signed += "-";
- if (opts.allowPlus === true) signed += "\+";
- signed += "]";
- var isValid = new RegExp(signed).test(chrs);
- return isValid;
- },
- cardinality: 1,
- prevalidator: null
- },
- ':': {
- validator: function (chrs, maskset, pos, strict, opts) {
- var isValid = opts.negationhandler(chrs, maskset.buffer, pos, strict, opts);
- if (!isValid) {
- var radix = "[" + $.inputmask.escapeRegex.call(this, opts.radixPoint) + "]";
- isValid = new RegExp(radix).test(chrs);
- if (isValid && maskset["validPositions"][pos] && maskset["validPositions"][pos]["match"].placeholder == opts.radixPoint) {
- isValid = { "pos": pos, "remove": pos };
- }
- }
- return isValid;
- },
- cardinality: 1,
- prevalidator: null,
- placeholder: "" //radixpoint will be set in the mask function
- }
- },
- insertMode: true,
- autoUnmask: false,
- onUnMask: function (maskedValue, unmaskedValue, opts) {
- var processValue = maskedValue.replace(opts.prefix, "");
- processValue = processValue.replace(opts.suffix, "");
- processValue = processValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), "");
- //processValue = processValue.replace($.inputmask.escapeRegex.call(this, opts.radixPoint), ".");
- return processValue;
- },
- isComplete: function (buffer, opts) {
- var maskedValue = buffer.join(''), bufClone = buffer.slice();
- //verify separator positions
- opts.postFormat(bufClone, 0, true, opts);
- if (bufClone.join('') != maskedValue) return false;
- var processValue = maskedValue.replace(opts.prefix, "");
- processValue = processValue.replace(opts.suffix, "");
- processValue = processValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), "");
- processValue = processValue.replace($.inputmask.escapeRegex.call(this, opts.radixPoint), ".");
- return isFinite(processValue);
- },
- onBeforeMask: function (initialValue, opts) {
- if (isFinite(initialValue)) {
- return initialValue.toString().replace(".", opts.radixPoint);
- } else {
- var kommaMatches = initialValue.match(/,/g);
- var dotMatches = initialValue.match(/\./g);
- if (dotMatches && kommaMatches) {
- if (dotMatches.length > kommaMatches.length) {
- initialValue = initialValue.replace(/\./g, "");
- initialValue = initialValue.replace(",", opts.radixPoint);
- } else if (kommaMatches.length > dotMatches.length) {
- initialValue = initialValue.replace(/,/g, "");
- initialValue = initialValue.replace(".", opts.radixPoint);
- }
- } else {
- initialValue = initialValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), "");
- }
- return initialValue;
- }
- }
- },
- 'decimal': {
- alias: "numeric"
- },
- 'integer': {
- alias: "numeric",
- digits: "0"
- }
- });
- }));
|