Skip to main content

HTML Forms

HTML Input Attributes

Written by Updated HTML Input Attributes exercises

An input box on its own just takes text. Its attributes turn it into something useful: a required email field, a number between 1 and 10, a password with at least 6 characters. Each one is a single word or value, and most of them also give you free validation.

Below are the attributes you will actually use, each with an example you can run. The input types themselves (email, number, date and so on) are covered in HTML Input Types.

The value Attribute

The text that is already in the box when the page loads. The user can change it, and whatever is in the box when the form is sent is what the server receives.

Input Value Attribute

html

<input type="text" value="Swapnil Raja">

The name Attribute

The name the server receives the value under. Without name, the field is not sent at all, which is one of the most common reasons a form "loses" data.

Input Name Attribute

html

<input type="email" name="user_email">

The placeholder Attribute

Grey hint text that disappears when the user starts typing. Use it for an example of the format, like name@example.com, not as a replacement for a label: once the user types, the hint is gone.

HTML Placeholder Attribute

html

<input type="text" placeholder="Enter your name">

The required Attribute

The form will not submit while this field is empty. The browser shows its own message and moves the cursor to the field.

Input Required Attribute

html

<input type="email" required>

The readonly Attribute

The user can see and copy the value, but not change it. It is still sent with the form.

Input Readonly Attribute

html

<input type="text" value="Read only text" readonly>

The disabled Attribute

The field is greyed out, cannot be clicked, and is not sent with the form. That is the key difference from readonly.

Input Disabled Attribute

html

<input type="text" value="Disabled" disabled>

The maxlength Attribute

The user simply cannot type more than this many characters. It counts characters, not words.

HTML Maxlength Attribute

html

<input type="text" maxlength="10">

The min and max Attributes

The lowest and highest allowed value for numbers and dates. For a date, write the value as YYYY-MM-DD.

Input Min and Max Attributes

html

<input type="number" min="1" max="10">
<input type="date" min="2024-01-01" max="2025-12-31">

The step Attribute

How much the value goes up or down with the arrow buttons, and which values count as valid. With step="5", 7 is invalid; 5, 10 and 15 are fine. Use step="0.01" for prices, because the default step of 1 rejects decimals.

Input step Attribute

html

<input type="number" step="5">

The autofocus Attribute

Puts the cursor in this field as soon as the page loads. Use it only when the field is clearly the reason for the page, like a search page. On a long page it makes screen reader users start in the middle.

Input Autofocus Attribute

html

<input type="text" autofocus>

The autocomplete Attribute

Turns the browser's saved suggestions on or off. More useful than on are specific values like autocomplete="email", "name" or "current-password", which help browsers and password managers fill the right data into the right field.

Input Autocomplete Attribute

html

<input type="email" autocomplete="on">
<input type="email" autocomplete="off">

The pattern Attribute

A pattern the value must match, written as a regular expression. [A-Za-z]{3} means exactly three letters. Always add a title explaining the format, because the browser shows it in the error message.

Input Pattern Attribute

html

<input type="text" pattern="[A-Za-z]{3}" title="Enter only 3 letters">

The checked Attribute

Ticks a checkbox or selects a radio button when the page loads.

Input Checked Attribute

html

<input type="checkbox" checked> I agree

The multiple Attribute

Lets the user pick several files at once, or type several email addresses separated by commas.

Input Multiple Attribute

html

<input type="file" multiple>
<input type="email" multiple>

The size Attribute

The visible width of the box, counted in characters. It does not limit how much can be typed; that is what maxlength does. Most sites set the width with CSS instead.

Input Size Attribute

html

<input type="text" size="30">

The list Attribute and <datalist>

Connects the input to a <datalist> of suggestions. Users can pick one or still type their own value. The list value must match the id of the datalist.

Input List and Datalist Attribute

html

<input list="browsers" name="browser">
<datalist id="browsers">
  <option value="Chrome">
  <option value="Firefox">
  <option value="Edge">
  <option value="Safari">
</datalist>

The minlength and maxlength Attributes

Together they set a range: here the password must be 6 to 12 characters. Unlike maxlength, minlength does not stop typing; it just makes the form invalid until the value is long enough.

Input Minlength and Maxlegth Attributes

html

<input type="password" minlength="6" maxlength="12">

Example: Various Input Attributes

Different Input Attributes Example

html

<form>
  <label>Username:</label>
  <input type="text" name="username" placeholder="Enter your name" required minlength="3" maxlength="15"><br><br>

  <label>Email:</label>
  <input type="email" name="email" autocomplete="on" required><br><br>

  <label>Password:</label>
  <input type="password" name="password" minlength="6" maxlength="12" required><br><br>

  <label>Age:</label>
  <input type="number" min="18" max="100" step="1"><br><br>

  <label>Gender:</label>
  <input type="radio" name="gender" value="male" checked> Male
  <input type="radio" name="gender" value="female"> Female<br><br>

  <label>Favorite Browser:</label>
  <input list="browsers">
  <datalist id="browsers">
    <option value="Chrome">
    <option value="Firefox">
    <option value="Edge">
    <option value="Safari">
  </datalist><br><br>

  <label>Upload Files:</label>
  <input type="file" multiple><br><br>

  <input type="submit" value="Submit">
</form>

Try submitting the form empty, then with a two-letter username, then with an age of 15. Each attribute blocks the form in its own way, without a single line of JavaScript.

Browser Support

Chrome
Edge
Firefox
Safari
Opera
IE9+
YesYesYesYesYesYes

Good to know

  • Browser checks are a convenience for the user, not security. Anyone can remove them in DevTools, so the server must check the data again.
  • Every input needs a proper <label>. A placeholder is not a label.
  • readonly and disabled look similar, but only readonly values are sent with the form.

HTML Input Attributes exercises

A hint in the field

Add a text field named name that shows the hint Your name while it is empty.

Result

Check your answer to see it here.