入力ボックスの文字数を制限(中国語と英語の区別)

1972 ワード

コピー貼り付け時の文字判断、中国語入力時の判断を満たす.
使用シヨウ:vue、element-ui

制限方法:
//                 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);
            })
        }
   }
}

 
転載先:https://www.cnblogs.com/zqqya/p/9257492.html