表单输入框中只允许输入数值
方案:使用正则替换
//template模板 vue3
<a-input
v-model:value.trim="btnForm.showOrder"
@change="onChangeInput"
maxlength="10"
placeholder="请输入排序"
allow-clear
></a-input>
-----------------------------------------------------------
// 在 Input 值改变时触发只能输入数字
const onChangeInput = (e) => {
let value = e.target.value.replace(/[^\d]/g, ''); // 只能输入数字
value = value.replace(/^0+(\d)/, '$1'); // 第一位0开头,0后面为数字,则过滤掉,取后面的数字
btnForm.value.showOrder = value;
};
表单输入框中不允许输入空格vue2
因为有很多地方需要限制所以方法挂载到全局
空格键的keyCode为32
//这是项目中全局公共函数utils文件中
// 禁用input空格
Vue.prototype.keydown = e => {
if (e.keyCode == 32) { //
e.returnValue = false; //禁用空格的意思
}
};
//template中需要用的表单
<el-input
ref="isbnInputRef"
v-model="collectionForm.isbn"
clearable
@keydown.native="keydown($event)"
>