HTML Forms

HTML Input Attributes

HTML <input> elements support a wide range of attributes that define their behavior, appearance, and interaction with users. These attributes help control data entry, validation, accessibility, and styling within web forms.

The value Attribute

Specifies the default value for an input field.

Input Value Attribute

html

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

The name Attribute

Defines the name of the input control, used when submitting form data.

Input Name Attribute

html

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

The placeholder Attribute

Displays a short hint inside the input box before the user enters a value.

HTML Placeholder Attribute

html

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

The required Attribute

Marks a field as mandatory. The form cannot be submitted without filling it.

Input Required Attribute

html

<input type="email" required>

The readonly Attribute

Makes an input field uneditable, but its value is still submitted with the form.

Input Readonly Attribute

html

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

The disabled Attribute

Disables an input field - it cannot be interacted with or submitted.

Input Disabled Attribute

html

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

The maxlength Attribute

Limits the maximum number of characters allowed in a text field.

HTML Maxlength Attribute

html

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

The min and max Attributes

Define minimum and maximum values for numeric or date inputs.

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

Specifies the legal number intervals for numeric inputs.

Input step Attribute

html

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

The autofocus Attribute

Automatically focuses on the input field when the page loads.

Input Autofocus Attribute

html

<input type="text" autofocus>

The autocomplete Attribute

Tells the browser whether to enable autocomplete for a field.

Input Autocomplete Attribute

html

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

The pattern Attribute

Specifies a regular expression that the input value must match.

Input Pattern Attribute

html

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

The checked Attribute

Pre-selects a checkbox or radio button.

Input Checked Attribute

html

<input type="checkbox" checked> I agree

The multiple Attribute

Allows multiple values to be selected for file or email inputs.

Input Multiple Attribute

html

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

The size Attribute

Defines the visible width (in characters) of the input field.

Input Size Attribute

html

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

The list Attribute and <datalist>

Connects the input field to a list of predefined options.

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

Ensure a specific range of input length.

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>

Browser Support

Chrome
Edge
Firefox
Safari
Opera
IE9+
YesYesYesYesYesYes

Conclusion

HTML input attributes enhance the functionality, usability, and validation of web forms. By combining these attributes effectively, developers can create user-friendly, secure, and accessible input fields.