vue中Element-ui 輸入銀行賬號每四位加一個(gè)空格的實(shí)現(xiàn)代碼
一、問題描述:
我們在輸入銀行賬號會設(shè)置每四位添加一個(gè)空格,輸入金額,每三位添加一個(gè)空格。那么,在vue,element-ui 組件中,如何實(shí)現(xiàn)呢?
二、效果圖:
三、實(shí)現(xiàn)代碼:
<el-table-column prop="account" label="銀行賬號"> <template slot-scope="scope"> <el-input type="text" maxlength="23" v-model="scope.row.account" placeholder="請輸入銀行賬號" @change="validateNum(scope.$index)"></el-input> </template> </el-table-column> // 輸入銀行卡號 validateNum (index) { this.setNum(this.supplierObjs.supplierBankAccount, index) }, // 設(shè)置銀行卡號,每四位添加一個(gè)空格 setNum (data, index) { data.forEach((element, i) => { element.account = element.account.replace(/\s/g, '').replace(/[^\d]/g, '').replace(/(\d{4})(?=\d)/g, '$1 ') this.$set(element, 'account', element.account) }) },
四、思路:
1、在組件的change事件中添加實(shí)現(xiàn)方法(因?yàn)槲覀兊臉I(yè)務(wù)需求是可以添加多個(gè)銀行卡號,所以用index做了區(qū)分),取出每行的值;
2、用element.account = element.account.replace(/\s/g, '').replace(/[^\d]/g, '').replace(/(\d{4})(?=\d)/g, '$1 ')
,類似正則表達(dá)式的方法對數(shù)據(jù)進(jìn)行處理;
3、this.$set(element, 'account', element.account)
,set方法,將處理后的值set到model中,處理后的數(shù)據(jù)就保存到model中了。
實(shí)現(xiàn)起來也非常簡單,但是因?yàn)閑lement-UI沒有提供輸入設(shè)置的方法,所以需要自己根據(jù)需求完善一下。
補(bǔ)充:下面看下js 填寫銀行卡號時(shí),每4個(gè)數(shù)字用空格隔開
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>Document</title> <script src="js/jQuery.js"></script> </head> <body> <script> var num = 0; function inputAccount(){ var str = $('#bankCard').val(); var elem = document.getElementById("bankCard"); console.log(elem); if(str.length > num){ var c = str.replace(/\s/g, ""); if(str != "" && c.length > 4 && c.length % 4 == 1){ $('#bankCard').val(str.substring(0, str.length - 1)+ " " + str.substring(str.length - 1, str.length)); } } if(elem.setSelectionRange){//W3C setTimeout(function(){ elem.setSelectionRange(elem.value.length,elem.value.length); elem.focus(); },0); }else if(elem.createTextRange){//IE var textRange=elem.createTextRange(); textRange.moveStart("character",elem.value.length); textRange.moveEnd("character",0); textRange.select(); } num = str.length; } </script> <input type="text" name="" oninput="inputAccount()" id="bankCard" /> </body>
</html> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>Document</title> <script src="js/jQuery.js"></script> </head> <body> <input type="text" name="" id="box" /> <script> $(function(){ $('#box').keyup(function(){ var value=$(this).val().replace(/\s/g,'').replace(/(\d{4})(?=\d)/g,"$1 "); $(this).val(value) }) }) </script> </body> </html>
總結(jié)
以上所述是小編給大家介紹的vue中Element-ui 輸入銀行賬號每四位加一個(gè)空格的實(shí)現(xiàn)代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- vue+element-ui中form輸入框無法輸入問題的解決方法
- 詳解Vue3.0中ElementPlus<input輸入框自動獲取焦點(diǎn)>
- element input輸入框自動獲取焦點(diǎn)的實(shí)現(xiàn)
- Vue ElementUI實(shí)現(xiàn):限制輸入框只能輸入正整數(shù)的問題
- Element Input輸入框的使用方法
- vue element-ui實(shí)現(xiàn)input輸入框金額數(shù)字添加千分位
- Vue?elementUI表單嵌套表格并對每行進(jìn)行校驗(yàn)詳解
- element表單驗(yàn)證如何清除校驗(yàn)提示語
- Vue Element校驗(yàn)validate的實(shí)例
- element必填校驗(yàn)輸入空格問題修改正則表達(dá)式、請求攔截器實(shí)現(xiàn)所有輸入框去除首尾空格(推薦)
相關(guān)文章
解決vue與node模版引擎的渲染標(biāo)記{{}}(雙花括號)沖突問題
這篇文章主要介紹了解決vue與node模版引擎的渲染標(biāo)記{{}}(雙花括號)沖突問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09vue項(xiàng)目怎樣用nginx反向代理WebSocket請求地址
這篇文章主要介紹了vue項(xiàng)目怎樣用nginx反向代理WebSocket請求地址問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09vue+js點(diǎn)擊箭頭實(shí)現(xiàn)圖片切換
這篇文章主要為大家詳細(xì)介紹了vue+js點(diǎn)擊箭頭實(shí)現(xiàn)圖片切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06Vuejs第十一篇組件之slot內(nèi)容分發(fā)實(shí)例詳解
這篇文章主要介紹了Vuejs第十一篇之slot內(nèi)容分發(fā)組件詳解的相關(guān)資料2016-09-09vue 數(shù)據(jù)遍歷篩選 過濾 排序的應(yīng)用操作
這篇文章主要介紹了vue 數(shù)據(jù)遍歷篩選 過濾 排序的應(yīng)用操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11