jquery-maskmoney
jquery-maskmoney copied to clipboard
Allow zero and null values
I noticed that I can have zero value or null value but not the both.
If the user is permited to delete all the values so this could be an empty value to be submited instead of keeping zero (if zeroAllowed = true)
Is there any solution for this at the moment?
Thanks
you can try allowZero: true, defaultZero: false
The problem is that if I use allowZero, the field always get with 0.00 instead of empty value. Doesnt matter if I type some value or not. The only difference with defaultZero is that the field starts with zero value. With defaultZero:false and allowZero:true I get the mask without any value and when I changed the focus of the field, the value becomes 0.00
Thanks
@marcoshmuniz gotcha. I will keep this open since we need to make some changes in order to make it work
+1 on this, it´s totally common scenario
+1 on this. I am having the same problem. There should be a way to allow the field to be null and zero. Maybe a new option called emptyAllowed.
Another way to solve it is to create an option to set the default value, so we can check if the field is empty on blur and if so, fill it with the default value. This way, we can set the default to an empty string.
+1, same issue. If someone ever sets the field they can never unset it.
+1, I'm having the same problem:
- I need to show "$ 0.00" only if the value is 0.
- If the value is "null", I need to show the field as empty.
@marcoshmuniz @thelinuxlich @vbmendes @hadees @abrhbruno I'm trying to think here, but I really can't come up with a good idea of how to know if the current "zero" is just because the users entered the input and then exited(and we set the default mask) or because he actually typed "0".
The only idea I had is to store the chars that the user type in a variable(for example) and then checks if it keeps the value or if it delete the numbers, check if some zero was left(which I think is a terrible idea to implement).
@marcoshmuniz @thelinuxlich @vbmendes @hadees @abrhbruno guys? :)
+1, i would love to see an option allowEmpty or something,. If false - it should works as it does today, but if true, and user clears the content of input field, blur on empty field is not formatting content to 0.00 but leaves field empty instead.
@ValeryG to achieve what you explained, you just need to set allowZero
to true
(default is false
) :)
Not really. When allowZero is set to true and content of the field is cleared, blur action re-formats empty field to 0.00 and what i am suggesting to have a setting that will leave empty field empty on blur.
@ValeryG so, it goes back to my question above: how to know if the current "zero" is just because the users entered the input and then exited(and we set the default mask) or because he actually typed "0"?
Can't you set a flag if user deletes all the input in the field?
The way I expect it to work is I click on the field and it'll go to my default mask of 0.00
but if I delete that then I'm really looking for nothing in the field. If I click back into the field I think it's ok for it to go back to 0.00
.
Maybe use the field focus for this? So it'll only set the default when a field comes into focus and is empty. I think that would let me delete the contents right?
@plentz What do you think about storing the typed string value, without the mask, in an internal variable. So you can mask this value anytime you need to show it.
If this internal value is empty, it means the user deleted all the content. In this case, you show the default mask until the blur event. In the blur event, if this internal value is empty, you delete the field's content. Otherwise, you keep the masked value.
@vbmendes that's the same approach I described above and I think it's the best and only one till now :)
If anyone want's to send a pull request, it's very welcome :+1:
It looks like defaultZero
is not a setting in v3.0. I'll take a shot at this as I am having this problem myself.
@jpSimkins thanks for working on this :)
do you mind adding some tests to ensure that what you've done actually work(and that I will not break it in the feature :) )
Tests are added. I also removed a useless line of code from my changes.
I updated the pull request since it wouldn't auto merge from the allowNegative
update.
In a nutshell, this works by checking for the zero value and if it is deleted a flag is set to keep empty. This allows you to use allowZero
as expected but will also give you the ability to have zero values and empty.
+1, in my scenario, zero is value and it's an modifier. Empty value, do nothing in my scenario, zero value do something. By this reason, I need give to user the option of put empty or zero values and persist this value.
@jpSimkins , allowEmpty works very nice here! Thanks!
+1
i made a little adjustment to the .js by the line 335 where you check if the key pressed is the backspace it checks if the input value is "0.00', it makes de input value ""
it's important that it checks the value before changing it, so you can have the 0.00 value