``` // 手機號分隔顯示 let tel = this.data.tel_value // 原始手機號 let len = tel_value.length // 原始手機號的長度 let mobile = '' for (var i = 0; i ...
// 手機號分隔顯示
let tel = this.data.tel_value // 原始手機號
let len = tel_value.length // 原始手機號的長度
let mobile = ''
for (var i = 0; i < len; i++) {
if (i == 2 || i == 6) {
mobile = mobile + tel.charAt(i) + " ";
}
else {
mobile = mobile + tel.charAt(i);
}
}
正則
<script> var str = '13212345678'; var newstr = ''; newstr = str.replace(/\s/g, '').replace(/(^\d{3})(?=\d)/g, "$1 ").replace(/(\d{4})(?=\d)/g, "$1 "); console.log(newstr); </script>
手機號格式校驗
let reg = /^[1][1,3,4,5,6,7,8,9][0-9]{9}$/ //正則驗證手機號