diff --git a/README.md b/README.md index e424961..eb9976a 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,7 @@ The options that you can set are: * `precision`: how many decimal places are allowed. 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 + * `allowNoDecimal`: use this setting to allow decimal portions to be triggered manually by the user (in conjunction with thousands and decimal properties - make sure they are unique). default: false __IMPORTANT__: if you try to bind maskMoney to a read only field, nothing will happen, since we ignore completely read only fields. So, if you have a read only field, try to bind maskMoney to it, it will not work. Even if you change the field removing the readonly property, you will need to re-bind maskMoney to make it work. diff --git a/demo/index.html b/demo/index.html index e586b25..10de36b 100644 --- a/demo/index.html +++ b/demo/index.html @@ -58,6 +58,16 @@

jQuery-maskMoney examples

var num = $('#demo8').maskMoney('unmasked')[0]; alert('type: '+ typeof(num) + ', value: ' + num) + + + +
$("#demo9").maskMoney({ allowNoDecimal: true });
+ + + +
$("#demo10").maskMoney();
+
+ +
diff --git a/test/optional_decimals_test.js b/test/optional_decimals_test.js new file mode 100644 index 0000000..9bc2a5d --- /dev/null +++ b/test/optional_decimals_test.js @@ -0,0 +1,111 @@ +"use strict"; + +module("optional decimals"); + +test("testing basic entry with unenforced decimals",function() { + var input = $("#input1").maskMoney({ + allowNoDecimal: true + }); + + input.trigger("focus"); + keypress(input, 1); + keypress(input, 2); + keypress(input, 3); + keypress(input, 4); + keypress(input, 5); + keypress(input, 6); + keypress(input, 7); + + equal(input.val(), "1,234,567", "accept the input and format correctly"); + + // no idea why this doesn't work. so I'm hacking around it to simulate keypress + // keypress(input, "."); + input.val(input.val() + "."); + keypress(input, 8); + keypress(input, 9); + keypress(input, 0); + + equal(input.val(), "1,234,567.89", "accept the input and format correctly"); +}); + +test("testing basic entries with unenforced decimals and variable precision",function() { + var input = $("#input1").maskMoney({ + allowNoDecimal: true, + precision: 3 + }); + + input.trigger("focus"); + keypress(input, 1); + keypress(input, 2); + keypress(input, 3); + keypress(input, 4); + keypress(input, 5); + keypress(input, 6); + keypress(input, 7); + + equal(input.val(), "1,234,567", "accept the input and format correctly"); + + // no idea why this doesn't work. so I'm hacking around it to simulate keypress + // keypress(input, "."); + input.val(input.val() + "."); + keypress(input, 8); + keypress(input, 9); + keypress(input, 0); + + equal(input.val(), "1,234,567.890", "accept the input and format correctly"); + + keypress(input, 1); + + equal(input.val(), "1,234,567.890", "accept the input and format correctly"); +}); + +test("testing prefilled entries with unenforced decimals",function() { + var input = $("#input1").maskMoney({ + allowNoDecimal: true + }); + + input.val("1234567.890"); + input.trigger("focus"); + + equal(input.val(), "1,234,567.89", "accept the input and format correctly"); +}); + +test("testing prefilled entries with unenforced decimals and variable precision",function() { + var input = $("#input1").maskMoney({ + allowNoDecimal: true, + precision: 3 + }); + + input.val("1234567.890"); + input.trigger("focus"); + + equal(input.val(), "1,234,567.890", "accept the input and format correctly"); + + input.val("1234567.8901"); + input.trigger("focus"); + + equal(input.val(), "1,234,567.890", "accept the input and format correctly"); +}); + +test("testing unmask of unenforced decimals", function() { + var input = $("#input1").maskMoney({ + allowNoDecimal: true + }); + + input.val("1,234,567"); + input.maskMoney("mask"); + + equal(input.maskMoney("unmasked")[0], 1234567.00, "check appropriate unmasked precision"); + + + input.val("1234567.89"); + input.maskMoney("mask"); + + equal(input.maskMoney("unmasked")[0], 1234567.89); + + input.val("1234567.8901234"); + input.maskMoney("mask"); + + equal(input.maskMoney("unmasked")[0], 1234567.89); + +}); \ No newline at end of file diff --git a/test/unmasked_test.js b/test/unmasked_test.js index ab3c066..1b4f35b 100644 --- a/test/unmasked_test.js +++ b/test/unmasked_test.js @@ -4,7 +4,7 @@ module("unmasked"); test("with prefix", function() { var input = $("#input1"), unmasked; - input.val("+ 123.456,78"); + input.val("+ 123.456,78").maskMoney({ decimal: "," }); unmasked = input.maskMoney("unmasked")[0]; equal(unmasked, 123456.78, "unmask method return the correct number when the field value has prefix"); }); @@ -12,7 +12,7 @@ test("with prefix", function() { test("with suffix", function() { var input = $("#input1"), unmasked; - input.val("123.456,78 €"); + input.val("123.456,78 €").maskMoney({ decimal: "," }); unmasked = input.maskMoney("unmasked")[0]; equal(unmasked, 123456.78, "unmask method return the correct number when the field value has suffix"); }); @@ -20,7 +20,7 @@ test("with suffix", function() { test("with prefix and suffix", function() { var input = $("#input1"), unmasked; - input.val("+ 123.456,78 €"); + input.val("+ 123.456,78 €").maskMoney({ decimal: "," }); unmasked = input.maskMoney("unmasked")[0]; equal(unmasked, 123456.78, "unmask method return the correct number when the field value has prefix and suffix"); }); @@ -28,7 +28,7 @@ test("with prefix and suffix", function() { test("with negative number", function() { var input = $("#input1"), unmasked; - input.val("-R$ 123.456,78"); + input.val("-R$ 123.456,78").maskMoney({ decimal: "," }); unmasked = input.maskMoney("unmasked")[0]; equal(unmasked, -123456.78, "unmask method return the correct number when the field value has prefix and suffix"); }); @@ -36,8 +36,8 @@ test("with negative number", function() { test("with collection of fields", function() { var input = $(".all"), unmaskedCollection; - $("#input1").val("+ 123.456,78 €"); - $("#input2").val("R$ 876.543,21"); + $("#input1").val("+ 123.456,78 €").maskMoney({ decimal: "," }); + $("#input2").val("R$ 876.543,21").maskMoney({ decimal: "," }); unmaskedCollection = input.maskMoney("unmasked").get(); deepEqual(unmaskedCollection, [123456.78, 876543.21], "unmask method return the correct number when the field value has prefix and suffix"); });