瀏覽器的自動完成功能雖方便,但在銀行帳號、密碼、信用卡號等敏感資訊輸入框中會造成安全隱患。開發者無法強制用戶關閉瀏覽器設定,但可透過 HTML 屬性與 JavaScript 從頁面層面禁用該功能。
IE 5.0+、Firefox、Chrome、Edge 均支援 autocomplete 屬性,值為 on(預設)或 off。
<form method="post" action="submit.asp" autocomplete="off">
<!-- 表單內所有 input 將繼承 off -->
</form>
<input type="text" name="creditcard" maxlength="16" autocomplete="off" />
<input type="password" name="pwd" autocomplete="off" />
function disableAutoComplete(element) {
element.setAttribute("autocomplete", "off");
}
// 使用範例:頁面載入後針對特定元素
window.onload = function() {
disableAutoComplete(document.getElementById("txtCardNo"));
};
.NET 2.0+ 的 TextBox 控件提供 AutoCompleteType 屬性,設為 Disabled 即可:
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password" AutoCompleteType="Disabled" />
或在代碼後置:
txtPassword.AutoCompleteType = AutoCompleteType.Disabled;
若需禁止中文輸入法(僅限制輸入法切換,不防複製貼上):
<input type="text" style="ime-mode: disabled;" />
<asp:TextBox ID="txtEnglishOnly" runat="server" Style="ime-mode: disabled;" />
注意:此 CSS 僅在 IE/Edge(舊版)生效,現代瀏覽器建議使用 inputmode="latin" 或配合正則驗證。
type="password")務必加上 autocomplete="off"。off。<form autocomplete="off">,並配合 AutoCompleteType="Disabled" 雙重保險。autocomplete="off"(特別是密碼管理器),可嘗試 autocomplete="new-password" 或 autocomplete="one-time-code" 引導瀏覽器行為。正確配置自動完成屬性,是保護用戶隱私與帳號安全的基礎防線之一。