jquery-maskmoney icon indicating copy to clipboard operation
jquery-maskmoney copied to clipboard

Treat empty as zero

Open edelbalso opened this issue 11 years ago • 8 comments

This pull request adds a new option to the library: treatEmptyAsZero.

Currently in this library, when focusing on an empty field, the masking library assumes the user intends "blank" to mean 0. I needed empty to mean "nothing" and allow the user to specify either "nothing" or 0 as the submitted value.

Here, setting treatEmptyAsZero to false allows you to delete a 0.00 masked value and allows the user to enter either a blank input or zero.

edelbalso avatar Feb 20 '14 03:02 edelbalso

The "treatEmptyAsZero" can be set on submit form event too ?

dbasilioesp avatar Feb 27 '14 17:02 dbasilioesp

How is this different from: https://github.com/plentz/jquery-maskmoney/pull/110? Seems to be the same thing.

jpSimkins avatar Mar 10 '14 15:03 jpSimkins

Yes and No.

The treatEmptyAsZero that I was thinking is just the zero, so in this test could be like this:

test("allowZero: true - mask keeps default 0 value", function() {
  var input = $("#input5").maskMoney();
  input.trigger("mask");
  equal(input.val(), "0.00", "0 value should be 0.00$ due to settings");
}); 

dbasilioesp avatar Mar 12 '14 16:03 dbasilioesp

Nice. +1

jpSimkins avatar Mar 17 '14 19:03 jpSimkins

Hey guys, thanks for looking. I needed to set this up for a personal project. Maybe I screwed up, but when I tried #110, it didn't give me the behavior I needed so I hacked it up myself and put it here in case it helps someone.

With this PR, if you set treatEmptyAsZero as false, then say you enter a value and it gets masked to $117,000. If you then select all in the field and hit backspace, it gets set to $0 and places the cursor to the right of $0. If you then hit backspace again, it will allow you to delete the $0 and have the field be considered 'nil'.

It's the best of both worlds for us and so I thought I'd share. I do, however, think I prefer @jpSimkins terminology of allowEmpty, I just didn't think of it.

Happy to rework it accordingly if you guys like the code/behavior, plan to merge it but need me to make any changes. Let me know!

edelbalso avatar Mar 17 '14 22:03 edelbalso

Great, this is what I was looking for! +1

famoraes avatar May 23 '14 17:05 famoraes

After 3 years, today I discovered that I need this feature in the plugin. Should be merged. +1.

masiorama avatar Aug 07 '17 14:08 masiorama

@edelbalso do you can resolve this conflict?

aureliosaraiva avatar Aug 07 '17 14:08 aureliosaraiva