1. Setting method on the page (taking restricting the input of positive integers as an example):
changeInput(name) {
if (this[name] <= 0) {
this[name] = '';
} else {
this[name] = this[name].replace(/\D/g, '')
}
},
2.Input tag for binding:
v-model="addFoodA" @input="changeInput('addFoodA')"
tips: Pass the bound parameter string in the method
That's it
3. Attached are regular expressions that restrict other conditions.
Only numbers can be entered
const inputType = /[^\d]/g
Only numbers and decimal points can be entered
const inputType =/[^\d.]/g
Only letters can be entered
const inputType = /[^a-zA-Z]/g
Only numbers and letters can be entered
const inputType =/[\W]/g
Only lowercase letters can be entered
const inputType =/[^az]/g
Only uppercase letters can be entered
const inputType =/[^AZ]/g
Only numbers, letters and underscores can be entered
const inputType =/[^\w_]/g //Underscores can also be changed to %
Only Chinese can be input
const inputType =/[^\u4E00-\u9FA5]/g