HTML Input Attributes: Complete Guide with Examples

Learn important HTML input attributes with examples, including required, placeholder, value, pattern, maxlength, autocomplete, min, max, and more.


Table of Contents

HTML Input Attributes: A Complete Guide with Examples

HTML input attributes are used to control the behavior, appearance, validation, and functionality of input fields in web forms. They help developers collect information such as names, email addresses, passwords, phone numbers, dates, and files from users.

The <input> element is one of the most important elements in HTML forms. By using different input attributes, developers can make forms more user-friendly, accessible, and reliable without writing unnecessary JavaScript.

What Are HTML Input Attributes?

HTML input attributes are special properties added to an <input> element to define how it works. They can specify the input’s type, initial value, placeholder text, required status, maximum length, and other behaviors.

For example:

<form>
<label for="username">Username:</label>
<input
type="text"
id="username"
name="username"
placeholder="Enter your username"
required
>
<button type="submit">Submit</button>
</form>

In this example:

  • type="text" creates a text input field.
  • id="username" uniquely identifies the element within the page.
  • name="username" identifies the submitted form data.
  • placeholder displays a hint inside the empty field.
  • required prevents normal form submission when the field is empty.

Important HTML Input Attributes

The following table explains some of the most useful input attributes.

AttributePurpose
typeSpecifies the kind of input field.
nameDefines the name of the submitted form data.
valueSets the initial or current value.
placeholderDisplays a short hint inside the field.
requiredMakes the field mandatory.
readonlyPrevents the user from editing the value.
disabledDisables the input field.
maxlengthLimits the maximum number of characters.
minlengthSets the minimum number of characters.
minDefines the minimum permitted numeric or date value.
maxDefines the maximum permitted numeric or date value.
stepSpecifies permitted increments for numeric or date-related inputs.
patternDefines a regular expression for text validation.
multipleAllows multiple values where supported, such as multiple files or email addresses.
checkedPreselects a checkbox or radio button.
acceptSuggests which file types can be selected.
autocompleteControls browser autofill hints.
autofocusRequests automatic focus when the page loads.
listConnects an input to a <datalist> of suggestions.
formAssociates an input with a form by its ID.
captureCan request camera or microphone capture for supported file inputs and devices.
dirnameSubmits the text direction of an input alongside its value.
formactionOverrides the form’s submission URL for a submit button.
formenctypeOverrides the form’s encoding type for a submit button.
formmethodOverrides the form submission method for a submit button.
formnovalidateSkips built-in validation for a submit button.
formtargetSpecifies where the response to a form submission should be displayed.

Some attributes apply only to particular input types. For example, checked applies to checkboxes and radio buttons, while accept applies to file inputs.

1. The type Attribute

The type attribute determines what kind of input control the browser displays.

Example:

<input type="text" name="username">
<input type="email" name="email">
<input type="password" name="password">
<input type="number" name="age">
<input type="date" name="birthdate">
<input type="checkbox" name="subscribe">
<input type="radio" name="gender" value="other">
<input type="file" name="document">

Common input types include:

  • text — ordinary text.
  • email — an email address.
  • password — a password field.
  • number — a numeric value.
  • tel — a telephone number.
  • url — a web address.
  • date — a date picker.
  • time — a time picker.
  • checkbox — a selectable checkbox.
  • radio — one choice from a group.
  • file — a file selection control.
  • submit — a form submission button.

Choosing an appropriate input type improves usability and may provide built-in browser validation.

2. The name Attribute

The name attribute identifies a field when its data is submitted with a form. Without a name, an ordinary input’s value is generally not included in the submitted form data.

Example:

<form action="/submit" method="post">
<label for="fullname">Full Name:</label>
<input type="text" id="fullname" name="fullname">
<button type="submit">Submit</button>
</form>

If the user enters “Rahul Kumar,” the form data includes the field name fullname and the entered value.

The name attribute is especially important when working with server-side languages such as PHP, Python, Java, or Node.js.

3. The value Attribute

The value attribute specifies an input’s initial value or, for certain input types, its configured value.

Example:

<input type="text" name="country" value="India">

The input initially displays “India,” which the user can edit.

For checkboxes and radio buttons, value specifies the value submitted when the control is selected.

<input type="radio" name="plan" value="basic">
<input type="radio" name="plan" value="premium">

