Is there a W3C valid way to disable autocomplete in a HTML form?

后端 未结 18 1128
心在旅途
心在旅途 2020-11-22 05:58

When using the xhtml1-transitional.dtd doctype, collecting a credit card number with the following HTML



        
相关标签:
18条回答
  • 2020-11-22 06:20

    I MADE THIS WORK IN 2020!

    I basically create a css class that applies -webkit-text-security to my inputs.

    Here's the link to a more recent discussion: https://stackoverflow.com/a/64471795/8754782

    0 讨论(0)
  • 2020-11-22 06:22

    No, but browser auto-complete is often triggered by the field having the same name attribute as fields that were previously filled out. If you could rig up a clever way to have a randomized field name, autocomplete wouldn't be able to pull any previously entered values for the field.

    If you were to give an input field a name like "email_<?= randomNumber() ?>", and then have the script that receives this data loop through the POST or GET variables looking for something matching the pattern "email_[some number]", you could pull this off, and this would have (practically) guaranteed success, regardless of browser.

    0 讨论(0)
  • 2020-11-22 06:22

    Valid autocomplete off

    <script type="text/javascript">
        /* <![CDATA[ */
        document.write('<input type="text" id="cardNumber" name="cardNumber" autocom'+'plete="off"/>');
        /* ]]> */ 
    </script>
    
    0 讨论(0)
  • 2020-11-22 06:23

    Here is a good article from the MDC which explains the problems (and solutions) to form autocompletion. Microsoft has published something similar here, as well.

    To be honest, if this is something important to your users, 'breaking' standards in this way seems appropriate. For example, Amazon uses the 'autocomplete' attribute quite a bit, and it seems to work well.

    If you want to remove the warning entirely, you can use JavaScript to apply the attribute to browsers that support it (IE and Firefox are the important browsers) using someForm.setAttribute( "autocomplete", "off" ); someFormElm.setAttribute( "autocomplete", "off" );

    Finally, if your site is using HTTPS, IE automatically turns off autocompletion (as do some other browsers, as far as I know).

    Update

    As this answer still gets quite a few upvotes, I just wanted to point out that in HTML5, you can use the 'autocomplete' attribute on your form element. See the documentation on W3C for it.

    0 讨论(0)
  • 2020-11-22 06:23

    How about setting it with JavaScript?

    var e = document.getElementById('cardNumber');
    e.autocomplete = 'off'; // Maybe should be false
    

    It's not perfect, but your HTML will be valid.

    0 讨论(0)
  • 2020-11-22 06:27

    autocomplete="off" this should fix the issue for all modern browsers.

    <form name="form1" id="form1" method="post" autocomplete="off"
       action="http://www.example.com/form.cgi">
      [...]
    </form>
    

    In current versions of Gecko browsers, the autocomplete attribute works perfectly. For earlier versions, going back to Netscape 6.2, it worked with the exception for forms with "Address" and "Name"

    Update

    In some cases, the browser will keep suggesting autocompletion values even if the autocomplete attribute is set to off. This unexpected behavior can be quite puzzling for developers. The trick to really forcing the no-autocompletion is to assign a random string to the attribute, for example:

    autocomplete="nope"
    

    Since this random value is not a valid one, the browser will give up.

    Documetation

    0 讨论(0)
提交回复
热议问题