如何使type =“text”不要键入减号( - )?

时间:2017-02-22 09:29:30

标签: javascript jquery html

我想使用input type = text,但只使用keyin编号,如何不允许用户keyin减号( - )?

<input required type="text" pattern="[0-9]*" name="discount" placeholder='not allow negative'  >

2 个答案:

答案 0 :(得分:3)

只需输入类型编号并添加min属性,因此您的代码必须像这样

<input type="number" min="0"/>

答案 1 :(得分:1)

改变
<input required type="text" pattern="[0-9]*" name="discount" placeholder='not allow negative' >

<input required type="text" onkeypress="return (event.charCode == 8 || event.charCode == 0) ? null : event.charCode >= 48 && event.charCode <= 57" name="discount" placeholder='not allow negative' >