Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

inputmask in a text box -- numeric only >0 1

Status
Not open for further replies.

scklifasovskiy

Programmer
Nov 12, 2012
57
CA
Hi all,
Another silly question about inputmask.
I need the textbox to allow only positive numbers.
 
How many solutions would you like?

Take one of these:

1. In Valid RETURN This.Value>0
2. In InteractiveChange Set THIS.Value = ABS(This.Value)
3. In Keypress suppress the - key, but also suppress CTRL+V, less doable
4. Use a spinner instead of textbox conrol , you can set MinValue 0

There is no way to solve that with inputmask, as both # and 9 allow entry of a sign, too

Bye, Olaf.
 
One more and most important recommendation:

Validate your data inbiz objects, not in controls. You need to assure data is consistent and by rules on that level more than anywhere else, not on the control level. For example you don't want to allow negative prices, then the best thing is to make a field rule in the table, on top of that have a biz rule to never let the database throw an error because you never save a negative price and only in third place let your UI handle that.

Bye, Olaf.
 
You've also got to consider whether the user needs to be aware of the fact that negative numbers are not allowed.

If users sometimes enter negative prices (say), without realising that the application does not support these, then the best approach might be to do the check in the Valid event, and to display an error message if the check fails. The message should inform the user of the rule in question, and explain (briefly) the reason for it.

But if, on the other hand, you have a rule that says than any negative price (say) should automatically be considered positive, then setting THIS.Value = ABS(This.Value) in the LostFocus might be a better approach.

My point is that you can't necessarily leave the user out of the decision.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
You have everything you need.

If your numbers are integer then you can try TextBox inputmask of the form 99999 which will allow only 0..9 digits.
The downside is you have to do conversion using str() when reading and val() when doing mathematical operations.

nasib


 
Good, Nasib, that indeed also works.

Help topic says "9 - Permits entry of digits and signs."

I think the easiest solution still is using the spinner control with its minimum value in SpinnerLowValue and KeyboardLowValue. Set both 0 and you can't enter anything wrong. Additional to that SpinnerHighvalue and KeyboardHighvalue can be used to specify a maximum value. You can also make a difference to what can be entered via keyboard and via spinner buttons.

Also what Mike said is still true, the user should be involved and informed about the valid value range, if it's not obvious. Negative prices are a possible solution to a discount or a refund, for example, it's not a natural to disallow them. But there might be a different place in a POS app to handle refunds.

Bye, Olaf.
 
Yes Olaf: it should say
"9 - Permits entry of digits and signs, when TextBox is not a char type"

I think valid() return value > 0 is very practical and neat. You have all the numeric properties of the TextBox that you could play with including formating with decimal and ',' etc. (99,999.99)



nasib

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top