vue 輸入電話(huà)號(hào)碼自動(dòng)按3-4-4分割功能的實(shí)現(xiàn)代碼
輸入框綁定
<input class="inputBox" type="phone" placeholder=" 請(qǐng)輸入手機(jī)號(hào)" maxlength="13" v-model="phoneNum"/>
監(jiān)聽(tīng)事件,每次號(hào)碼發(fā)生改變時(shí)觸發(fā)
大體的邏輯是:先比較號(hào)碼變化前后的長(zhǎng)度,判斷是輸入還是刪除,如果是輸入的話(huà),利用正則表達(dá)式改變號(hào)碼格式。
watch: { phoneNum (newValue, oldValue) { // 監(jiān)聽(tīng)電話(huà)號(hào)碼 this.phoneNum = newValue.length > oldValue.length ? newValue.replace(/\s/g, '').replace(/(\d{3})(\d{0,4})(\d{0,4})/, '$1 $2 $3') : this.phoneNum.trim() if (this.phoneNum.length === 13) { // 驗(yàn)證/保存的手機(jī)號(hào)碼,去除空格 this.state.checkPhoneNum = this.phoneNum.replace(/\s/g, '') console.log('輸入的電話(huà)號(hào)碼是:', this.state.checkPhoneNum) } } },
效果示意
附錄:下面看下vue手機(jī)號(hào)按344分隔,銀行卡號(hào)每4位空格分隔
實(shí)現(xiàn)效果:
1. 手機(jī)號(hào)輸入/粘貼時(shí),不允許輸入數(shù)字外的其它字符,按344分隔,最大輸入11位數(shù)字
2. 銀行卡號(hào)輸入/粘貼時(shí),不允許輸入數(shù)字外的其它字符,每四位用空格分隔
代碼:
<template> <div class="form"> <p> 手機(jī)號(hào): <input v-model="mobile" type="tel" ref="mobile" maxlength="13" @keyup="inputMobile" @paste="inputMobile" /> </p> <p> 銀行卡號(hào): <input v-model="card" type="text" @keyup="inputCard" @paste="inputCard" /> </p> </div> </template>
js:
<script> export default { data() { return { mobile: '', card: '' } }, methods: { inputMobile() { let value = this.mobile.replace(/\D/g, '').substr(0, 11) // 不允許輸入非數(shù)字字符,超過(guò)11位數(shù)字截取前11位 let len = value.length if (len > 3 && len < 8) { value = value.replace(/^(\d{3})/g, '$1 ') } else if (len >= 8) { value = value.replace(/^(\d{3})(\d{4})/g, '$1 $2 ') } this.mobile = value }, inputCard() { this.card = this.card.replace(/\D/g, '') // 不允許輸入非數(shù)字字符 this.card = this.card.replace(/(\d{4})(?=\d)/g, '$1 ') // 4位一組,非獲取匹配最后一組數(shù)字,避免刪除到空格時(shí)會(huì)馬上自動(dòng)補(bǔ)齊 } } } </script>
上述方案即可實(shí)現(xiàn)基本效果,但如果從中間開(kāi)始刪除或添加內(nèi)容時(shí),光標(biāo)會(huì)自動(dòng)跑到最后,如下:
若想光標(biāo)留在刪除/添加內(nèi)容位置,需要設(shè)置光標(biāo)位置:
修改js如下:
<script> export default { data () { return { mobile: '', card: '' } }, methods: { inputMobile (e) { this.formatMobile(e) this.mobile = this.$refs.mobile.value }, formatMobile (e) { let val = this.$refs.mobile.value // 不可直接用this.mobile,第一方便提取該方法降低代碼耦合度,第二直接用this.mobile,在輸入漢字時(shí)按下shift按鍵會(huì)導(dǎo)致無(wú)法再輸入和刪除內(nèi)容 let selStart = this.$refs.mobile.selectionStart // 選中區(qū)域左邊界位置 let mobileLen = val.length let value = this.getValue(e, val).substr(0, 11) // 獲取輸入/粘貼內(nèi)容,并截取前11位 let len = value.length if (len > 3 && len < 8) { value = value.replace(/^(\d{3})/g, '$1 ') } else if (len >= 8) { value = value.replace(/^(\d{3})(\d{4})/g, '$1 $2 ') } this.$refs.mobile.value = value if (selStart !== mobileLen) { if (selStart === 3) { selStart++ } // 設(shè)置光標(biāo)位置 this.$refs.mobile.selectionStart = this.$refs.mobile.selectionEnd = selStart } }, getValue(e, val) { let value = '' if (e.type === 'keyup') { value = val.replace(/\D/g, '') } else if (e.type === 'paste') { // window.clipboardData:IE瀏覽器獲取剪貼板數(shù)據(jù)對(duì)象 // event.clipboardData:Chrome, Firefox, Safari獲取剪貼板數(shù)據(jù)對(duì)象 let clipboardData = event.clipboardData || window.clipboardData; value = clipboardData.getData('Text'); // 獲取剪貼板text格式的數(shù)據(jù) value = value.replace(/\D/g, '') } return value } } } </script>
未實(shí)現(xiàn):
不允許粘貼非數(shù)字內(nèi)容到輸入框還未實(shí)現(xiàn),改為了提交時(shí)校驗(yàn),如果有比較好的解決方案望大家提出
到此這篇關(guān)于vue 輸入電話(huà)號(hào)碼自動(dòng)按3-4-4分割功能的實(shí)現(xiàn)代碼的文章就介紹到這了,更多相關(guān)vue輸入電話(huà)號(hào)碼自動(dòng)分割內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue-cli+webpack在生成的項(xiàng)目中使用bootstrap實(shí)例代碼
本篇文章主要介紹了vue-cli+webpack在生成的項(xiàng)目中使用bootstrap實(shí)例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-05-05基于Vue+ELement搭建動(dòng)態(tài)樹(shù)與數(shù)據(jù)表格實(shí)現(xiàn)分頁(yè)模糊查詢(xún)實(shí)戰(zhàn)全過(guò)程
這篇文章主要給大家介紹了關(guān)于如何基于Vue+ELement搭建動(dòng)態(tài)樹(shù)與數(shù)據(jù)表格實(shí)現(xiàn)分頁(yè)模糊查詢(xún)的相關(guān)資料,Vue Element UI提供了el-pagination組件來(lái)實(shí)現(xiàn)表格數(shù)據(jù)的分頁(yè)功能,需要的朋友可以參考下2023-10-10vue+elementUI動(dòng)態(tài)生成面包屑導(dǎo)航教程
今天小編就為大家分享一篇vue+elementUI動(dòng)態(tài)生成面包屑導(dǎo)航教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11vue使用iframe嵌入網(wǎng)頁(yè)的示例代碼
本篇文章主要介紹了vue使用iframe嵌入網(wǎng)頁(yè)的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03Vue中使用ECharts與v-if的問(wèn)題和解決方案
在Vue項(xiàng)目中使用v-if指令控制ECharts圖表顯示時(shí),可能會(huì)遇到圖表無(wú)法正常渲染或顯示錯(cuò)誤的問(wèn)題,下面這篇文章主要介紹了Vue中使用ECharts與v-if的問(wèn)題和解決方案,需要的朋友可以參考下2024-10-10