当前位置: 首页 > article >正文

Vue el-input密码输入框 按住显示密码,松开显示*;阻止浏览器密码回填,自写密码输入框;校验输入非汉字内容;文本框聚焦到内容末尾;

输入框功能集合

<template>
  <div style="padding: 10px">
  
    <!-- 密码输入框 -->
    <el-input
      :type="inputType"
      v-model="password"
      placeholder="请输入密码"
      auto-complete="new-password"
      id="pwd"
      style="width: 200px; margin-right: 10px"
    >
      <i
        class="el-input__icon el-icon-view el-input__clear"
        slot="suffix"
        @mousedown="showPwd('text')"
        @mouseup="showPwd('password')"
        @mouseleave="showPwd('password')"
        style="user-select: none"
      ></i>
    </el-input>
    <el-button @click="focusFunc" type="primary">聚焦结尾</el-button>
    <br />
    
    <!-- 账号输入框 限制仅允许输入非汉字 -->
    <el-input
      v-model="loginForm.userCode"
      clearable
      type="text"
      placeholder="输入您的账号"
      @input="checkChinese"
      style="width: 200px; margin: 10px 0"
    >
    </el-input>
    <br />
    
    <!-- 自写密码输入框 根源上阻止密码回填 -->
    <el-input
      v-model="pwdCover"
      type="text"
      id="pwd"
      placeholder="输入您的密码"
      @input="setPassword"
      style="width: 200px; margin-right: 10px"
    >
      <i
        slot="suffix"
        class="el-input__icon el-icon-view el-input__clear"
        @mousedown="hidePassword(true)"
        @mouseup="hidePassword(false)"
        @mouseleave="hidePassword(false)"
      ></i>
    </el-input>
  </div>
</template>

<script>
export default {
  data() {
    return {
      inputType: "password", //输入框类型
      password: "", //密码
      //
      pwdCover: "", // 密码 临时显示变量
      isShowPassword: false, // 显示密码标志位
      loginForm: {
        userCode: "", // 账号
        password: "", // 密码
      },
    };
  },
  methods: {
    // 显示密码
    showPwd(key) {
      this.inputType = key;
    },
    // 聚焦到输入框结尾
    focusFunc() {
      this.$nextTick(() => {
        var num = this.password.length;
        var dom = document.getElementById("pwd");
        dom.focus(); //聚焦
        dom.setSelectionRange(num, num); //移动光标
      });
    },

    // 校验输入非汉字
    checkChinese(value) {
      if (value) {
        if (/[\u4E00-\u9FA5]/g.test(value)) {
          this.loginForm.userCode = value.replace(/[\u4E00-\u9FA5]/g, "");
        }
      }
    },
    // 输入框输入事件
    setPassword(val) {
      if (val) {
        if (/[\u4E00-\u9FA5]/g.test(val)) {
          val = val.replace(/[\u4E00-\u9FA5]/g, "");
          this.pwdCover = val;
        }
      }
      if (this.isShowPassword) {
        this.loginForm.password = val;
      } else {
        // let reg = /[0-9a-zA-Z]/g; // 只允许输入字母和数字
        let reg = /./g; // 只允许输入字母和数字
        let nDot = /[^●]/g; // 非圆点字符
        let index = -1; // 新输入的字符位置
        let lastChar = void 0; // 新输入的字符
        let realArr = this.loginForm.password.split(""); // 真实密码数组
        let coverArr = val.split(""); // 文本框显示密码数组
        let coverLen = val.length; // 文本框字符串长度
        let realLen = this.loginForm.password.length; // 真实密码长度
        // 找到新输入的字符及位置
        coverArr.forEach((el, idx) => {
          if (nDot.test(el)) {
            index = idx;
            lastChar = el;
          }
        });
        // 判断输入的字符是否符合规范,不符合的话去掉该字符
        if (lastChar && !reg.test(lastChar)) {
          coverArr.splice(index, 1);
          this.pwdCover = coverArr.join("");
          return;
        }
        if (realLen < coverLen) {
          // 新增字符
          realArr.splice(index, 0, lastChar);
        } else if (coverLen <= realLen && index !== -1) {
          // 替换字符(选取一个或多个字符直接替换)
          realArr.splice(index, realLen - (coverLen - 1), lastChar);
        } else {
          // 删除字符,因为 val 全是 ● ,没有办法匹配,不知道是从末尾还是中间删除的字符,删除了几个,不好对 password 处理,所以可以通过光标的位置和 val 的长度来判断
          let pos = document.getElementById("pwd").selectionEnd; // 获取光标位置
          realArr.splice(pos, realLen - coverLen);
        }
        // 将 pwdCover 替换成 ●
        this.pwdCover = val.replace(/\S/g, "●");
        this.loginForm.password = realArr.join("");
      }
    },
    // 点击右侧小眼睛控制显示隐藏
    hidePassword(flag) {
      if (flag) {
        console.log("显示");
        this.isShowPassword = true;
        this.pwdCover = this.loginForm.password;
      } else {
        console.log("隐藏");
        this.isShowPassword = false;
        this.pwdCover = this.pwdCover.replace(/\S/g, "●");
      }
    },
  },
};
</script>

拓展内容 selectionStart 与 selectionEnd

在 HTML 中,文本框和文本域都有 selectionStart 和 selectionEnd 这两个属性。它们分别表示当前选定文本的起始位置和结束位置,以字符为单位。
光标起始位置 selectionStart
光标结束位置 selectionEnd
let pos = document.getElementById("pwd").selectionEnd;// 举个栗子

实用例子

const textarea = document.querySelector('textarea');
const start = textarea.selectionStart;
const end = textarea.selectionEnd;
const textToReplace = 'hello world';
textarea.value = textarea.value.substring(0, start) + textToReplace + textarea.value.substring(end);//替换
textarea.value = textarea.value.substring(0, start) + textarea.value.substring(end);//删除

http://www.kler.cn/a/535403.html

相关文章:

  • 基于Qt的Ribbon界面框架
  • DES 3DES 简介 以及 C# 和 js 实现【加密知多少系列_2】
  • 八大排序算法细讲
  • go流程控制
  • 【Uniapp-Vue3】z-paging插件组件实现触底和下拉加载数据
  • 【华为OD-E卷 - 108 最大矩阵和 100分(python、java、c++、js、c)】
  • 68.浏览文件并选择文件 C#例子 WPF例子
  • 在K8S中,如何解决SVC容灾问题?
  • 想品客老师的第十二天:异步和promise
  • 图片PDF区域信息批量提取至Excel,基于QT和阿里云api的实现方案
  • Unity 简易的UI框架
  • C和Rust的一些区别
  • C中静态库和动态库的使用
  • 数据治理项目为什么沦为了PPT工程?
  • 2025.2.6(c++杂项补充及qt基础介绍)
  • Vue Dom截图插件,截图转Base64 html2canvas
  • H5+CSS+JS制作好看的轮播图
  • OSPF基础(2):数据包详解
  • 51单片机07 串口通信
  • 【C语言】常量指针和指针常量,指针数组和数组指针,指针函数和函数指针怎么区分?
  • vue2-nextTick
  • JAVA面试框架篇
  • 注册中心不知选哪个?Zookeeper、Eureka、Nacos、Consul和Etcd 5种全方位剖析对比
  • Python利用VideoCapture和FFmpeg读取多个rtsp流性能的比较
  • idea整合deepseek实现AI辅助编程
  • 【React】表单校验:从基础到集成库