input框限制输入内容

发布时间 2023-07-03 12:06:12作者: 你笑的好瓜
			<input v-model="request.idCardNo" maxlength="18" @input="typeInput($event)" placeholder="请输入您的身份证号">

对应在methods中的方法

			typeInput(event) {
				// 只能输入数字和字母的验证;
				const inputType = /[\W]/g //想限制什么类型在这里换换正则就可以了
				this.$nextTick(function() {
					//这里的 this.request.idCardNo 是input框 v-model 绑定的值
					this.request.idCardNo = event.target.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