vue 输入手机号格式化为344格式

2,161 阅读1分钟
<template>
    <input v-model="phone" placeholder="手机号" />
</template>

<script>
export default {
    watch: {
        phone(newValue, oldValue) {
            // 监听
            this.phone =
                newValue.length > oldValue.length
                    ? newValue
                          .replace(/\s/g, '')
                          .replace(/(\d{3})(\d{0,4})(\d{0,4})/, '$1 $2 $3')
                    : this.phone.trim();
        }
    }
};
</script>