1

I want to use input type=text, but only keyin number, how to no allow user keyin Minus sign (-)?

<input required type="text" pattern="[0-9]*" name="discount" placeholder='not allow negative'  >
Osama AbuSitta
  • 3,918
  • 4
  • 35
  • 51
Doran Wu
  • 97
  • 11

2 Answers2

3

Just make the type number and add min attribute, so your code must be like this

<input type="number" min="0"/>
Amro Mustafa
  • 589
  • 4
  • 15
1

change from

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

to

<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' >
dhruv jadia
  • 1,684
  • 2
  • 15
  • 28