Vue实现输入框某一行高亮

实现边输入边校验是否符合限制规则,如果不符合使得这一行的字符颜色为红

<style scoped>
.main {
    width: 500px;
    margin-left: 100px;
    height: 500px;
    position: relative;
  }
  .common-style {
    width: 100%;
    height: 100%;
    font-size: 14px;
    font-family: monospace;
    word-break: break-all;
    letter-spacing: 1px; /* 设置字间距 */
    line-height: 1.5715;
  }
/* div */
  .highlight-shadow-input {
    position: absolute;
    top: 0;
    left: 0;
    margin-left: -100px;
    padding: 9px;
    border: 1px;
    box-sizing: border-box;
    overflow: auto;
    white-space: pre-wrap;
    text-align: left;
    vertical-align: bottom;
    z-index: 0;
  }
  .highlight-input {
    position: relative;
    padding: 5px 11px;
    display: block;
    margin-left: -100px;
    padding: 8px;
    box-sizing: border-box;
    background: rgba(0, 0, 0, 0);
    z-index: 999;
    word-break: break-all;
    resize: none;
  }
  .show-text {
    -webkit-text-fill-color: transparent;
  }
</style>

<style>
.vin-matching-error {
   color: #ff4d4f;
   position: relative;
   z-index: 10000;
   word-break: break-all;
   font-size: 14px;
   line-height: 1.5715;
}
</style>

<template>
   <div style="padding: 50px">
      <a-row>
         <a-col :span="8">
            <div class="main" >
            <div
              v-html="content"
              class="highlight-shadow-input common-style"
              spellcheck="false"
            ></div>
            <a-textarea
              v-model:value="vinStringq"
              cols="30"
              rows="10"
              class="highlight-input common-style"
              :class="{ 'show-text': vinStringq }"
              @input="processInput"
              @scroll="syncScroll"
              spellcheck="false"
              placeholder="请输入符合要求的数据。"
            ></a-textarea>
            <span v-if="!isValid" style="color: red; margin-left: -50px; margin-top: 10px"
              >请输入正确的 数据</span
            >
          </div>
         </a-col>
      </a-row>
   </div>
</template>

<script setup>
  import { ref } from 'vue';
  const vinStringq = ref('');
  const content = ref('');
  const isValid = ref(true);
  
  let syncScroll = (event) => {
    let contentDivDom = document.querySelector('.highlight-shadow-input');
    const scrollTop = event.target.scrollTop;
    contentDivDom.scrollTop = scrollTop;
  };

  function processInput() {
  const lines = vinStringq.value.split('\n');
  //写正则表达式 此表达式限制每一行必须为17位字符,并且不含A/a字母
  const regex = /^[B-Z0-9]{17}$/i;
  let isAllValid = true;
  let processedLines = lines.map((line) => {
    const trimmedLine = line.replace(/\s/g, ''); // 去除空格
    if (trimmedLine !== '' && (trimmedLine.length !== 17 || !regex.test(trimmedLine))) {
      isAllValid = false;
      return `<span style="color:#e52e2e">${line}</span>`;
    } else {
      return (!line ? `<span></span> `: line);
    }
  });
  content.value = processedLines.join('<br>');
  isValid.value = isAllValid;
}


</script>

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值