0

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>
Fabian S.
  • 2,441
  • 2
  • 24
  • 34
  • Theres a similar question (asking about greater than 49): https://stackoverflow.com/questions/8592488/regex-how-can-i-match-all-numbers-greater-than-49. Maybe it contains an answer that helps? – Fabian S. Dec 10 '21 at 08:12

2 Answers2

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;
}
Arif Khan
  • 508
  • 5
  • 12
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>
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40