Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

390
Vistas
How to insert hypen exactly after 3 & 2 places after respectively in a string?

I am creating like a template which accepts user input and hide it with *. I just need to insert hyphens in between like a SSN number. I am almost done just the places are not proper.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Vue</title>
  </head>
  <body>
    <div id="app">
      <input type="text" @input="onInput" v-model="someData" maxlength="11" />
    </div>
    <script src="https://unpkg.com/vue"></script>
    <script src="https://cdn.jsdelivr.net/npm/v-mask/dist/v-mask.min.js"></script>
    <script>
      Vue.filter("uppercase", function (value) {
        const ssnValue = value.substr(0,3) + '-' + value.substr(3, 6) + '-' + value.substr(6);
        console.log('fil',ssnValue)
        return ssnValue.replace(/\d/g, '*')
      });
      const app = new Vue({
        el: "#app",
        data() {
          return {
            someData: "",
            maskedData: "",
          };
        },
        computed: {
          getMaskedData() {
            return this.maskedData;
          },
        },
        methods: {
          onInput(e) {
            let originalString = e.data
            if(!!originalString)
            this.maskedData += originalString;
            console.log('mask', this.getMaskedData)
            this.someData = this.$options.filters.uppercase(e.target.value);
          },
        },
      });
    </script>
  </body>
</html>

I referred Insert hyphens in javascript and put dash after every n character during input from keyboard but i am unclear how can i implement those in my case.

When user is entering 123456789, I want like ***-**-****. (Please don't recommend using input type password or changing font family)

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

If I understand you correctly try following snippet:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Vue</title>
  </head>
  <body>
    <div id="app">
      <input type="text" @input="onInput" v-model="someData" maxlength="11" />
    </div>
    <script src="https://unpkg.com/vue"></script>
    <script src="https://cdn.jsdelivr.net/npm/v-mask/dist/v-mask.min.js"></script>
    <script>
      Vue.filter("uppercase", function (value) {
        let ssnValue =value
        if(value.length === 3 || value.length === 6) {
          ssnValue = value + '-'
        }
        console.log('fil',ssnValue)
        return ssnValue.replace(/\d/g, '*')
      });
      const app = new Vue({
        el: "#app",
        data() {
          return {
            someData: "",
            maskedData: "",
          };
        },
        computed: {
          getMaskedData() {
            return this.maskedData;
          },
        },
        methods: {
          onInput(e) {
            let originalString = e.data
            if(!!originalString)
            this.maskedData += originalString;
            console.log('mask', e.data)
            if(e.data) this.someData = this.$options.filters.uppercase(e.target.value);
          },
        },
      });
    </script>
  </body>
</html>

about 4 years ago · Juan Pablo Isaza Denunciar

0

I saw one of the links that you put in your question there is a format function which you can use:

function format(input, format, sep) {
  input = input.replaceAll("-", "");
  var output = "";
  var idx = 0;
  for (var i = 0; i < format.length && idx < input.length; i++) {
    output += input.substr(idx, format[i]);
    if (idx + format[i] < input.length) output += sep;
    idx += format[i];
  }
  output += input.substr(idx);
  return output;
}

Vue.filter("uppercase", function (value) {
  let ssnValue = format(value, [3, 3, value.length - 6], "-");
  console.log("fil", ssnValue);
  return ssnValue.replace(/\d/g, "*");
});
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda