Number Fields
At the cinema, you buy tickets: one, two, maybe four. Never two and a half, and never minus one. A box that asks "how many?" should know that too.
type="number"
A box for a number is type="number":
<label for="tickets">Tickets</label><br />
<input type="number" id="tickets" />
Two small arrows show in the box. Click them, or press the up and down arrow keys, and the number goes up or down by one. Try to type a letter: most browsers don't let you. On a phone, the keyboard shows digits.
min and max
min is the smallest number the box takes, and max is the biggest:
<form>
<label for="tickets">Tickets</label><br />
<input type="number" id="tickets" name="tickets" min="1" max="4" />
<button>Buy</button>
</form>
Click the arrows: they stop at 1 and at 4. Now type 9 and click Buy. Nothing is sent. The browser shows a small message that says the number must be 4 or less. Type 3, and it is sent:
The form was sent: tickets=3
step
A number box takes whole numbers: 1, 2, 3. The step attribute changes how big each jump is. A runner who saves how far they ran wants halves too:
<form>
<label for="run">How far did you run? (km)</label><br />
<input type="number" id="run" name="run" min="0" step="0.5" />
<button>Save</button>
</form>
Click the up arrow a few times: 0, 0.5, 1, 1.5. Type 1.2 and save: it isn't sent, and the browser gives the two closest numbers it takes, 1 and 1.5.
Without step, it's like step="1": only whole numbers.
Break it on purpose
Take the max away from the tickets:
<form>
<label for="tickets">Tickets</label><br />
<input type="number" id="tickets" name="tickets" min="1" />
<button>Buy</button>
</form>
Type 400 and click Buy:
The form was sent: tickets=400
The browser had no limit to check, so the form sent 400 tickets. Every limit you leave out is a number nobody checks.
Mission: How many bags?
What you will see: a box like "Number of bags", with small arrows. Type 2 and send: the console's line ends with bags=2. Type 4 and send: nothing is sent.
Your booking form asks for the name, the email and the phone. Each passenger may bring up to three bags, or none at all.
- Add a number box with the id
bags, sent asbags. - It has a label that puts the cursor in it.
- It only takes whole bags, from 0 to 3.
- The browser doesn't send 4, -1 or 1.5.