【问题标题】:How do I restrict the input to only number at the input box?如何在输入框中将输入限制为仅数字?
【发布时间】:2020-12-01 11:39:21
【问题描述】:
   <Form.Group>
              <Form.Control. onChange={e => { console.log('e------',e.target.value)}}
             type='number' />
   </Form.Group>

我设置的类型可以是数字,但是当我点击“enter”(返回按钮)和“-”时,

我仍然可以在我的输入框中看到“e”和“-”,但我只想让用户在输入框中看到数字。

【问题讨论】:

标签: html reactjs react-bootstrap


【解决方案1】:

你可以用这个

<Form.Control type="number" onKeyDown={ (evt) => evt.key === 'e' && evt.preventDefault() } />

根据stackoverflow上类似问题的现有答案

【讨论】:

    【解决方案2】:

    你可以试试正则表达式。

      <Form.Group>
           <Form.Control. 
              onChange={e =>  if(e.target.value.match("/^\d*(\.\d+)?$/")){
                  console.log('e------',e.target.value)
              }}
           />
       </Form.Group>
     
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-14
      • 1970-01-01
      • 1970-01-01
      • 2013-08-11
      • 2017-05-07
      • 1970-01-01
      • 2013-08-21
      相关资源
      最近更新 更多