Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

274
Visualizações
how to delete if the text input contain hyphens?

I was just trying to make a snippet that allows user to enter their ssn but also hides it with asterisk (*). I followed SSN masking numbers to a special character (*) in input on keyup answer and i tried to add hyphen (-) between input for formatting. But when I am pressing backspace, its not deleting the character.

<!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>Document</title>
  </head>
  <body>
    <div>
      <input id="password" type="text" onkeyup="changeValue(event)" maxlength="11"/>
      <input id="hiddenPass" type="hidden" />
    </div>
    <script>
      var arr = [];
      function changeValue(event) {
        var element = document.getElementById("password");
        var hiddenElement = document.getElementById("hiddenPass");
        var startPos = element.selectionStart;
        var endPos = element.selectionEnd;
        var isTrue = /[0-9]/.test(element.value);
        // if [0-9] (for number)
        if (isTrue || event.keyCode == 8) {
          // for deletion
          if (event.keyCode == 8) {
            arr.splice(endPos, 1);
            setStar(arr);
          } else {
            var val = element.value;
            var obj = { key: val[startPos - 1], value: "*" };
            // perticular position insertion
            arr.splice(startPos - 1, endPos - startPos, obj);
            setStar(arr);
          }
        } else {
          // for space controlling
          element.value = element.value.trim();
          return;
        }
      }

      // star setting
      function setStar(arr) {
        var element = document.getElementById("password");
        var hiddenElement = document.getElementById("hiddenPass");
        var starText = "";
        var originalText = "";
        for (var i = 0; i < arr.length; i++) {
          if (i === 3 || i === 5) {
            starText +=  "-"
          }
          starText += arr[i].value
          originalText += arr[i].key;
        }
        element.value = starText;
        hiddenElement.value = originalText;
        console.log("HiddenElement", hiddenElement.value);
      }
    </script>
  </body>
</html>

how can I achieve that?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda