[Vuejs]-How to format phone number with v-model on VueJs

0๐Ÿ‘

 const q = (ph) => {
  const batches = [3, 3, 2, 2];
  const template = '({0}) - {1} - {2} - {3}';
  let index = 0, summator = 0;
  let data = template;
  batches.forEach(x => {
    const part = ph.substring(summator, x + summator);
    data = data.replace(`{${index}}`, part);
    index++;
    summator += x;
  });
  return data;
}

and then include it in your data:

abouts:[{phone:q('1234567890'),mail:''},{phone:'',mail:q('')}]

simple way, working. You can add trimming if phone number smaller than 10 digits

0๐Ÿ‘

I solved the problem. If you have the same problem you can use vue-mask package.

Leave a comment