只能输入50个中文。100个英文。

<el-input v-model="temp.postFailMsg" :placeholder="$t('configForm.Postfailuremessage')"  @focus="showTips($event)"  @blur="hideTips($event)"   ref="postFailMsg" @input="changeValue('postFailMsg',temp.postFailMsg,50)" />

//只能输入50个中文。100个英文。一个英文相当于0.5来算
               changeValue (ref,value,maxnum) {
                    let leng = this.validateTextLength(value);
                    // console.log(leng);
                    if (leng > maxnum) {
                      this.$refs[ref].maxLength = leng;
                      this.$message.closeAll();
                      this.$message.error(this.$t('focusTips.limitInput'))
                    }else{
                      this.$refs[ref].maxLength =100;
                     }
                  },
                  validateTextLength (value) {
                    // 中文、中文标点、全角字符按1。长度,英文、英文符号、数字按0.5长度计算
                    let cnReg = /([\u4e00-\u9fa5]|[\u3000-\u303F]|[\uFF00-\uFF60])/g
                    let mat = value.match(cnReg)
                    let length
                    if (mat) {
                      length = (mat.length + (value.length - mat.length) * 0.5)
                      return length
                    } else {
                      return value.length * 0.5
                    }
                  },

猜你喜欢

转载自blog.csdn.net/qq_33769914/article/details/123903981