input框常用的正则表达式
1.html
<input @input="onKeyVinInput" type="text" v-model="bindcode" placeholder="请输入" />
2.js
//限制输入框只能输入数字或者字母 onKeyVinInput(e){ const o = e.target; // 数字和字母 const inputRule =/[\W]/g; //修改inputRule 的值 this.$nextTick(function() { this.bindcode = o.value.replace(inputRule , ''); }) }
其它控制
1.只能输入数字 const inputRule = /[^\d]/g 2.只能输入字母 const inputRule = /[^a-zA-Z]/g 3.只能输入数字和字母 const inputRule =/[\W]/g 4.只能输入小写字母 const inputRule =/[^a-z]/g 5.只能输入大写字母 const inputRule =/[^A-Z]/g 6.只能输入数字和字母和下划线 const inputRule =/[^\w_]/g //下划线也可以改成% 7.只能输入中文 const inputRule =/[^\u4E00-\u9FA5]/g 8.只能输入数字和小数点 const inputRule =/[^\d.]/g
其它知识补充
验证手机号是否正确 var phone=15800000000; var strTemp = /^1[3|4|5|6|7|8|9][0-9]{9}$/; if(strTemp.test(phone)){ console.log(“正确”) }else{ uni.showToast({icon:‘none’,title:‘请输入正确的电话’}); return false } 电话号码隐藏中间4位 var phonenumber=15800000000; var reg = /^(\d{3})\d{4}(\d{4})$/; this.phonenum=phonenumber.replace(reg, “$1****$2”)