欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue 輸入電話(huà)號(hào)碼自動(dòng)按3-4-4分割功能的實(shí)現(xiàn)代碼

 更新時(shí)間:2020年04月30日 17:16:02   作者:吃茶去吖  
這篇文章主要介紹了vue 輸入電話(huà)號(hào)碼自動(dòng)按3-4-4分割功能的實(shí)現(xiàn)代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

輸入框綁定

<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)文章

最新評(píng)論