Definition and Usage
The type="password" attribute of the <input> tag represents a single-line password input field.
The browser processes sensitive information entered by replacing the text characters with symbols such as dots (•) so that they cannot be read on the screen.
This field visually hides the entered text characters, but does not encrypt them for security.
In other words, it only hides the entered text characters so that people near the user cannot read them, and otherwise works the same way as a plain text field (type="text").
Basic Example
<label for="user-password">User Password</label>
<input type="password" id="user-password">
Using Related Attributes
Let's learn about the attributes related to <input type="password">.
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-password">User Password</label>
<input type="password" id="user-password" 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="password" placeholder="User Password">
For more information, see Accessibility Considerations for the <input> Tag.
value Attribute
The value attribute sets the initial value.
This value specifies the password displayed in the element when the page loads. However, the text characters are replaced with symbols such as dots (•).
<label for="user-password">User Password</label>
<input type="password" id="user-password" value="mypass#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-password">User Password</label>
<input type="password" id="user-password" value="mypass#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-password">User Password</label>
<input type="password" id="user-password" value="mypass#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-password">User Password</label>
<!-- Limit input to a maximum of 10 characters with maxlength="10" -->
<input type="password" id="user-password" 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="pw">User Password</label>
<!-- Require a minimum of 2 characters with minlength="2" -->
<input type="password" id="pw" 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>Password is required</legend>
<p>
<label for="username">Name</label>
<input id="username" type="text">
</p>
<p>
<label for="userpassword">Password (*required)</label>
<!-- Specify as a required input field -->
<input id="userpassword" type="password" required>
</p>
<p>
<button type="submit">Submit</button>
</p>
</fieldset>
</form>
- Check for Errors: Without entering anything in the Password (*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 Password (*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="password" 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="password">
</label>
</p>
<p>
<label>
Short input
<input type="password" size="5">
</label>
</p>
<p>
<label>
Long input
<input type="password" size="50">
</label>
</p>
If you specify a width for an element with the size attribute using CSS, the CSS width takes precedence.
Password Visibility Toggle (Show/Hide)
It is recommended to provide a password visibility toggle (show/hide) button so that users can verify the password they entered. As shown in the following example, this button uses JavaScript to implement this feature.
<label for="user-password">User Password</label>
<input type="password" id="user-password" value="12345" readonly>
<button type="button" id="toggle-password" onclick="togglePassword()">Show Password</button>
<script>
function togglePassword() {
const x = document.getElementById("user-password");
const toggleButton = document.getElementById("toggle-password");
if (x.type === "password") {
x.type = "text";
toggleButton.textContent = "Hide Password";
} else {
x.type = "password";
toggleButton.textContent = "Show Password";
}
}
</script>
Browser Compatibility
| Attribute |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
<input type="password"> |
1 | 12 | 1 | 1 |
placeholder
|
3 | 12 | 4 | 4 |
readonly
|
1 | 12 | 1 | 1 |
value
|
1 | 12 | 1 | 1 |
name
|
1 | 12 | 1 | 1 |
size
|
1 | 12 | 1 | 1 |
Specifications
| Specification | |
|---|---|
<input type="password">
|
HTML Standard #password-state-(type=password) |