ReactJs prevent e and dot in an input type number

前端 未结 6 1545
轮回少年
轮回少年 2020-12-18 01:52

I would like to prevent e and . to be type in an . Without jQuery or using step attribute.

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-18 02:04

    The 'e' is the only letter that's accepted in a number field because it allows for exponents. You could use input type="text" but it doesn't give you the browser's native up and down arrows that type="number" does. And the pattern attribute validates on submission but doesn't stop someone from typing the 'e' in the first place. In REACT you can use this to completely block the 'e' key from being typed in a number input:

     evt.key === 'e' && evt.preventDefault() } />
    

提交回复
热议问题