How to disable Chrome autocomplete feature?
Asked Answered
W

15

23

We want to disable autocomplete in Chrome browser in our React JavaScript application. We have tried a bunch of solutions available on the Internet but nothing worked. autoComplete=off is not reliable and so are other ways.

This is really important for us at this moment so can you please suggest us a foolproof way to disable autocomplete in Chrome using React JavaScript?

Secondly, we are using a common control/component for our text boxes and using them everywhere

Wellfound answered 15/5, 2018 at 10:10 Comment(5)
Possible duplicate of Chrome Browser Ignoring AutoComplete=Off – Commercialize
Can you please tell why autoComplete = "off" is not reliable ? because it works like a charm. Please see the React example I have created here.. codesandbox.io/s/72q0z6rp61 – Regin
@johnsam Because it is working for few fields while not for the others. Working for 95% scenarios – Wellfound
@ShivaWahi this is tricky cause not sure what scenario is not working 😐.. if it was reproducible then we can help you out :) – Regin
@johnsam It is tough for me to explain too. On few situations.. sutocomplete"nope" works while somewhere off is working – Wellfound
P
29

Do autocomplete="new-password" to disable autocomplete. (For React, use autoComplete)

This is a hint, which browsers are not required to comply with. However modern browsers have stopped autofilling elements with autocomplete="new-password" for this very reason.

MDN

Pacifier answered 15/5, 2018 at 11:17 Comment(5)
thanks it's worked, but how, can anybody explain? – Danieldaniela
@Danieldaniela developer.mozilla.org/en-US/docs/Web/Security/… – Gony
This is invalid jsx – Broughton
@Broughton You've to do <input autocomplete="new-password"/> – Pacifier
With this solution you will see "random strong password" suggestions in Chrome. So, you will get rid with one problem and have a new one :D – Upholstery
R
14

You can override chrome autofill by add onFocus attribute.

render()
{
  return <input type="text" name="name" value="this is my input" autoComplete="off" onFocus={this.onFocus} />
}

In the onFocus method we need to change "autocomplete" attribute through javaScript.

onFocus = event => {

   if(event.target.autocomplete)
   {
     event.target.autocomplete = "whatever";
   }

};

This solution works for me.

let me know if it works for you ;)

Ruskin answered 21/7, 2019 at 7:23 Comment(2)
so far this is the only solution that sees to work for me, that actually makes any sort of sense. Ive tried so many other alternatives. but they sometimes work and sometimes they dont and are unreliable, thanks! – Cresset
thank you so much to worked for me i was trying different things nothing worked this worked I just used the on focus off – Jigging
L
13

Nothing worked for me including new-password, so this is how I did:

onFocus={(event) => {
  event.target.setAttribute('autocomplete', 'off');
  console.log(event.target.autocomplete);
}}
Logic answered 23/5, 2020 at 14:30 Comment(1)
Thank you, this is the only thing that worked for me. – Nichollenicholls
S
4

I got the same issue with my React project. My solution is to use a random string for autoComplete attribute. Don't use "off", as per Pim, you need to set a invalid value to really turn auto completion off. Please also note the attribute name has to be autoComplete in React.

Scherman answered 12/2, 2019 at 2:8 Comment(0)
N
2

When using jsx - you have to camel case attributes; so autoComplete="new-password" instead of autocomplete.

Nonplus answered 26/10, 2020 at 7:40 Comment(0)
H
2

For me changing just the autoComplete attribute to 'new-password' didn't work on its own because the name attribute of the field was set to 'password'. As soon as I changed that to something else it worked (in my case for example I used 'devicePassword')

<input type='password' name='devicePassword' autoComplete='new-password' />
Humorous answered 8/9, 2022 at 10:41 Comment(0)
E
1

Try not to set the type attribute, if you have a password and username, remove both of those input types and voila.. problem solved

Exhaust answered 5/10, 2022 at 19:36 Comment(0)
K
1

I solved with

<form autoComplete="off">
   <input
     autoComplete="new-off"
   />
</form>
Kloof answered 24/5, 2023 at 9:11 Comment(0)
R
0

The only hack that worked for me is to create hidden input and add random number for the original input name:

<input type="text" name="" value="" readOnly={true} style={{display: "none"}}/>
<input
  type="text"
  name={"address " + Math.random()}
/>
Rettarettig answered 18/6, 2019 at 23:12 Comment(0)
P
0

The only workaround that worked for me:

      onChange={({ target }) => {
          if (target.autocomplete === 'off') {
            setPassword(target.value);
          }
      }}
      onFocus={({ target }) => {
        target.setAttribute('autocomplete', 'off');
      }}

The goal is just to define any attribute and check if the value is correct. I chose autocomplete just to make it obvious.

Planula answered 16/12, 2021 at 12:30 Comment(0)
D
0

try setting autocomplete="new-password" to the <input type="password">, It works.

Dave answered 26/4, 2022 at 5:52 Comment(0)
A
0
Use both together ,for email use autoComplete="off" and for password use autoComplete="new-password"
Aret answered 1/6, 2023 at 17:24 Comment(0)
C
0

The only thing that worked for me from the above solutions was to set the password type to text. This prevented the autocomplete on all my form input fields. I then used state and useEffect to dynamically set the type to password as soon as a value was entered for the password.

useEffect(() => {
if (!isPasswordVisible) {
  if (password.length > 0) {
    setIsPasswordVisible(true);
  }
}

}, [password]);

And in the JSX,

<InputField
        id={'signup-password-input'}
        type={isPasswordVisible ? 'password' : 'text'}
        value={password}
        name='devicePassword'
        autoComplete='new-password'
        onChangeFunc={handlePasswordChange}>
        Password
      </InputField>

Obviously, I was using an InputField component in this case.

Cystotomy answered 2/1 at 16:37 Comment(1)
This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From Review – Thence
J
0

put this before your input element

<input name="abc" type="text" style={{display:'none'}}/>

Jenaejenda answered 6/1 at 5:53 Comment(0)
O
0

so i integrate two hidden input field on top of my existing input to trick chrome and this is the only solution which work for me

 <form>
  {/* Hidden input fields to trick autofill */}
  <input type="text" name="fake_user_name" autoComplete="username" style={{ display: 'none' }} />
  <input type="password" name="fake_password" autoComplete="new-password" style={{ display: 'none' }} />

  <div>
    <label htmlFor="email">Email:</label>
    <input
      type="email"
      id="email"
      name="user_email"
      value={email}
      onChange={handleEmailChange}
      autoComplete="email"
      placeholder="Enter your email"
    />
  </div>
  <div>
    <label htmlFor="phone">Phone:</label>
    <input
      type="tel"
      id="phone"
      name="user_phone"
      value={phone}
      onChange={handlePhoneChange}
      autoComplete="tel"
      placeholder="Enter your phone number"
    />
  </div>
  <button type="submit">Submit</button>
</form>
Oxtail answered 18/7 at 11:49 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.