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);//删除
相关推荐
paopaokaka_luck27 分钟前
基于SpringBoot+Uniapp的健身饮食小程序(协同过滤算法、地图组件)
前端·javascript·vue.js·spring boot·后端·小程序·uni-app
患得患失9491 小时前
【前端】【vscode】【.vscode/settings.json】为单个项目配置自动格式化和开发环境
前端·vscode·json
飛_1 小时前
解决VSCode无法加载Json架构问题
java·服务器·前端
YGY Webgis糕手之路4 小时前
OpenLayers 综合案例-轨迹回放
前端·经验分享·笔记·vue·web
90后的晨仔4 小时前
🚨XSS 攻击全解:什么是跨站脚本攻击?前端如何防御?
前端·vue.js
Ares-Wang4 小时前
JavaScript》》JS》 Var、Let、Const 大总结
开发语言·前端·javascript
90后的晨仔4 小时前
Vue 模板语法完全指南:从插值表达式到动态指令,彻底搞懂 Vue 模板语言
前端·vue.js
德育处主任4 小时前
p5.js 正方形square的基础用法
前端·数据可视化·canvas
烛阴4 小时前
Mix - Bilinear Interpolation
前端·webgl
90后的晨仔4 小时前
Vue 3 应用实例详解:从 createApp 到 mount,你真正掌握了吗?
前端·vue.js