The value attribute is not the same as placeholder. A value is actual field content, while a placeholder is a hint.

4. The placeholder Attribute

The placeholder attribute displays a short hint inside an empty input field.

Example:

<input
type="email"
name="email"
placeholder="Enter your email address"
>

The hint disappears as the user enters a value.

Best practice: Use a visible <label> as well as a placeholder. Placeholder text is not a replacement for a proper label because it disappears during entry and may be difficult to read.

5. The required Attribute

The required attribute makes an input mandatory before a form can normally be submitted.

Example:

<form>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<button type="submit">Submit</button>
</form>

If the field is empty, the browser displays a validation message and prevents normal submission.

The attribute works with many input types, including text, email, password, number, date, radio, checkbox, and file inputs.

However, HTML validation can be bypassed, so important information must also be validated on the server.

6. The readonly Attribute

The readonly attribute prevents the user from directly editing an input’s value.

Example:

<input type="text" name="username" value="Rahul" readonly>

The value can generally still be selected and copied. Unlike disabled controls, readonly controls are normally included in submitted form data.

The attribute applies to supported text-like inputs and is not available for every input type.

7. The disabled Attribute

The disabled attribute makes an input unavailable for normal interaction.

Example:

<input type="text" name="username" value="Rahul" disabled>

A disabled input typically appears faded and cannot be edited. Its value is also excluded from normal form submission.

Difference between readonly and disabled

Featurereadonlydisabled
User can edit the valueNoNo
Usually receives focusYesNo
Included in normal form submissionYesNo
Works with every input typeNoSupported by many input types

Choose readonly when a value should remain visible and be submitted. Choose disabled when the control should be unavailable.

8. The maxlength and minlength Attributes

These attributes control the length of text entered into supported text-like input fields.

  • maxlength specifies the maximum number of characters.
  • minlength specifies the minimum number of characters for constraint validation.

Example:

<input
type="text"
name="username"
minlength="3"
maxlength="20"
required
>

This field requires a username between 3 and 20 characters long.

These attributes are useful for usernames, short descriptions, and other text fields. They should not be treated as a substitute for server-side validation.

9. The min, max, and step Attributes

These attributes help control numeric values and supported date or time inputs.

Example:

<label for="age">Age:</label>
<input
type="number"
id="age"
name="age"
min="18"
max="60"
step="1"
>

In this example:

  • min="18" sets the minimum age.
  • max="60" sets the maximum age.
  • step="1" permits increments of one.

A decimal example:

<input type="number" name="price" min="0" max="1000" step="0.5">

The permitted values can include 0, 0.5, 1, 1.5, and so on, subject to the minimum, maximum, and step rules.

The exact behavior of step depends on the input type and its default step base.

10. The pattern Attribute

The pattern attribute defines a regular expression that the input’s value must match for constraint validation.

Example:

<label for="code">Product Code:</label>
<input
type="text"
id="code"
name="code"
pattern="[A-Z]{3}[0-9]{3}"
title="Enter three uppercase letters followed by three digits"
required
>

A valid example is ABC123.

The pattern requires three uppercase letters followed by three digits. The title attribute provides additional guidance about the expected format.

The pattern attribute is generally used with text-like inputs, including text, search, tel, url, and email.

Important: A pattern validates formatting, not whether the supplied information is genuine or belongs to the user.

11. The checked Attribute

The checked attribute preselects a checkbox or radio button when the page initially loads.

Example:

<label>
<input type="checkbox" name="newsletter" checked>
Subscribe to the newsletter
</label>

For radio buttons, use the same name for options that belong to one group.

<label>
<input type="radio" name="payment" value="cash" checked>
Cash
</label>
<label>
<input type="radio" name="payment" value="card">
Card
</label>

Here, cash is initially selected. The user can choose the card option instead.

12. The multiple Attribute

The multiple attribute allows more than one value where the input type supports it.

Example with file uploads:

<input type="file" name="documents" multiple>

The user can select multiple files.

It can also be used with email inputs:

<input type="email" name="emails" multiple>

This allows a comma-separated list of email addresses. The browser can validate their general email format, but it does not confirm that the addresses exist.

13. The accept Attribute

The accept attribute suggests the types of files a user should select in a file input.

Example:

<input
type="file"
name="photo"
accept="image/*"
>

To suggest particular image formats:

<input type="file" name="photo" accept=".jpg,.jpeg,.png,.webp">

