2023-04-14 手机号码间隔

发布时间 2023-04-14 20:26:30作者: 哎呦你可棒棒了

前言:uniapp input之输入手机号需要进行间隔显示。即18800188001显示成188 0018 8001。

部分代码如下:

// 间隔函数
formatPhone(val) {
if (val) { if (this.inputPhoneValue.replace(/\s/g, "").length == 11 || this.inputPhoneValue.replace(/\s/g, "").length > 11) { console.log(val+" == 进来了: "+this.inputPhoneValue.replace(/\s/g, "").length) let a = val.substring(0,12); return a; } const matches = /^(\d{3})(\d{4})(\d{4})$/.exec(val); if (matches) { return matches[1] + " " + matches[2] + " " + matches[3]; } } return val; }, // 监听输入手机号 onInputPhone(e) { this.inputPhoneValue = this.formatPhone(e.detail.value); // 有值时显示清除按钮 },