限制输入框字符数(中英文区分)

满足复制粘贴时的字符判断,中文输入时判断;

使用: vue、element-ui

<el-input type="textarea" v-model="versionForm.notes" v-on:input="checkLength(versionForm.notes)"></el-input>

限制方法:

// 判断版本更新内容输入字节是否大于500
checkLength(data) {
    const REG_CHINESE = /[\u4e00-\u9fa5]/g;
    if (data) {
        const chineseLength = data.match(REG_CHINESE) ? data.match(REG_CHINESE).length : 0;
        const charLength = data.match(REG_NOT_CHINESE) ? data.match(REG_NOT_CHINESE).length : 0;
        let total = chineseLength * 2 + charLength;
        if (total > 500) {
            let count = 0;
            let copyData = data.slice(0);
            while (total > 500) {
                count ++;
                const isChinese = /[\u4e00-\u9fa5]/.test(copyData.charAt(copyData.length - 1));
                copyData = copyData.slice(0, -1);
                total = isChinese ? total - 2 : total - 1;
             }
            this.$nextTick(() => {
                this.versionForm.notes = data.slice(0, - count);
            })
        }
   }
}

猜你喜欢

转载自www.cnblogs.com/zqqya/p/9257492.html
今日推荐