在上一個部落格中,有關于限制長度的使用,本文介紹限制隻能輸入數字的方法
el-input 代碼如下:
<el-form-item label="賬号" required>
<el-input v-model="form.tele" style="width:160px;" oninput="if(value.length>11)value=value.slice(0,11)" placeholder="請輸入賬号" type="number"></el-input>
</el-form-item>
在main.js中加入如下代碼:
1 Vue.directive('enterNumber', {
2 inserted: function (el) {
3 el.addEventListener("keypress",function(e){
4 e = e || window.event;
5 let charcode = typeof e.charCode === 'number' ? e.charCode : e.keyCode;
6 let re = /\d/;
7 if(!re.test(String.fromCharCode(charcode)) && charcode > 9 && !e.ctrlKey){
8 if(e.preventDefault){
9 e.preventDefault();
10 }else{
11 e.returnValue = false;
12 }
13 }
14 });
15 }
16 });
示範如下:
