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

206
Visualizações
Javascript - How to search for numbers greater than x in regex

I am trying to not to include numbers greater than 26 in my input. So if a user input 27 above in the input tag, it will not show on the result. How can i do it? Here is my code thank you!

Please separate the numbers using space, for example:
<br> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 <br> Output: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z <br><br><br>
<input type="text" id="inputNum"><br>
<button onclick="convert()">Convert to Letters</button>
<br>
<p id="result"></p>

<script>
  function convert() {
    let numbers = document.getElementById("inputNum").value;

    let outputVal = numbers.replace("10", "J").replace("11", "K").replace(/12/gi, "L").replace("13", "M").replace("14", "N").replace("15", "O").replace("16", "P").replace("17", "Q").replace("18", "R").replace("19", "S").replace("20", "T").replace("21", "U").replace("22", "V").replace("23", "W").replace("24", "X").replace("25", "Y").replace("26", "Z").replace(/1/gi, "A").replace(/2/gi, "B").replace("3", "C").replace("4", "D").replace("5", "E").replace("6", "F").replace("7", "G").replace("8", "H").replace("9", "I");

    document.getElementById("result").innerHTML = outputVal;
  }
</script>

about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

Just a basic validation can work:

function convert() {
  let numbers = document.getElementById("inputNum").value;
  if(isNaN(numbers) || parseInt(numbers) > 26){
      return;
  }

  let outputVal = numbers.replace("10", "J").replace("11", "K").replace(/12/gi, "L").replace("13", "M").replace("14", "N").replace("15", "O").replace("16", "P").replace("17", "Q").replace("18", "R").replace("19", "S").replace("20", "T").replace("21", "U").replace("22", "V").replace("23", "W").replace("24", "X").replace("25", "Y").replace("26", "Z").replace(/1/gi, "A").replace(/2/gi, "B").replace("3", "C").replace("4", "D").replace("5", "E").replace("6", "F").replace("7", "G").replace("8", "H").replace("9", "I");

  document.getElementById("result").innerHTML = outputVal;
}
about 4 years ago · Santiago Gelvez Relatório

0

I'd suggest simplifying the logic using String.split(), and Array.reduce().

We can check each number to ensure it's in range in the .reduce() call (>=0 and <= 26).

You can then use String.fromCharCode() to convert each number to a letter:

function convert() {
    let numbers = document.getElementById("inputNum").value.trim();
    let outputVal = numbers.split(/\s+/).reduce((acc, n) => {
        return acc + (((n >= 0) && (n <= 26)) ? String.fromCharCode(+n + 64) + ' ': '');
    }, '');
    document.getElementById("result").innerHTML = outputVal;
}
Please separate the numbers using space, for example:

<br> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 <br> Output: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z <br><br><br>
<input type="text" id="inputNum"><br>
<button onclick="convert()">Convert to Letters</button>
<br>
<p id="result"></p>

about 4 years ago · Santiago Gelvez 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