el-input 只能輸入數字並限制長度


在上一個博客中,有關於限制長度的使用,本文介紹限制只能輸入數字的方法

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 });

 

演示如下:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM