How to allow only numbers in textbox in reactjs?

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

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

7条回答
  •  鱼传尺愫
    2020-12-03 05:32

    A simple solution, using hooks:

    const [age, setAge] = useState();
    
    const handleChange = (e) => {
      const value = e.target.value.replace(/\D/g, "");
      setAge(value);
    };
    
    return (
      
    );

    Example: https://codesandbox.io/s/adoring-minsky-9sqvv

提交回复
热议问题