|
|
@@ -155,6 +155,7 @@
|
|
|
insertMode: true,
|
|
|
autoUnmask: false,
|
|
|
unmaskAsNumber: false,
|
|
|
+ inputType: "text", //number ~ indicates whether the value passed for initialization is text or a number
|
|
|
inputmode: "numeric",
|
|
|
preValidation: function (buffer, pos, c, isSelection, opts, maskset) {
|
|
|
if (c === "-" || c === opts.negationSymbol.front) {
|
|
|
@@ -539,72 +540,39 @@
|
|
|
},
|
|
|
onBeforeMask: function (initialValue, opts) {
|
|
|
opts.isNegative = undefined;
|
|
|
+ var radixPoint = opts.radixPoint || ",";
|
|
|
|
|
|
- if (typeof initialValue == "number" && opts.radixPoint !== "") {
|
|
|
- initialValue = initialValue.toString().replace(".", opts.radixPoint);
|
|
|
+ if ((typeof initialValue == "number" || opts.inputType === "number") && radixPoint !== "") {
|
|
|
+ initialValue = initialValue.toString().replace(".", radixPoint);
|
|
|
}
|
|
|
|
|
|
- initialValue = initialValue.toString().charAt(initialValue.length - 1) === opts.radixPoint ?
|
|
|
- initialValue.toString().substr(0, initialValue.length - 1) : initialValue.toString();
|
|
|
+ var valueParts = initialValue.split(radixPoint),
|
|
|
+ integerPart = valueParts[0].replace(/[^\-0-9]/g, ""),
|
|
|
+ decimalPart = valueParts.length > 1 ? valueParts[1].replace(/[^0-9]/g, "") : "";
|
|
|
|
|
|
- if (opts.radixPoint !== "" && isFinite(initialValue)) {
|
|
|
- var vs = initialValue.split("."),
|
|
|
- groupSize = opts.groupSeparator !== "" ? parseInt(opts.groupSize) : 0;
|
|
|
- if (vs.length === 2 && (vs[0].length > groupSize || vs[1].length > groupSize || (vs[0].length <= groupSize && vs[1].length < groupSize))) {
|
|
|
- initialValue = initialValue.replace(".", opts.radixPoint);
|
|
|
- }
|
|
|
- }
|
|
|
- var kommaMatches = initialValue.match(/,/g) || [],
|
|
|
- dotMatches = initialValue.match(/\./g) || [];
|
|
|
- if (dotMatches.length > kommaMatches.length && kommaMatches.length !== 0) {
|
|
|
- initialValue = initialValue.replace(/\./g, "");
|
|
|
- if (opts.radixPoint !== "")
|
|
|
- initialValue = initialValue.replace(",", opts.radixPoint);
|
|
|
- } else if (kommaMatches.length > dotMatches.length && dotMatches.length !== 0) {
|
|
|
- initialValue = initialValue.replace(/,/g, "");
|
|
|
- if (opts.radixPoint !== "")
|
|
|
- initialValue = initialValue.replace(".", opts.radixPoint);
|
|
|
- } else if (kommaMatches === dotMatches) {
|
|
|
- if (initialValue.indexOf(".") < initialValue.indexOf(",")) {
|
|
|
- initialValue = initialValue.replace(/\./g, "");
|
|
|
- if (opts.radixPoint !== "")
|
|
|
- initialValue = initialValue.replace(",", opts.radixPoint);
|
|
|
- } else {
|
|
|
- initialValue = initialValue.replace(/,/g, "");
|
|
|
- if (opts.radixPoint !== "")
|
|
|
- initialValue = initialValue.replace(".", opts.radixPoint);
|
|
|
- }
|
|
|
- } else {
|
|
|
-
|
|
|
- initialValue = initialValue.replace(new RegExp(Inputmask.escapeRegex(initialValue.indexOf("0") === 0 ? opts.radixPoint : opts.groupSeparator), "g"), "");
|
|
|
- initialValue = initialValue.replace(new RegExp(Inputmask.escapeRegex(initialValue.indexOf("0") === 0 ? opts.radixPoint : opts.groupSeparator), "g"), "");
|
|
|
- }
|
|
|
+ initialValue = integerPart + (decimalPart !== "" ? radixPoint + decimalPart : decimalPart);
|
|
|
|
|
|
var digits = 0;
|
|
|
- if (opts.radixPoint !== "" && initialValue.indexOf(opts.radixPoint) !== -1) {
|
|
|
- var valueParts = initialValue.split(opts.radixPoint),
|
|
|
- digits = valueParts[1].match(new RegExp("\\d*"))[0].length,
|
|
|
- digitsFactor = Math.pow(10, digits || 1);
|
|
|
- if (isFinite(opts.digits)) {
|
|
|
- digits = parseInt(opts.digits);
|
|
|
- digitsFactor = Math.pow(10, digits);
|
|
|
- }
|
|
|
-
|
|
|
- //make the initialValue a valid javascript number for the parsefloat
|
|
|
- initialValue = initialValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".");
|
|
|
- if (isFinite(initialValue))
|
|
|
- initialValue = Math.round(parseFloat(initialValue) * digitsFactor) / digitsFactor;
|
|
|
- initialValue = initialValue.toString().replace(".", opts.radixPoint);
|
|
|
- }
|
|
|
+ if (radixPoint !== "") {
|
|
|
+ digits = decimalPart.length;
|
|
|
+ if (decimalPart !== "") {
|
|
|
+ var digitsFactor = Math.pow(10, digits || 1);
|
|
|
+ if (isFinite(opts.digits)) {
|
|
|
+ digits = parseInt(opts.digits);
|
|
|
+ digitsFactor = Math.pow(10, digits);
|
|
|
+ }
|
|
|
|
|
|
- if (opts.digits === 0) {
|
|
|
- if (initialValue.indexOf(".") !== -1) {
|
|
|
- initialValue = initialValue.substring(0, initialValue.indexOf("."));
|
|
|
- } else if (initialValue.indexOf(",") !== -1) {
|
|
|
- initialValue = initialValue.substring(0, initialValue.indexOf(","));
|
|
|
+ //make the initialValue a valid javascript number for the parsefloat
|
|
|
+ initialValue = initialValue.replace(Inputmask.escapeRegex(radixPoint), ".");
|
|
|
+ if (isFinite(initialValue))
|
|
|
+ initialValue = Math.round(parseFloat(initialValue) * digitsFactor) / digitsFactor;
|
|
|
+ initialValue = initialValue.toString().replace(".", radixPoint);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+ //this needs to be in a separate part and not directly in decimalPart to allow rounding
|
|
|
+ if (opts.digits === 0 && initialValue.indexOf(Inputmask.escapeRegex(radixPoint)) !== -1) {
|
|
|
+ initialValue = initialValue.substring(0, initialValue.indexOf(Inputmask.escapeRegex(radixPoint)));
|
|
|
+ }
|
|
|
return alignDigits(initialValue.toString().split(""), digits, opts).join("");
|
|
|
},
|
|
|
onKeyDown: function (e, buffer, caretPos, opts) {
|