满足复制粘贴时的字符判断,中文输入时判断;
使用: 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); }) } } }