Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to turn off autocomplete for an input in html? [duplicate]

I have an input for getting password from user : <input name="Password" id="Password" required/> in most devices browser will try to autocomplete the password input ( based on saved passwords ), but lets say we don't want this behavior , **so how could we turn off autocomplete for an input? **

after searching and trying to turn off autocomplete found this solution, set attribute autocomplete to off : <input name="Password" id="Password" required autocomplete="off"/> but it didn't work, when page loads this will happen , i tried JavaScript too but it didn't worked neither $("#Password").val('')

like image 283
behin gostar Avatar asked Dec 23 '25 02:12

behin gostar


2 Answers

The solution for Chrome is to add autocomplete="new-password" to the input type password. Please check the example below.

Example:

<form name="myForm"" method="post">
   <input name="user" type="text" />
   <input name="pass" type="password" autocomplete="new-password" />
   <input type="submit">
</form>

Chrome always autocomplete the data if it finds a box of type password, just enough to indicate for that box autocomplete = "new-password".

Note: make sure with CTRL + F5 that your changes take effect. Many times, browsers save the page in the cache.

like image 197
Omid purdarbani Avatar answered Dec 24 '25 15:12

Omid purdarbani


There are 3 ways you could do this:-

  1. Add an attribute for the input field i.e, autocomplete = "off"

Example:-

<form>
   <input type="text" />
   <input type="password" autocomplete = "off"/>
   <input type="submit">
</form>
  1. Add an attribute for the entire form i.e, autocomplete = "off"

Example:-

<form method="post" autocomplete="off">
   <input type="text" />
   <input type="password" autocomplete = "off"/>
   <input type="submit">
</form>
  1. If the above 2 didn't work then, set autocomplete = "new-password" to the input field. This should definitely prevent the browser from saving the input data.

Example:-

<form method="post">
   <input type="text" />
   <input type="password" autocomplete = "new-password"/>
   <input type="submit">
</form>

The first 2 solutions might not work since modern browsers provide a feature to save passwords i.e, they may ask to save the login credentials which will prevent the autocomplete = "off" to act.

For Reference:- https://developer.mozilla.org/en-US/docs/Web/Security/Securing_your_site/Turning_off_form_autocompletion

like image 30
Rahul Kumar Avatar answered Dec 24 '25 16:12

Rahul Kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!