Here's an example of how to format a phone number using Vue.js:
<template>
<div>
<input type="text" v-model="phoneNumber" @input="formatPhoneNumber">
</div>
</template>
<script>
export default {
data() {
return {
phoneNumber: ''
}
},
methods: {
formatPhoneNumber() {
// Remove all non-numeric characters
let cleanedNumber = this.phoneNumber.replace(/\D/g, '');
// Format the number with dashes
let formattedNumber = cleanedNumber.slice(0, 3) + '-' + cleanedNumber.slice(3, 6) + '-' + cleanedNumber.slice(6);
// Set the formatted number back to the model
this.phoneNumber = formattedNumber;
}
}
}
</script>
This example listens for input changes on an input field with v-model
and uses the formatPhoneNumber
method to remove non-numeric characters and format the number with dashes. The method then sets the formatted number back to the model.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2021-08-14 11:00:00 +0000
Seen: 11 times
Last updated: May 30 '21