You can’t directly prevent a browser from storing passwords using HTML and JavaScript due to security restrictions.
Browsers have built-in mechanisms for saving passwords, and these mechanisms are controlled by the browser itself, not by web pages.
However, you can use the autocomplete
attribute to suggest that the browser not save passwords for a specific form field.
This attribute can be set to off
for input fields where you don’t want the browser to store passwords.
Here’s an example:
<form>
<label for="username">Username:</label>
<input type="text" id="username" name="username" autocomplete="off">
<br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" autocomplete="off">
<br>
<input type="submit" value="Submit">
</form>
In this example, the autocomplete="off"
attribute is added to both the username and password input fields.
This suggests to the browser that it should not save passwords for these fields.
However, keep in mind that the browser may not always honor this suggestion, as it ultimately depends on the browser’s implementation and the user’s preferences.
Additionally, some browsers may allow users to override the autocomplete
attribute settings or may have their own settings that control password saving behavior.
Therefore, while autocomplete="off"
can be used as a suggestion to the browser, it’s not a foolproof method for preventing password storage.