禁止(防止)浏览器自动填充密码

本文介绍了一种通过JavaScript逻辑控制来禁用浏览器自动填充密码的方法,该方法通过改变密码输入框的属性并监听鼠标和输入事件来实现密码输入的安全防护。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 由于设置autocomplete属性和添加隐藏文本框,以及在初始化时阻止写入等方法都无法完全满足需求,所以只能通过js逻辑来控制。

 

  • 效果图

 

  • 主要代码  
      /**
        *
        * 禁止浏览器自动填充密码
        *
        * @method disabledRememberPassword
        * @param {any} el 目标(可多个)
        *
        */
        function disabledRememberPassword(el) {
            var _el = typeof el === 'object' ? el : $(el);
            if (!_el || _el.length == 0)
                return;
            _el.each(function (index, item) {
                $(item).attr('ilength', 0).attr('autocomplete', 'off').attr('type', 'text').attr('readonly', 'readonly').val('').on('focus', function () {
                    this.type != 'password' ? this.type = 'password' : 1;
                }).on('mouseout', function () {
                    this.setAttribute('readonly', 'readonly');
                }).on('mouseover', function () {
                    this.removeAttribute('readonly');
                }).on('input', function () {
                    this.setAttribute('ilength', this.value.length > item.attributes['ilength'].value ? ++item.attributes['ilength'].value : --item.attributes['ilength'].value);
                });
                var clear = () => {
                    !item.value ? setTimeout(check, 500) : (item.value = '', setTimeout(clear, 100));
                };
                var check = () => {
                    item.value.length != item.attributes['ilength'].value ? (item.setAttribute('ilength', 0), item.value.length == 0 ? setTimeout(check, 500) : (layer.tips('检测到密码输入异常,已自动拦截', item, {
                        tips: [2, '#000000'],
                        time: 2000
                    }), clear())) : setTimeout(check, 500);
                };
                check();
            });
        }

     

     

     说明:其中提示相关的代码使用的是layui的layer模块,可以换成自己想用的东西,或者不进行任何提示。

 

  • 使用方式
    1. 单个
      <body>
      <input id="password"/>
      </body>
      <script>
        $(function(){
          disabledRememberPassword('#password');
          //或者
          disabledRememberPassword($('#password'));
        })
      </script>

       

       

    2. 多个
      <body>
      <input id="password_0">
      <input id="password_1">
      ......
      </body>
      <script>
          $(function(){
              disabledRememberPassword('#password_0,#password_1')
              //或者
              disabledRememberPassword($('#password_0,#password_1'))
          })
      </script>

       

    3. 也可以直接写在body的onload中
      <body onload="disabledRememberPassword('#password')">
          <input id="password" />
      </body>

       

  • 总结

      其实比较完善的解决方式是

      1. 在登录页以及其他会使浏览器提示是否记住密码的页面,放置说明和提示,告知用户这样操做会存在风险
      2. 在需要输入密码的地方使用这个js方法进行防范

  

  

  如果这些有帮助到你,记得点个赞哟。有什么疑问的话,可以在评论区留言。

 

posted on 2019-04-09 13:17 LCTR 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lctr/p/10676271.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值