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

108
Visualizações
What's the best way to code an input tag with certain locked characters?

Introduction

I would like to make an input of a fixed length that you can only fill some of the characters, for instance let's say I have __llo w_rld! and i want the user to fill in the gaps, but not allow to modify the prefilled characters.

Ideas

I thought of using an input tag for each character and mark as disabled the prefilled ones, here is an example:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Example</title>
</head>
<body>
  <input size="1"></input>
  <input size="1"></input>
  <input size="1" value="l" disabled></input>
  <input size="1" value="l" disabled></input>
  <input size="1" value="o" disabled></input>
  &ensp;
  <input size="1" value="w" disabled></input>
  <input size="1"></input>
  <input size="1" value="r" disabled></input>
  <input size="1" value="l" disabled></input>
  <input size="1" value="d" disabled></input>
  <input size="1" value="!" disabled></input>
</body>
</html>

However this approach doesn't allow the user to keep typing characters and jump from one input to the next one.

Is there any way of accomplishing this?

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

0

Using required and querySelector(':invalid') seems to work pretty well.

By the way, it's not valid html to end <input> tags.

const container = document.getElementById("container");
container.addEventListener("input", ev => {
  container.querySelector("input:invalid")?.focus();
});
input {
  width: 1em;
}
<div id="container">
  <input required maxlength="1">
  <input required maxlength="1">
  <input value="l" disabled>
  <input value="l" disabled>
  <input value="o" disabled>
  &ensp;
  <input value="w" disabled>
  <input required maxlength="1">
  <input value="r" disabled>
  <input value="l" disabled>
  <input value="d" disabled>
  <input value="!" disabled>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

If I understand right, the one character long text boxes are a fallback not what you actually want? If so you can use a regex to reject anything that doesn't match. To do so you use a . for every character you are leaving blank and the character for each one you want to be fixed. The regex for your hello world example is /..llo w.rld!/ and you revert the field value to the last version that passed this test every time it fails. Technically they can edit those characters, but the code will change them back immediately after:

let lastValue = '__llo w_rld!';
let currentValue = '__llo w_rld!';
const regex = new RegExp('..llo w.rld!');
testTextID.value = lastValue;
    const strLength = 12;

function overwriteTestText(e){
    let caretpos = testTextID.selectionStart;
    currentValue = testTextID.value;
    if (currentValue.length == strLength + 1){
        currentValue = removeCharacter(currentValue, caretpos);
    }

    testText(e, false);
   
    //selecting 0 characters long puts the caret at that position.
    testTextID.setSelectionRange(caretpos, caretpos);
}

function testText(e, readInput = true){
    if(readInput) {
        currentValue = testTextID.value;
    }

    if (regex.test(currentValue) && currentValue.length == strLength) {
        lastValue = currentValue;
    }

    testTextID.value = lastValue;
}

function removeCharacter(str, pos) {
    return str.slice(0,pos) + str.slice(pos+1);
}
  <input id="testTextID" onkeyup="overwriteTestText()"  onblur="testText()" />

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