Input
The value
Attribute
The value
attribute for an input field defines its default or initial value.
The value attribute specifies an initial value for an input field:
<h1>The input value attribute</h1>
<p>The value attribute specifies an initial value for an input field:</p>
<form>
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" value="John"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="Doe"><br><br>
<input type="submit" value="Submit">
</form>
The readonly
Attribute
The readonly
attribute makes an input field non-editable, allowing users to tab, highlight, and copy the text, but not modify it. The value of a read-only field is still submitted with the form.
Example
A non-editable
input field.
The readonly attribute specifies that an input field should be read-only (cannot be changed):
<h1>The input readonly attribute</h1>
<p>The readonly attribute specifies that an input field should be read-only (cannot be changed):</p>
<form>
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" value="John" readonly><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="Doe"><br><br>
<input type="submit" value="Submit">
</form>
The disabled
Attribute
The disabled
attribute makes an input field unusable and un-clickable. Its value is not submitted with the form.
Example
A non-interactive
input field.
The disabled attribute specifies that an input field should be disabled (unusable and un-clickable):
<h1>The input disabled attribute</h1>
<p>The disabled attribute specifies that an input field should be disabled (unusable and un-clickable):</p>
<form>
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" value="John" disabled><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="Doe"><br><br>
<input type="submit" value="Submit">
</form>
The size
Attribute
The size
attribute for an input field determines the visible width of the field in terms of the number of characters. By default, the size
value is set to 20.
size
attribute is applicable to the following input types: text, search, tel, url, email, and password.Example
Define the width
of an input field:
The size attribute specifies the width (in characters) of an input field:
<h1>The input size attribute</h1>
<p>The size attribute specifies the width (in characters) of an input field:</p>
<form>
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" size="50"><br>
<label for="pin">PIN:</label><br>
<input type="text" id="pin" name="pin" size="4"><br><br>
<input type="submit" value="Submit">
</form>
The maxlength
Attribute
The maxlength
attribute for an input field sets the maximum number of characters that can be entered.
maxlength
attribute limits the number of characters in an input field, but it doesn't provide user feedback, so JavaScript is needed to alert the user.Example
Define a maximum character limit for an input field:
The maxlength attribute specifies the maximum number of characters allowed in an input field:
<h1>The input maxlength attribute</h1>
<p>The maxlength attribute specifies the maximum number of characters allowed in an input field:</p>
<form>
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" size="50"><br>
<label for="pin">PIN:</label><br>
<input type="text" id="pin" name="pin" maxlength="4" size="4"><br><br>
<input type="submit" value="Submit">
</form>
The min
and max
Attributes
The min
and max
attributes define the minimum and maximum allowable values for an input field.
These attributes are compatible with input types such as number, range, date, datetime-local, month, time, and week.
Example
Define a maximum date, a minimum date, and a valid range of values:
The min and max attributes specify the minimum and maximum values for an input element.
<h1>The input min and max attributes</h1>
<p>The min and max attributes specify the minimum and maximum values for an input element.</p>
<form>
<label for="datemax">Enter a date before 1980-01-01:</label>
<input type="date" id="datemax" name="datemax" max="1979-12-31"><br><br>
<label for="datemin">Enter a date after 2000-01-01:</label>
<input type="date" id="datemin" name="datemin" min="2000-01-02"><br><br>
<label for="quantity">Quantity (between 1 and 5):</label>
<input type="number" id="quantity" name="quantity" min="1" max="5"><br><br>
<input type="submit" value="Submit">
</form>
The multiple
Attribute
The multiple
attribute allows users to enter multiple values in an input field.
It is applicable to input types such as email and file.
Example
A file upload field that allows multiple
selections:
The multiple attribute specifies that the user is allowed to enter more than one value in an input field.
To select multiple files, hold down the CTRL or SHIFT key while selecting.
<h1>The input multiple attributes</h1>
<p>The multiple attribute specifies that the user is allowed to enter more than one value in an input field.</p>
<form>
<label for="files">Select files:</label>
<input type="file" id="files" name="files" multiple><br><br>
<input type="submit" value="Submit">
</form>
<p>To select multiple files, hold down the CTRL or SHIFT key while selecting.</p>
The pattern
Attribute
The pattern
attribute defines a regular expression that the input value must match when the form is submitted.
It works with input types such as text, date, search, url, tel, email, and password.
global
title attribute to explain the pattern and assist the user.Example
An input
field that accepts only three letters, with no numbers or special characters:
The pattern attribute specifies a regular expression that the input element's value is checked against.
<h1>The input pattern attribute</h1>
<p>The pattern attribute specifies a regular expression that the input element's value is checked against.</p>
<form>
<label for="country_code">Country code:</label>
<input type="text" id="country_code" name="country_code" pattern="[A-Za-z]{3}" title="Three letter country code"><br><br>
<input type="submit" value="Submit">
</form>
The placeholder
Attribute
The placeholder
attribute provides a brief hint about the expected value in an input field, such as a sample value or a description of the required format.
This hint appears inside the input field before the user starts typing.
The placeholder
attribute is compatible with input types like text, search, url, number, tel, email, and password.
Example
An input field displaying placeholder
text:
The placeholder attribute specifies a short hint that describes the expected value of an input field.
<h1>The input placeholder attribute</h1>
<p>The placeholder attribute specifies a short hint that describes the expected value of an input field.</p>
<form>
<label for="phone">Enter a phone number:</label>
<input type="tel" id="phone" name="phone" placeholder="123-45-678" pattern="[0-9]{3}-[0-9]{2}-[0-9]{3}"><br><br>
<input type="submit" value="Submit">
</form>
The required
Attribute
The required
attribute ensures that an input field must be completed before the form can be submitted.
It is applicable to input types such as text, search, url, tel, email, password, date pickers, number, checkbox, radio, and file.
Example
An input
field that must be filled out:
The required attribute specifies that an input field must be filled out before submitting the form.
<h1>The input required attribute</h1>
<p>The required attribute specifies that an input field must be filled out before submitting the form.</p>
<form>
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<input type="submit" value="Submit">
</form>
The step
Attribute
The step
attribute in an input field defines the acceptable intervals for numeric input.
For instance, if step="3"
, the allowed values would be -3, 0, 3, 6, and so on.
max
and min
attributes to set a specific range of valid values.The step
attribute is compatible with the following input types: number, range, date, datetime-local, month, time, and week.
Example
An input
field with defined valid number intervals:
The step attribute specifies the legal number intervals for an input element.
<h1>The input step attribute</h1>
<p>The step attribute specifies the legal number intervals for an input element.</p>
<form>
<label for="points">Points:</label>
<input type="number" id="points" name="points" step="3">
<input type="submit" value="Submit">
</form>
The autofocus
Attribute
The autofocus
attribute on an input field ensures that the field automatically receives focus when the page is loaded.
Example
Make the "First name"
input field automatically receive focus when the page loads:
The autofocus attribute specifies that the input field should automatically get focus when the page loads.
<h1>The input autofocus attribute</h1>
<p>The autofocus attribute specifies that the input field should automatically get focus when the page loads.</p>
<form>
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" autofocus><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname"><br><br>
<input type="submit" value="Submit">
</form>
The list
Attribute
The list
attribute in an input element points to a <datalist>
element, which contains a set of predefined options.
Example
An <input>
element with a set of predefined options in a <datalist>
:
The list attribute refers to a datalist element that contains pre-defined options for an input element.
<h1>The input list attribute</h1>
<p>The list attribute refers to a datalist element that contains pre-defined options for an input element.</p>
<form>
<input list="browsers" name="browser">
<datalist id="browsers">
<option value="Edge">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
</datalist>
<input type="submit" value="Submit">
</form>
The autocomplete
Attribute
The autocomplete
attribute for an input field or form controls whether autocomplete
is enabled or disabled.
When enabled, autocomplete
allows the browser to suggest values as the user types, based on previously entered information.
The autocomplete
attribute can be used with <form>
elements and the following input types: text, search, url, tel, email, password, date, range, and color.
Example
An HTML form with autocomplete
enabled, but disabled for one specific input field:
The autocomplete attribute specifies whether or not an input field should have autocomplete enabled.
Fill in and submit the form, then reload the page to see how autocomplete works.
Notice that autocomplete is "on" for the form, but "off" for the e-mail field!
<h1>The autocomplete attribute</h1>
<p>The autocomplete attribute specifies whether or not an input field should have autocomplete enabled.</p>
<p>Fill in and submit the form, then reload the page to see how autocomplete works.</p>
<p>Notice that autocomplete is "on" for the form, but "off" for the e-mail field!</p>
<form autocomplete="on">
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname"><br><br>
<label for="lname">Last name:</label>
<input type="text" id="lname" name="lname"><br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" autocomplete="off"><br><br>
<input type="submit" value="Submit">
</form>
autocomplete
feature through the browser's "Preferences" menu for this to function properly.Conclusion
In conclusion, HTML input attributes offer essential functionality for customizing form behavior, ensuring data validation, and enhancing user experience. They allow developers to control field values, limits, accessibility, and interactivity. By leveraging attributes like value
, maxlength
, and required
, forms become more intuitive and user-friendly.
wrap
The HTML wrap attribute in the <textarea> element controls how text wraps within the input area when it exceeds the visible width.
abbr
The abbr attribute in HTML provides a way to shorten the text within <th> elements, improving the accessibility of tables. It is especially useful for long phrases, helping screen readers to convey the information more clearly.