Common examples include:

  • image/* — image files.
  • audio/* — audio files.
  • video/* — video files.
  • .pdf — PDF files.
  • .doc,.docx — Word document extensions.

The accept attribute is a selection hint, not a security feature. Always validate uploaded files on the server.

14. The autocomplete Attribute

The autocomplete attribute helps the browser determine whether it should suggest previously stored information.

Example:

<input type="text" name="name" autocomplete="name">
<input type="email" name="email" autocomplete="email">
<input type="tel" name="phone" autocomplete="tel">

Common values include:

  • name
  • given-name
  • family-name
  • email
  • tel
  • username
  • current-password
  • new-password
  • street-address
  • postal-code
  • off
  • on

For passwords, use an appropriate autocomplete value, such as current-password for an existing password or new-password for a new one.

Autocomplete improves convenience, but browsers may handle autofill differently depending on user settings and context.

15. The autofocus Attribute

The autofocus attribute requests that the browser automatically focus an input when the page or relevant dialog loads.

Example:

<input type="search" name="query" autofocus>

This can be useful on a dedicated search page.

However, avoid using autofocus excessively. Automatically moving keyboard focus can disorient some users, particularly those using assistive technology.

16. The list Attribute

The list attribute connects an input to a <datalist> element, which provides suggested values.

Example:

<label for="browser">Choose a browser:</label>
<input type="text" id="browser" name="browser" list="browsers">
<datalist id="browsers">
<option value="Chrome">
<option value="Firefox">
<option value="Safari">
<option value="Edge">
</datalist>

The browser can display suggestions as the user types. Unlike a <select> element, a datalist usually allows the user to enter a value that is not included in the suggestions.

17. The form Attribute

The form attribute associates an input with a form using the form’s id. This is useful when the input is located outside the form element.

Example:

<form id="profileForm" action="/save" method="post">
<button type="submit">Save Profile</button>
</form>
<label for="city">City:</label>
<input type="text" id="city" name="city" form="profileForm">

Although the city input appears outside the form in the HTML structure, it belongs to profileForm and can be submitted with it.

18. The capture Attribute

The capture attribute can request capture using a device’s camera or microphone for supported file inputs.

Example:

<input type="file" name="photo" accept="image/*" capture="environment">

The value environment requests the outward-facing camera where supported. The value user requests the user-facing camera.

Device support and browser behavior vary. The attribute does not guarantee that a camera will open or that capture will be available.

19. The dirname Attribute

The dirname attribute submits the text direction of a supported input alongside its value.

Example:

<input
type="text"
name="message"
dirname="message.dir"
>

When submitted, the browser can include both the message and its text direction, such as left-to-right or right-to-left.

This can be useful for multilingual websites that accept text in languages written in different directions.

Some attributes are used on submit buttons rather than ordinary text inputs. They override the corresponding form settings for that particular submission.

AttributePurpose
formactionSpecifies a different submission URL.
formenctypeSpecifies a different form-data encoding.
formmethodSpecifies get or post.
formnovalidateRequests submission without built-in constraint validation.
formtargetSpecifies where to display the response.

Example:

<form action="/save" method="post">
<label for="title">Title:</label>
<input type="text" id="title" name="title" required>
<button type="submit">Save</button>
<button type="submit" formnovalidate>
Save Without Validation
</button>
</form>

The second button requests that built-in browser validation be skipped for that submission. Server-side validation should still be performed.

Complete Example: HTML Form Using Input Attributes

The following example combines several important input attributes in a simple registration form.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Registration Form</title>
</head>
<body>
<h2>Create an Account</h2>
<form action="/register" method="post">
<label for="username">Username:</label>
<input
type="text"
id="username"
name="username"
placeholder="Enter username"
minlength="3"
maxlength="20"
required
autocomplete="username"
>
<br><br>
<label for="email">Email:</label>
<input
type="email"
id="email"
name="email"
placeholder="Enter email address"
autocomplete="email"
required
>
<br><br>
<label for="age">Age:</label>
<input
type="number"
id="age"
name="age"
min="18"
max="100"
required
>
<br><br>
<label for="password">Password:</label>
<input
type="password"
id="password"
name="password"
minlength="8"
autocomplete="new-password"
required
>
<br><br>
<label for="photo">Profile Photo:</label>
<input
type="file"
id="photo"
name="photo"
accept="image/*"
>
<br><br>
<label>
<input type="checkbox" name="terms" value="accepted" required>
I agree to the terms and conditions
</label>
<br><br>
<button type="submit">Register</button>
</form>
</body>
</html>

This form demonstrates how input attributes improve data collection and basic browser validation. A real registration system must also validate the submitted information on the server, securely handle passwords, and verify uploaded files.

Benefits of HTML Input Attributes

HTML input attributes offer several important advantages:

  1. Better user experience: Placeholders, autocomplete, and appropriate input types make forms easier to use.
  2. Basic form validation: Attributes such as required, min, max, and pattern help detect invalid entries.
  3. Less JavaScript: Many common form behaviors can be implemented directly in HTML.
  4. Improved mobile usability: Email, telephone, numeric, and date inputs can provide more suitable controls on supported devices.
  5. Consistent data collection: name, value, and form-related attributes help structure submitted data.
  6. Improved accessibility when used correctly: Appropriate input types and clearly associated labels help users understand and operate forms.

Best Practices for Using HTML Input Attributes

  • Always use a meaningful name for fields whose values must be submitted.
  • Associate each input with a descriptive <label>.
  • Choose the correct type for the information being collected.
  • Use required, min, max, and other validation attributes where appropriate.
  • Do not rely on placeholder as the only label.
  • Validate important data on the server, even when browser validation is enabled.
  • Use accept only as a file-selection hint, not as proof that an uploaded file is safe.
  • Avoid collecting unnecessary personal information.
  • Use appropriate autocomplete values to improve form usability.
  • Use readonly and disabled according to their different submission and interaction behaviors.

Conclusion

HTML input attributes are essential tools for creating effective web forms. They control input types, initial values, validation rules, text limits, file selection, browser autofill, and form submission behavior.

Learning attributes such as type, name, value, placeholder, required, min, max, pattern, autocomplete, and disabled provides a strong foundation for HTML form development. When combined with accessible labels, server-side validation, and secure data handling, these attributes help developers build reliable and user-friendly websites.

FAQs: HTML Input Attributes

1. What are HTML input attributes?

HTML input attributes are properties added to the <input> element to control its behavior, appearance, validation, and functionality in web forms.

2. What are the most commonly used HTML input attributes?

Commonly used attributes include type, name, value, placeholder, required, readonly, disabled, maxlength, min, max, pattern, and autocomplete.

3. What is the purpose of the required attribute?

The required attribute makes an input field mandatory. The browser normally prevents form submission if the required field is empty.

4. What is the difference between readonly and disabled?

The readonly attribute prevents editing but generally allows the field’s value to be submitted. The disabled attribute prevents normal interaction and excludes the field’s value from normal form submission.

5. What is the purpose of the placeholder attribute?

The placeholder attribute displays a short hint inside an empty input field, helping users understand what information to enter.

6. What do the min and max attributes do?

The min attribute specifies the minimum permitted value, while max specifies the maximum permitted value for supported input types, such as number and date.

7. What is the pattern attribute in HTML?

The pattern attribute defines a regular expression that a supported text input must match to pass browser constraint validation.

8. What is the difference between value and placeholder?

The value attribute provides actual input content, while placeholder displays a temporary hint that disappears as the user enters information.

9. What is the purpose of the maxlength attribute?

The maxlength attribute sets the maximum number of characters allowed in supported text input fields.

10. What does the autocomplete attribute do?

The autocomplete attribute helps the browser identify information that can be automatically filled, such as names, email addresses, usernames, and passwords.

11. What is the multiple attribute used for?

The multiple attribute allows users to select multiple files in a file input or enter multiple email addresses in a supported email input.

12. What is the purpose of the accept attribute?

The accept attribute suggests which file types users should select when uploading files. It does not replace server-side file validation.

13. Can HTML input attributes validate form data?

Yes. Attributes such as required, pattern, min, max, minlength, and maxlength provide built-in browser validation. Server-side validation is still necessary for security and data integrity.

14. What is the checked attribute?

The checked attribute initially selects a checkbox or radio button when the page loads.

15. Why is the name attribute important?

The name attribute identifies a form field in submitted form data. An ordinary input without a name is generally not included in the form submission.


Discover more from DL Mendali

Subscribe now to keep reading and get access to the full archive.

Continue reading