ReactJs prevent e and dot in an input type number

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

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

6条回答
  •  一个人的身影
    2020-12-18 02:13

    The best way to handle this is to use the onKeyDown prop (onkeydown in plain html) to check the keyCode when the user uses the keyboard to input a character. If the keyCode for the event is 69 (for 'e') or 190 (for '.'), then you can preventDefault(), preventing the input from being displayed.

     ( e.keyCode === 69 || e.keyCode === 190 ) && e.preventDefault() }
    />
    

提交回复
热议问题