<template>
<el-input
v-model="inputValue"
@input="handleInput"
@compositionstart="isComposing = true"
@compositionend="isComposing = false"
placeholder="请输入内容"
></el-input>
</template>
<script>
export default {
data() {
return {
inputValue: '',
isComposing: false, // 标记是否处于输入法编辑状态
};
},
methods: {
handleInput() {
if (!this.isComposing) {
// 使用正则表达式检查输入是否只包含中文、英文和数字
const regex = /^[\u4e00-\u9fa5a-zA-Z0-9]*$/;
this.inputValue = this.inputValue.replace(/[^\u4e00-\u9fa5a-zA-Z0-9]/g, '');
}
},
},
};
</script>
这里compositionstart表明中文输入开始,compositionend表明中文输入结束,isComposing标志正在输入,从而控制输入框的校验在输入拼音时不进行,不在中文输入状态下才校验。