input type vs attribute inputmode
(I know W3Schools is not the best place to learn things & sometimes not very accurate, but it has some explanation with screenshots attached)
type
determines functionality of input, and tells the browser how to should render it. Like <input type="checkbox">
will make it look like a box, with ticked & unticked options available on click, while <input type="file">
shows a button which onclick allows you to select a file from your device.
inputmode
softly changes the virtual keyboard on mobile/tablet (if available), but doesn't restrict user from typing or pasting anything.
Let's talk about number as example:
<input type="number">
allows only numbers to be entered, and you can even set min="1" max="5"
attributes for further restriction, and mobiles will show numeric keyboard.
<input inputmode="numeric">
shows numeric keyboard on mobile, but you're actually allowed to type anything in this field.
How useful & which one to pick?
Let's say you want an input field for credit card, you want user to type only numbers, but visually through JS, while user is typing, you will change value to have spaces, to make it like "#### #### #### ####". So what to choose? You set it as <input type="text" inputmode="numeric">
, because:
type="text"
will allow JS to set spaces in the value
inputmode="numeric"
changes mobile keyboard to numeric
type='number'
is a bad idea as phone numbers can consist of other characters too, and a number (100034) is an entirely different thing from a phone number (1-800-U-LUV-PIZZA). The keyboards offered for numbers vs phone numbers, by iOS / Android, are different too. – Embassy