Welcome Login

Firefox: Forcing firefox not to autocomplete for password fields


Autocomplete is a useful feature for automatically filling in common form inputs.  However, you would want it applied to a password field.

IE uses a non-standard attribute (autocomplete="off") that can be added to a specified input control, or to an entire form.

Firefox doesn't recognise this attribute, so to get round this problem, you can simply clear the password input when it's automatically populated on Firefox after page loads using jquery.

 

Eg:

To fix password auto-complete:

firefox-autocomplete1

 

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />

<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.0.min.js"></script>

</head>

<body>
<form>
    Username: <input type="text" id="UserName"><br /><br />
    Password: <input type="password" id="Password"><br /><br />

    <input type="button" id="submit" value="submit">
</form>


<!-- load script last -->
<script type="text/javascript">

  $(function () {
      // brutally clears a password field in firefox
      var t = setTimeout("ClearPasswordInput()",100);
  });
  
  function ClearPasswordInput() {
      $("Password").val("");
  }

</script> 

</body>
</html>

 

 

Eg2:

<script type="text/javascript">

  $(function () {
      // brutally clears a password field in firefox
      var t = setTimeout("ClearPasswordInput()",100);
  
      $("*[id$='UserName']").watermark("Enter your username");
      $("*[id$='Password']").watermark("Enter your password");
  });
  
  function ClearPasswordInput() {
      $("*[id$='Password']").val("");
  }

</script> 

 

 

Note the use of wildcard selectors in Eg2 (used for asp.net id selection).  For more information, see here.


Created on: Tuesday, October 30, 2012 by Andrew Sin