<input type="text" v-model="phone" maxlength='11' id="input">
maxlength表示最多只能输入11个字符
<script>
var input = document.getElementById('input')
input.onkeyup = function () {
input.value = input.value.replace(/[^\d]/g, "") //只能是数字
if(input.value[0]!=1){ //第一个只能是1
this.phone=''
}else if(input.value[1]==0||input.value[1]==1||input.value[1]==2){ //第二个不能是0 1 2
this.phone = this.phone[0]
}
}
</script>
只允许输入数字(整数:小数点不能输入)
<input type="text" οnkeyup="value=value.replace(/[^\d]/g,'')" >
允许输入小数(两位小数)
<input type="text" οnkeyup="value=value.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')" >
允许输入小数(一位小数)
<input type="text" οnkeyup="value=value.replace(/^\D*(\d*(?:\.\d{0,1})?).*$/g, '$1')" >
开头不能为0,且不能输入小数
<input type="text" οnkeyup="value=value.replace(/[^\d]/g,'').replace(/^0{1,}/g,'')" >