1. 在页面设置方法(以限制输入正整数为例):
changeInput(name) {
if (this[name] <= 0) {
this[name] = '';
} else {
this[name] = this[name].replace(/\D/g, '')
}
},
2.input标签进行绑定:
v-model="addFoodA" @input="changeInput('addFoodA')"
tips: 方法里传递绑定的参数字符串
这样就完成啦
3.另附限制其他条件的正则表达式
只能输入数字
const inputType = /[^\d]/g
只能输入数字和小数点
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