How to allow only numbers in textbox in reactjs?

后端 未结 7 544
感情败类
感情败类 2020-12-03 05:03

How to allow only numbers in textbox in reactjs using regular expression only?

7条回答
  •  醉梦人生
    2020-12-03 05:51

    I know you've asked for regular expression.
    But here is a solution without regex if anyone wants.

    
      const handlePincode = (e) => {
        !isNaN(e.nativeEvent?.data) && setPincode(e.currentTarget.value);
      };
    
      return (
        
    );

    The event.nativeEvent.data gives me the currently typed value, I can just validate that and set the event.currentTarget.value or just return it.

提交回复
热议问题