HTML Input Types: text, number, date, radio and select
The type attribute turns one <input> element into many different controls. Choosing the right type gives users the right keyboard on phones, built-in validation, and accessible widgets for free.
Text-like types
textis the general case;maxlengthlimits its length.email,urlandtellook like text fields but bring a suitable mobile keyboard, and the first two are validated.passwordhides what is typed.searchadds a clear button in some browsers.
Numbers and dates
type="number" accepts only numbers, with min, max and step limits, and offers up and down arrows. In JavaScript, valueAsNumber gives the value as a number instead of a string; the example adds 1 to it. type="date" opens a date picker and always uses the format YYYY-MM-DD in its value, whatever the display format in the visitor's locale. time, datetime-local and month work similarly.
Expected page text
Name Age Start date Track Python SQL Level Beginner Intermediate Message
Choices
- Radio buttons (
type="radio") let the user pick exactly one option from a group. All radios in a group share the samename. Wrap the group in a<fieldset>with a<legend>, so screen readers announce "Track, Python, radio button, 1 of 2". - Checkboxes (
type="checkbox") toggle independent yes/no options. <select>shows a drop-down list of<option>elements, useful when there are too many choices for radios. Addselectedto preselect an option, andmultipleto allow several.
Multi-line text
<textarea> accepts paragraphs of text. Unlike <input>, its initial content goes between the tags, and rows sets its visible height.
What the page text shows
The expected output above is the page's visible text. Values typed into fields are not part of a page's text content, so only the labels, the legend and the option texts of the <select> appear there. Chrome lists every option, not just the selected one. Run the example to see the console line it prints from the controls' current values.
Validation attributes
required, min, max, minlength, maxlength and pattern describe valid input. checkValidity() reports whether a control currently passes them; the example's number field does, so it logs true.
Common mistakes
- Using
type="text"for everything and losing mobile keyboards and validation. - Giving radio buttons in a group different
namevalues, so several can be selected. - Using
type="number"for things like ZIP codes or card numbers, which are identifiers, not quantities. Useinputmode="numeric"on a text input instead.
Summary
- Choose input types for meaning:
email,number,dateand more. - Radios share a
nameand sit in afieldsetwith alegend. <select>with<option>for longer lists;<textarea>for paragraphs.- Validation attributes describe the rules;
checkValidity()tests them.
Quiz: Input types and controls