溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何實現(xiàn)vue輸入電話號碼自動按3-4-4分割功能

發(fā)布時間:2020-07-29 10:30:02 來源:億速云 閱讀:464 作者:小豬 欄目:web開發(fā)

這篇文章主要講解了如何實現(xiàn)vue輸入電話號碼自動按3-4-4分割功能,內(nèi)容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。

輸入框綁定

<input class="inputBox" type="phone" 
placeholder=" 請輸入手機號" 
maxlength="13" v-model="phoneNum"/>

監(jiān)聽事件,每次號碼發(fā)生改變時觸發(fā)
大體的邏輯是:先比較號碼變化前后的長度,判斷是輸入還是刪除,如果是輸入的話,利用正則表達式改變號碼格式。

watch: {
 phoneNum (newValue, oldValue) { // 監(jiān)聽電話號碼
 this.phoneNum = newValue.length > oldValue.length &#63; newValue.replace(/\s/g, '').replace(/(\d{3})(\d{0,4})(\d{0,4})/, '$1 $2 $3') : this.phoneNum.trim()
 if (this.phoneNum.length === 13) {
 // 驗證/保存的手機號碼,去除空格
 this.state.checkPhoneNum = this.phoneNum.replace(/\s/g, '')
 console.log('輸入的電話號碼是:', this.state.checkPhoneNum)
 } 
 }
 },

效果示意

如何實現(xiàn)vue輸入電話號碼自動按3-4-4分割功能

附錄:下面看下vue手機號按344分隔,銀行卡號每4位空格分隔

實現(xiàn)效果:

1. 手機號輸入/粘貼時,不允許輸入數(shù)字外的其它字符,按344分隔,最大輸入11位數(shù)字

2. 銀行卡號輸入/粘貼時,不允許輸入數(shù)字外的其它字符,每四位用空格分隔

如何實現(xiàn)vue輸入電話號碼自動按3-4-4分割功能

代碼:

<template>
 <div class="form">
 <p>
 手機號:
 <input v-model="mobile" type="tel" ref="mobile" maxlength="13" @keyup="inputMobile" @paste="inputMobile" />
 </p>
 <p>
 銀行卡號:
 <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ù)字字符,超過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})(&#63;=\d)/g, '$1 ') // 4位一組,非獲取匹配最后一組數(shù)字,避免刪除到空格時會馬上自動補齊
 }
 }
 } 
</script>

上述方案即可實現(xiàn)基本效果,但如果從中間開始刪除或添加內(nèi)容時,光標會自動跑到最后,如下:

如何實現(xiàn)vue輸入電話號碼自動按3-4-4分割功能

若想光標留在刪除/添加內(nèi)容位置,需要設(shè)置光標位置:

如何實現(xiàn)vue輸入電話號碼自動按3-4-4分割功能

修改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,在輸入漢字時按下shift按鍵會導致無法再輸入和刪除內(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è)置光標位置
  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ù)對象
  // event.clipboardData:Chrome, Firefox, Safari獲取剪貼板數(shù)據(jù)對象
  let clipboardData = event.clipboardData || window.clipboardData;
  value = clipboardData.getData('Text'); // 獲取剪貼板text格式的數(shù)據(jù)
  value = value.replace(/\D/g, '')
 }
 return value
 }
 }
 }
</script>

看完上述內(nèi)容,是不是對如何實現(xiàn)vue輸入電話號碼自動按3-4-4分割功能有進一步的了解,如果還想學習更多內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI