在标签上可以直接进行校验如下,如果只调数字键盘type=number不好用可以用type=tel
<input name="creditDate" οninput="value = value.replace(/[^\d{2}\/{1}\d{2}$]/ig,'')" @keyup="changeVal" v-model="getDate" type="text" placeholder="月/年">
changeVal(e){
let $val = e.currentTarget.value;
e.currentTarget.value = $val.replace(/[^\d{2}\/{1}\d{2}$]/ig,'')
let inputVal = e.currentTarget.value
this.all = true;
if(/^[2-9]/ig.test(inputVal)){
e.currentTarget.value = '0'+inputVal
this.all1 = true;
}
if(/^[1]/ig.test(inputVal)){
e.currentTarget.value = e.currentTarget.value.replace(/^1[^0-2|/]/ig,'1')
this.all1 = true;
}
if(/^[0]/ig.test(inputVal)){
e.currentTarget.value = e.currentTarget.value.replace(/^0[^1-9|/]/ig,'0')
this.all1 = true;
}
if(e.currentTarget.value.length == 2 && e.keyCode !=8 && !/^1\//ig.test(e.currentTarget.value)){
e.currentTarget.value = e.currentTarget.value + '/'
this.all2 = true;
}
if(e.currentTarget.value.length >= 5){
e.currentTarget.value = e.currentTarget.value.substr(0,5)
this.all3 = true;
}
e.currentTarget.value = e.currentTarget.value.replace(/[^\d{2}\/{1}\d{2}$]/ig,'')
this.getDate = e.currentTarget.value
if(this.all && this.all1 && this.all2 && this.all3){
this.submitBtnFrag();
}
}