2

My code is very simple:

<input id="myID" type="number" pattern="^\d*$">

Based on a famous online regex test website, it should only let me write numbers, like 1 and 123, but not strings like 1a and 2e.

Instead it lets me write the letter "e". I guess it's for scientific notation? I don't need it. I just need integer numbers. How can I do this?

I've also tried ^[0-9]*$ but it still lets me write the letter "e". This happens on Chrome.

JSFiddle

6
  • Use type="text" Commented Nov 28, 2017 at 8:16
  • Thank you @WiktorStribiżew, I tried this but it doesn't work; it lets me write anything now. The pattern doesn't get applied. Commented Nov 28, 2017 at 8:19
  • Yes, you may type, but you may not submit, that is what pattern attribute is for. It is used for final, on-submit, validation Commented Nov 28, 2017 at 8:20
  • I use onkeypress="return event.charCode >= 48 && event.charCode <= 57" for that. Commented Nov 28, 2017 at 8:20
  • Well, just found another, maybe also relevant, thread. Commented Nov 28, 2017 at 8:22

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.