input输入框限制输入条件(正整数/大写字母/小写字母/中文)

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

猜你喜欢

转载自blog.csdn.net/weixin_44805839/article/details/132838378
今日推荐