-3

I currently have a text input in my HTML document and some buttons below it. When pressed, these will run a function input(some number). I have added a feature to the function that prevents the length of the string for the input from being above 5. However, sometimes the string (currentAnswer) will be set to something like -768.. When this happens, the function will not let you enter any more numbers, when instead you should be able to enter 2 more digits. So how would I find the length of just the numbers in a string? Here is my code:

function input(num) {
currentAnswer = document.getElementById("answerBox").value;
answerLength = currentAnswer.length;

if (answerLength < 5) {

document.getElementById("answerBox").value = currentAnswer + num;
answerLength = answerLength + 1;

}}

As you can see, I am using currentAnswer.length to get the length of the string. But I only want the amount of digits. Can someone please help me?

BlueWater
  • 48
  • 10

1 Answers1

-2

Count the number of integers in a string

alert("g66ghy7".replace(/[^0-9]/g,"").length);

:)

BlueWater
  • 48
  • 10