Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

203
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!