业务需求,input只能输入数字和字母,不允许输入中文。
使用的是uniapp进行开发,采用Wot Design Uni组件,业务代码如下:
<wd-input v-model="invitationCode" placeholder="请输入邀请码" @input="changeCode" @focus="handleFocus" @blur="handleBlur" />
js:
changeCode(e) {
const inputType = /[\W]/g
//要写nextTick 不然无效
this.$nextTick(() => {
this.invitationCode = e.value.replace(inputType, '');
})
},
其他类型:
只能输入数字
const inputType = /[^\d]/g
只能输入字母
const inputType = /[^a-zA-Z]/g
只能输入数字和字母
const inputType =/[\W]/g
只能输入小写字母
const inputType =/[^a-z]/g
只能输入大写字母
const inputType =/[^A-Z]/g
只能输入数字和字母和下划线
const inputType =/[^\w_]/g //下划线也可以改成%
只能输入中文
const inputType =/[^\u4E00-\u9FA5]/g
只能输入数字和小数点
const inputType =/[^\d.]/g