Definition and Usage
This is a basic control that allows users to enter plain single-line text.
Basic Example
<label for="user-id">User ID</label>
<input type="text" id="user-id">
Usage Notes
The type attribute of the <input> tag includes several control types that allow users to enter single-line text, not just type="text".
type |
Description | Basic Example |
|---|---|---|
<input type="email"> |
This is an email address input field.
It looks like a text input, but on some devices with dynamic virtual keyboards (such as mobile devices), it supports a keyboard that helps with entering email addresses such as the @ and . characters. |
|
<input type="number"> |
This is a numeric input field.
It displays a spinner and performs validation to allow only numbers to be entered. On some devices with dynamic virtual keyboards (such as mobile devices), it displays a numeric keypad. |
|
<input type="password"> |
This is a password input field.
The browser hides sensitive information entered by the user by replacing text characters with symbols such as bullets (•), making them unreadable on the screen. |
|
<input type="search"> |
This is a search term input field.
It is a single-line text input control, and on some devices with dynamic virtual keyboards (such as mobile devices), it supports a search icon. Depending on the browser, it may include a delete icon that can be used to clear the field. |
|
<input type="tel"> |
This is a phone number input field.
It is a single-line text input control and does not validate the phone number format. |
|
<input type="url"> |
This is a URL input field.
It looks like a text input, but on some devices with dynamic virtual keyboards (such as mobile devices), it supports a keyboard that helps with entering URLs, with keys for characters such as / and . and keys for quickly entering strings commonly found in domain names, such as www. or .com. |
The type="text" attribute is suitable for entering plain text, rather than a more specific or specialized single-line text input field such as type="email", type="number", type="password", type="search", type="tel", or type="url".
Using Related Attributes
Let's learn about the attributes related to <input type="text">.
placeholder Attribute
The placeholder attribute specifies the text displayed in a text input control when no value is set. This is called placeholder text.
This value should provide the user with a brief hint about the expected type of data to enter into the control.
When the user starts entering text, the placeholder text disappears from the text input field.
<label for="user-id">User ID</label>
<input type="text" id="user-id" placeholder="10 characters max">
For information on styling placeholder text, see Styling CSS Placeholder Text.
Accessibility Considerations
The placeholder attribute cannot replace the label of the control.
placeholder attribute cannot replace the label of the control.
<input type="text" placeholder="User ID">
For more information, see Accessibility Considerations for the <input> Tag.
value Attribute
The value attribute sets the initial value.
This value specifies the text displayed in the element when the page loads.
<label for="user-id">User ID</label>
<input type="text" id="user-id" value="myid#123">
disabled Attribute
The disabled attribute is used to disable the element. A disabled element does not function even if the user tries to enter or change its value, because its inherent functionality is disabled. In addition, its value is not submitted when the form is submitted.
It is a Boolean attribute, meaning you only need to include the attribute name without specifying a value.
<label for="user-id">User ID</label>
<input type="text" id="user-id" value="myid#123" disabled>
However, JavaScript, which can set the value attribute directly, can still change the value of the control.
readonly Attribute
The readonly attribute prevents the control from being changed or edited.
It is a Boolean attribute, meaning you only need to include the attribute name without specifying a value.
It is mainly used with the value attribute to display the value entered or stored in the control to the user while preventing it from being edited.
<label for="user-id">User ID</label>
<input type="text" id="user-id" value="myid#123" readonly>
However, JavaScript, which can set the value attribute directly, can still change the value of the control.
maxlength Attribute
The maxlength attribute limits the maximum number of characters (maximum length) that the user can enter. When the maxlength attribute is specified for the element, it prevents the user from entering more characters than allowed.
- The attribute value must be a non-negative integer (0 or greater).
- If an invalid value is specified, the maximum length restriction is not applied.
- The maximum number of characters is calculated in standard character units (UTF-16 code units rather than bytes), so the same restriction applies regardless of the language, including English, numbers, and Korean. However, some emoji, such as 😀, are represented by two code units in UTF-16, so the maximum number of characters may not correspond to the number of characters perceived by the user.
Note
If a valid minlength attribute is specified for the element, the value of the maxlength attribute must be greater than or equal to that value.
<label for="user-id">User ID</label>
<!-- Limit input to a maximum of 10 characters with maxlength="10" -->
<input type="text" id="user-id" maxlength="10" placeholder="10 characters max">
minlength Attribute
The minlength attribute specifies the minimum number of characters (minimum length) that the user must enter. When the minlength attribute is specified for the element, the user must enter at least the specified minimum number of characters.
Note
The minlength attribute only checks the minimum number of characters that must be entered. If the user does not enter anything in the element, an empty value is allowed. If you do not want to allow an empty value, use the required attribute as well.
Additional Explanation
The required attribute indicates that the control is required, meaning that the user must enter a value before submitting the form.
- The attribute value must be a non-negative integer (0 or greater).
- If an invalid value is specified, the minimum length restriction is not applied.
- The minimum number of characters is calculated in standard character units (UTF-16 code units rather than bytes), so the same restriction applies regardless of the language, including English, numbers, and Korean. However, some emoji, such as 😀, are represented by two code units in UTF-16, so the minimum number of characters may not correspond to the number of characters perceived by the user.
Note
If a valid maxlength attribute is specified for the element, the value of the minlength attribute must be less than or equal to that value.
<form>
<label for="nickname">Nickname</label>
<!-- Require a minimum of 2 characters with minlength="2" -->
<input type="text" id="nickname" minlength="2" placeholder="Enter 2+ characters..." required>
<button type="submit">Submit</button>
</form>
- Check for Errors: Leave the input field empty or enter only 1 character, then click the [Submit] button. Confirm that the browser displays an error message indicating that the minimum input requirement (2 characters) has not been met.
- Check for Success: Enter 2 or more characters in the field, then click the [Submit] button again. Confirm that the form is submitted without an error.
required Attribute
The required attribute indicates that the control is required, meaning that the user must enter a value before submitting the form. It is a Boolean attribute, meaning you only need to include the attribute name without specifying a value.
<form>
<fieldset>
<legend>Name is required</legend>
<p>
<label for="username">Name (*required)</label>
<input id="username" type="text" required> <!-- Specify as a required input field -->
</p>
<p>
<label for="userpassword">Password</label>
<input id="userpassword" type="password">
</p>
<p>
<button type="submit">Submit</button>
</p>
</fieldset>
</form>
- Check for Errors: Without entering anything in the Name (*required) field, click the [Submit] button. Confirm that the browser displays an error message for the required field.
- Check for Success: Enter a value in the Name (*required) field, then click the [Submit] button again. Confirm that the form is submitted without an error.
pattern Attribute
The pattern attribute specifies the regular expression used to validate the input value.
<input type="text" pattern="Specify a regular expression">
name Attribute
The name attribute specifies the name of the element.
When the name attribute is specified, its value is used as the name of the data sent when the form is submitted. In JavaScript, the value of the name attribute is also used as the name of the element in the form.elements API.
size Attribute
The size attribute specifies the width of the element based on the number of characters.
The value specified by the size attribute specifies the visual width of the input field based on the number of characters. Therefore, the actual displayed width may vary depending on the font size (font-size) that is set.
The value of the size attribute must be specified as an integer greater than 0.
If the size attribute is not specified or an invalid value is set, most browsers use 20 as the default value.
The size attribute only sets the visual width of the input field and does not affect validation for limiting the number of characters entered when the form is submitted.
<p>
<label>
No size attribute
<input type="text">
</label>
</p>
<p>
<label>
Short input
<input type="text" size="5">
</label>
</p>
<p>
<label>
Long input
<input type="text" size="50">
</label>
</p>
If you specify a width for an element with the size attribute using CSS, the CSS width takes precedence.
list Attribute
The list attribute specifies the value of the id attribute of the <datalist> tag that provides a list of predefined options for the <input type="text">.
The user can select an option from this predefined list or enter a different value. When the user clicks or double-clicks the control, the corresponding suggested values are displayed in a drop-down menu. For example, in the Chrome browser, an arrow (▼) is also displayed on the right side of the control to indicate that predefined values are available.
<label for="fruits">Fruit Gift</label>
<input type="text" list="fruit-options" id="fruits">
<!-- Display in the drop-down menu -->
<datalist id="fruit-options">
<option value="Apple"></option>
<option value="Banana"></option>
<option value="Cherry"></option>
<option value="Date"></option>
</datalist>
Browser Compatibility
| Attribute |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
<input type="text">
|
1 | 12 | 1 | 1 |
list
|
20 | 12 | 4 | 12.1 |
Specifications
| Specification | |
|---|---|
<input type="text">
|
HTML Standard #text-(type=text)-state-and-search-state-(type=search) |