Numerictextbox : How to restrict the user from entering negative value.

1 Answer 2355 Views
NumericTextBox
Karthikeyan
Top achievements
Rank 1
Iron
Karthikeyan asked on 13 Aug 2021, 11:43 AM

Hi Team,

Control : numeric text box

 

How to restrict the user from entering negative value. 

If we set min value as min={0} it is allowing user to enter negative value and then on focus out it is reverting the value to min that is "0". Our expectation is  hard restriction by not allowing to enter the negative value.

 

I'm, surprised we don't have keyup and keydown events for numeric text box

1 Answer, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 13 Aug 2021, 11:59 AM

Hello, Karthikeyan,

This is expected as we allow the user to enter any values when the component has focus. This is needed because in some cases to write a valid value, the user has to first type an invalid one. For example, to enter valid min value 10, the user first has to enter the invalid value 1:

https://github.com/telerik/kendo-react/issues/102

As for the key up/down events we are planning to expose the onKeyDown event. Currently, it can be done as shown here:

https://www.telerik.com/forums/numerictextbox-prevent-default-key-up-down

Regards,
Stefan
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

David
Top achievements
Rank 1
Iron
Iron
Veteran
commented on 06 Apr 2022, 05:13 AM

Is it expected that when you set a min of zero and type in a negative value, that on focus out the value left in the text box is -0 rather than just 0?

This behavior can be observed by adding a min of 0 to the NumericTextBox on the KendoReact NumericTextBox Overview page: https://stackblitz.com/edit/react-hwuiud?file=app/main.tsx

Kind regards,

David

Konstantin Dikov
Telerik team
commented on 07 Apr 2022, 09:43 PM

The behavior is rather expected, because in JavaScript "-0 == 0" will return true. This means that the with min set to "0", entering -0 will still be valid.

For preventing the user of entering the "-" symbol at all, you can get reference to the input element and handle the "onKeyDown" event and prevent the event if the pressed key is "-".

Hope this helps.

Tags
NumericTextBox
Asked by
Karthikeyan
Top achievements
Rank 1
Iron
Answers by
Stefan
Telerik team
Share this question
or