Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Precisions "auto" and "custom" added #238

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ The options that you can set are:
* `affixesStay`: set if the prefix and suffix will stay in the field's value after the user exits the field. default: true
* `thousands`: the thousands separator. default: ','
* `decimal`: the decimal separator. default: '.'
* `precision`: how many decimal places are allowed. default: 2
* `precision`: how many decimal places are allowed. use 'auto' for get the precision from the value or 'custom' for no limitations. default: 2
* `allowZero`: use this setting to prevent users from inputing zero. default: false
* `allowNegative`: use this setting to prevent users from inputing negative values. default: false

Expand Down Expand Up @@ -89,6 +89,7 @@ npm install && grunt test
* [Daniel Loureiro](https://github.com/loureirorg)
* [Thiago Silva](http://twitter.com/tafs7/)
* [Guilherme Nagatomo](https://github.com/guilhermehn)
* [Carlos Eduardo](https://github.com/Dudu197)

***
### License:
Expand Down
87 changes: 55 additions & 32 deletions dist/jquery.maskMoney.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/*
* jquery-maskmoney - v3.0.2
* jquery-maskmoney - v3.1.1
* jQuery plugin to mask data entry in the input text in the form of money (currency)
* https://github.com/plentz/jquery-maskmoney
*
* Made by Diego Plentz
* Under MIT License (https://raw.github.com/plentz/jquery-maskmoney/master/LICENSE)
* Under MIT License
*/

(function ($) {
"use strict";
if (!$.browser) {
Expand All @@ -17,7 +18,7 @@
}

var methods = {
destroy : function () {
destroy: function () {
$(this).unbind(".maskMoney");

if ($.browser.msie) {
Expand All @@ -26,31 +27,45 @@
return this;
},

mask : function (value) {
mask: function (value) {
return this.each(function () {
var $this = $(this),
decimalSize;
var $this = $(this);
if (typeof value === "number") {
$this.trigger("mask");
decimalSize = $($this.val().split(/\D/)).last()[0].length;
value = value.toFixed(decimalSize);
$this.val(value);
}
return $this.trigger("mask");
});
},

unmasked : function () {
getPrecision: function($item, settings) {
var precision = 0;
if (settings.precision == "auto" || settings.precision == "custom") {
if (settings.precision == "auto" && $item.attr("precision") != undefined) {
return parseInt($item.attr("precision"));
}
var value = $item.val();
var split = value.split(settings.decimal);
if (split.length == 2) {
precision = split[1].length;
}
$item.attr("precision", precision);
} else {
precision = settings.precision
}
return precision;
},

unmasked: function () {
return this.map(function () {
var value = ($(this).val() || "0"),
isNegative = value.indexOf("-") !== -1,
decimalPart;
// get the last position of the array that is a number(coercion makes "" to be evaluated as false)
$(value.split(/\D/).reverse()).each(function (index, element) {
if(element) {
if (element) {
decimalPart = element;
return false;
}
}
});
value = value.replace(/\D/g, "");
value = value.replace(new RegExp(decimalPart + "$"), "." + decimalPart);
Expand All @@ -61,23 +76,24 @@
});
},

init : function (settings) {
settings = $.extend({
init: function (parameters) {
parameters = $.extend({
prefix: "",
suffix: "",
affixesStay: true,
thousands: ",",
decimal: ".",
precision: 2,
precision: "auto",
allowZero: false,
allowNegative: false
}, settings);
}, parameters);

return this.each(function () {
var $input = $(this),
var $input = $(this), settings,
onFocusValue;

// data-* api
settings = $.extend({}, parameters);
settings = $.extend(settings, $input.data());

function getInputSelection() {
Expand Down Expand Up @@ -169,7 +185,8 @@
function maskValue(value) {
var negative = (value.indexOf("-") > -1 && settings.allowNegative) ? "-" : "",
onlyNumbers = value.replace(/[^0-9]/g, ""),
integerPart = onlyNumbers.slice(0, onlyNumbers.length - settings.precision),
precision = methods.getPrecision($input, settings),
integerPart = onlyNumbers.slice(0, onlyNumbers.length - precision),
newValue,
decimalPart,
leadingZeros;
Expand All @@ -183,14 +200,15 @@
}
newValue = negative + integerPart;

if (settings.precision > 0) {
decimalPart = onlyNumbers.slice(onlyNumbers.length - settings.precision);
leadingZeros = new Array((settings.precision + 1) - decimalPart.length).join(0);
if (precision > 0) {
decimalPart = onlyNumbers.slice(onlyNumbers.length - precision);
leadingZeros = new Array((precision + 1) - decimalPart.length).join(0);
newValue += settings.decimal + leadingZeros + decimalPart;
}
return setSymbol(newValue);
}


function maskAndPosition(startPos) {
var originalLen = $input.val().length,
newLen;
Expand All @@ -202,6 +220,10 @@

function mask() {
var value = $input.val();
var precision = methods.getPrecision($input, settings);
if (precision > 0 && value.indexOf(settings.decimal) < 0) {
value += settings.decimal + new Array(precision + 1).join(0);
}
$input.val(maskValue(value));
}

Expand Down Expand Up @@ -240,16 +262,16 @@
}

// any key except the numbers 0-9
if (key < 48 || key > 57) {
if ((key < 48 || key > 57) && settings.precision !== "custom") {
// -(minus) key
if (key === 45) {
$input.val(changeSign());
return false;
// +(plus) key
// +(plus) key
} else if (key === 43) {
$input.val($input.val().replace("-", ""));
return false;
// enter key or tab key
// enter key or tab key
} else if (key === 13 || key === 9) {
return true;
} else if ($.browser.mozilla && (key === 37 || key === 39) && e.charCode === 0) {
Expand Down Expand Up @@ -309,7 +331,7 @@
startPos = value.length - lastNumber - 1;
endPos = startPos + 1;
}
//delete
//delete
} else {
endPos += 1;
}
Expand All @@ -331,22 +353,23 @@
mask();
var input = $input.get(0),
textRange;
if (input.createTextRange) {
if (settings.precision !== "custom" && input.createTextRange) {
textRange = input.createTextRange();
textRange.collapse(false); // set the cursor at the end of the input
textRange.select();
}
}

function cutPasteEvent() {
setTimeout(function() {
setTimeout(function () {
mask();
}, 0);
}

function getDefaultMask() {
var n = parseFloat("0") / Math.pow(10, settings.precision);
return (n.toFixed(settings.precision)).replace(new RegExp("\\.", "g"), settings.decimal);
var precision = methods.getPrecision($input, settings);
var n = parseFloat("0") / Math.pow(10, precision);
return (n.toFixed(precision)).replace(new RegExp("\\.", "g"), settings.decimal);
}

function blurEvent(e) {
Expand Down Expand Up @@ -376,7 +399,7 @@
function clickEvent() {
var input = $input.get(0),
length;
if (input.setSelectionRange) {
if (settings.precision !== "custom" && input.setSelectionRange) {
length = $input.val().length;
input.setSelectionRange(length, length);
} else {
Expand All @@ -400,10 +423,10 @@
$.fn.maskMoney = function (method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === "object" || ! method) {
} else if (typeof method === "object" || !method) {
return methods.init.apply(this, arguments);
} else {
$.error("Method " + method + " does not exist on jQuery.maskMoney");
$.error("Method " + method + " does not exist on jQuery.maskMoney");
}
};
})(window.jQuery || window.Zepto);
Loading