<input
v-model="phone"
placeholder="请输入手机号码"
type="text"
ref="cardInput"
maxlength="13"
@input="formatCardNumber"
id="bankAccountNo"
/>
formatCardNumber(){
let that = this
let elem = document.getElementById('bankAccountNo');
let patten = /\D/g
let curpos = elem.selectionStart;
let old_value = elem.value;
that.phone =
this.paddingSpace(elem.value.replace(patten, ""));
if (old_value[curpos - 1] == " ") {
curpos += -1;
} else {
var old_cursor = old_value.slice(0, curpos);
var old_cursor_spaced = this.paddingSpace(
old_cursor.replace(patten, "")
);
curpos += old_cursor_spaced.length - old_cursor.length;
}
setTimeout(function () {
elem.focus();
elem.setSelectionRange(curpos, curpos);
}, 0);
},
// 格式化空格
paddingSpace(str) {
// return str.replace(/\s/,'').replace(/(^\d{3})(?=\d)/g,"$1 ").replace(/(\d{4})(?=\d)/g,"$1 ") //手机号码、、
//银行卡号
return str.replace(/\s/,'').replace(/(\d{4})(?=\d)/g,"$1 ");
},
银行卡手机号 每个四位一个空格
最新推荐文章于 2024-08-26 16:38:21 发布