Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not allow zero in textbox

I need a textbox which only the user can permit to enter integers. But the user can't enter zero. i.e, he can enter 10,100 etc. Not 0 alone. How can I make event in KeyDown?

like image 545
Sauron Avatar asked Mar 10 '26 15:03

Sauron


2 Answers

The way you plan to do this, is very annoying for a user. You're guessing what a user wants to enter, and act upon your guess, but you can be so wrong.

It also has holes, for example, a user can enter "10" and then delete the "1". Or he could paste in a "0" -- you do allow paste, don't you?

So my solution would be: let him enter any digit he likes, any way he likes, and validate the input only after he finished, for example, when the input loses focus.

like image 98
bart Avatar answered Mar 12 '26 05:03

bart


Why not using a NumericUpDown and make the following settings:

upDown.Minimum = 1;
upDown.Maximum = Decimal.MaxValue;
like image 37
Oliver Avatar answered Mar 12 '26 05:03

Oliver



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!