0
Follow
0
View

How do I validate a number input within a React component

imperial_mark 注册会员
2023-01-25 21:53

This is because Number('bad input') returns NaN (Not a Number). NaN is a value which is not smaller or greater than 1. You should change your condition so that it handles those scenarios.

if (question?.type === 'number' && (isNaN(e.currentTarget.value) || Number(e.currentTarget.value) < 1)) {

Also something else, besides your question, changing the element value like you do in here e.currentTarget.value = 0; is bad practice since you're changing it imperatively. It's better to make sure you're changing the state so that the value variable becomes 0 (I'm not sure if that already happens in setResponseGeneric).

About the Author

Question Info

Publish Time
2023-01-25 21:53
Update Time
2023-01-25 21:53