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

288
Visualizações
How to replace the previous user input with new input when max length is reached?

I have an input which max length = 3

#username {
  width: 30px;
}
  <input type="text" id="username" name="username" maxlength="3">

Is there a way to automatically replace the oldest user input with the latest user input when max length is reached?

E.g. user types 'a', 'b', 'c', 'abc' is showing in the input. If user types 'd', 'bcd' should be shown in the input.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can get the last 3 characters of the input using String.prototype.substr(), then replace the input with the last 3 characters when someone inputs a value.

document.getElementById('username').addEventListener('input', function() {
  this.value = this.value.substr(-3);
});
<input type="text" id="username" name="username">

You will have to remove the maxlength="3", because otherwise people don't have the chance to input the 4th character used to rotate the string.

about 4 years ago · Juan Pablo Isaza Relatório
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