The real 2019 solution to disable chrome autofill

Viewed 3521

I know this question has a lot of answers. I have looked through all the solutions to disable google autocomplete(the drop down of suggestions), like using autocomplete=0ff or autocomplete=false, but nothing has solved the issue. I have created an MVC app that has views with dropdown lists and HTML EditorFor.

One solution to add a name to the HTML editor for, helped to remove autocomplete, however since I changed the name of the HTML EditorFor, I had an issue posting back the value.

<div class="col-md-10">
     @Html.EditorFor(model => model.Address, new {htmlAttributes = new { @class = "form-control", @id = "show_address", Name = Guid.NewGuid().ToString(), autocomplete = "noped" } })
</div>

Does anybody have a solution for 2019 to disable the google autocomplete?

Update:

I tried using html.textboxfor(as given in the first solution below), however I have realised that autocomplete=off only works if there is one other textboxfor in the view. If there is multiple textboxfor in the same view, using autocomplete=off on any of the Html Textboxfor will not work for any of them to disable autocomplete! Can anyone please help?

5 Answers

EditorFor is having its own some disadvantages, it did not work in some scenarios, a better way to use TextBoxFor instead of EditorFor.Also, It did not affect Postback value.(For more details check here)

@Html.TextBoxFor(model => model.Address, new { @class = "form-control",  @id = "show_address", autocomplete = "off" })

UPDATE: Check my updated demo DEMO
After adding autocomplete=off, still some browser ignores them and they try to show you some hint or autofill. More info check this
Add some random_value in autocomplete, so browsers consider as an off.

@Html.TextBoxFor(model => model.Address1, new { @class = "form-control",  @id = "show_address", autocomplete = "some_random_value" })

I think I have found the answer. I give credit for @Mangesh Ati for this solution. I just wanted to summarise the solution for anyone else interested.

autocomplete=off works to disable google autosuggestions on all @htmlTextBoxFor, besides for the part of model called address, instead use autocomplete=randomn_string

Important: If you are using a jquery autocomplete on the textboxfor..its important to add the attribute of autocomplete=randomn_string on .focus like below:

 $('#show_address').autocomplete({

            }).focus(function () {
                 $(this).attr('autocomplete', 'some_random_value');

            });

Did you try?

<%= Html.TextBoxFor(
    model => model.date, 
    new { @class = "aDatePicker", autocomplete = "off" }
)%>

It will generate markup that is close to the following:

<input type="text" id="date" name="date" class="aDatePicker" autocomplete="off" />

Also you can try:

//Disable autocomplete throughout the site
$(document).ready(function() {
    $("input:text,form").attr("autocomplete","off");
})

In VB this worked for me (use C# converter):

@Html.EditorFor(Function(m) m.Password, New With {.class = "form-control text-center", .autocomplete = "new-password"})
<input type="any" readonly onfocus="this.removeAttribute('readonly');">
Related