HTML Input Types

The <input> tag is the most flexible form element. The type attribute decides what kind of field it is. HTML5 added many new types, which give better keyboards on phones and built-in checks.


Common input types

TypeUse
textSingle line text (default)
passwordText hidden with dots
emailEmail address, with a built-in format check
numberNumbers only, with up and down arrows
telPhone number
urlWebsite address
searchSearch box
date, timeDate picker and time picker
colorColour picker
rangeSlider
fileChoose a file to upload
checkboxChoose none, one or many options
radioChoose only one option from a group
hiddenNot shown, but sent with the form
submit, reset, buttonButtons
<input type="text" name="name" placeholder="Your name">
<input type="password" name="pass" placeholder="Password">
<input type="email" name="email" placeholder="you@example.com">
<input type="number" name="age" min="1" max="100">
<input type="date" name="dob">
<input type="color" name="colour">
<input type="range" name="level" min="0" max="10">
<input type="file" name="photo">
Output in the browser


Useful input attributes

AttributeMeaning
nameName of the field, needed to send its value
valueStarting value
placeholderA hint shown inside an empty field
requiredThe field cannot be left empty
min, maxSmallest and biggest number or date
maxlengthMaximum number of characters
patternA regular expression the value must match
readonlyThe value can be seen but not changed
disabledThe field is switched off and its value is not sent
autofocusThe cursor goes to this field when the page loads

Built-in validation

With required, type="email" and pattern, the browser checks the form before sending it, and shows a message when something is wrong.

<form>
  <input type="email" name="email" required>
  <input type="text" name="pin" pattern="[0-9]{6}" maxlength="6" placeholder="6 digit PIN" required>
  <button type="submit">Send</button>
</form>
Note: Browser validation is only for the comfort of the visitor. A user can skip it, so always check the data again on the server.

Summary

  • The type decides how an input works.
  • Use the right type, like email or number, for better keyboards and checks.
  • Use required, pattern, min and max for basic validation.
  • Learn the basics of forms in HTML Forms.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.