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

Treat empty as zero #113

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

edelbalso
Copy link

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.

- Added tests for blur event
- Added tests for focus
- Updated documentation
@dbasilioesp
Copy link

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

@jpSimkins
Copy link

How is this different from: #110? Seems to be the same thing.

@dbasilioesp
Copy link

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");
}); 

@jpSimkins
Copy link

Nice. +1

@edelbalso
Copy link
Author

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!

@famoraes
Copy link

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

@plentz plentz added the feature label May 25, 2014
@masiorama
Copy link

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

@aureliosaraiva
Copy link
Collaborator

@edelbalso do you can resolve this conflict?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants