The HTML <input type="password"> element is used to create a password field in a form. The entered characters are hidden or masked for security purposes.
- Used to accept secure password input by hiding entered characters.
- Created using <input type="password"> and commonly used in login and registration forms.
- Supports attributes like placeholder, required, and maxlength.
Syntax:
<input type="password">Note: We can add the <label> tag for improved accessibility.
Example: Using the HTML <input type="password"> to create a password field where characters are masked, enhancing security by hiding sensitive information like passwords from plain view.
<!--Driver Code Starts-->
<!DOCTYPE html>
<html>
<body>
<h3>HTML input type="password" Example</h3>
<!--Driver Code Ends-->
<form action="/action_page.php" method="post">
<label for="email">Email:</label>
<input type="email" name="email"
placeholder="Email Address"
required pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$">
<br>
<br>
<label for="paswword">Password:</label>
<input type="password" name="password"
placeholder="Password" required>
<br>
<br>
<input type="submit" value="Login">
</form>
<!--Driver Code Starts-->
</body>
</html>
<!--Driver Code Ends-->
Note: Always ensure that forms containing sensitive information, such as passwords, are served over HTTPS to protect user data.
Best Practices for HTML Password Input
Here are a few tips for using <input type="password">:
- Password Input in HTML Forms: Use <input type="password"> to collect password information securely in HTML forms. This masks the entered text for privacy.
- Hide Password in HTML: Password masking is done automatically with <input type="password">, preventing the password from being visible in plain text and enhancing security.