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

Allow zero and null values

Open marcoshmuniz opened this issue 11 years ago • 24 comments

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

marcoshmuniz avatar Jun 30 '13 03:06 marcoshmuniz

you can try allowZero: true, defaultZero: false

plentz avatar Jun 30 '13 17:06 plentz

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 avatar Jul 01 '13 12:07 marcoshmuniz

@marcoshmuniz gotcha. I will keep this open since we need to make some changes in order to make it work

plentz avatar Jul 04 '13 20:07 plentz

+1 on this, it´s totally common scenario

thelinuxlich avatar Aug 13 '13 22:08 thelinuxlich

+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.

vbmendes avatar Aug 22 '13 21:08 vbmendes

+1, same issue. If someone ever sets the field they can never unset it.

hadees avatar Sep 26 '13 01:09 hadees

+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.

abrhbruno avatar Nov 27 '13 20:11 abrhbruno

@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).

plentz avatar Dec 22 '13 01:12 plentz

@marcoshmuniz @thelinuxlich @vbmendes @hadees @abrhbruno guys? :)

plentz avatar Dec 28 '13 02:12 plentz

+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 avatar Jan 02 '14 16:01 ValeryG

@ValeryG to achieve what you explained, you just need to set allowZero to true(default is false) :)

plentz avatar Jan 02 '14 16:01 plentz

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 avatar Jan 02 '14 16:01 ValeryG

@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"?

plentz avatar Jan 02 '14 16:01 plentz

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?

hadees avatar Jan 02 '14 17:01 hadees

@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 avatar Jan 10 '14 16:01 vbmendes

@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:

plentz avatar Jan 10 '14 23:01 plentz

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 avatar Jan 16 '14 20:01 jpSimkins

@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 :) )

plentz avatar Jan 18 '14 01:01 plentz

Tests are added. I also removed a useless line of code from my changes.

jpSimkins avatar Jan 18 '14 04:01 jpSimkins

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.

jpSimkins avatar Jan 20 '14 05:01 jpSimkins

+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.

bernardolm avatar Sep 10 '14 14:09 bernardolm

@jpSimkins , allowEmpty works very nice here! Thanks!

alispat avatar Nov 01 '14 00:11 alispat

+1

nyxcalamity avatar Mar 25 '15 14:03 nyxcalamity

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

Ijimero avatar Jun 05 '17 16:06 Ijimero