4

I would like to check if user input is a number; the 'input' is from an html input tag. I have provided code below. I don't understand why my code is not working, any help would be greatly appreciated.

document.getElementById('input').addEventListener("input",
function(){
  var userInput = document.getElementById('input').value;
  if(!isNaN(userInput)){
    alert("Is a number");
  }
  else{
    alert("Is not a number");
  }

},true);
User Input : <input id="input">

5
  • 1
    could <input type='number'> help? Commented Dec 25, 2018 at 5:59
  • Do console.log(document.getElementById('input').value) to see what you get Commented Dec 25, 2018 at 5:59
  • 4
    Possible duplicate of Check if input is number or letter javascript Commented Dec 25, 2018 at 6:07
  • use typeof to get a string indicating type Commented Dec 25, 2018 at 6:08
  • @Prabhakaran the answer to this question is different from the post you have provided. I think it will be beneficial to leave this post. With that being said, I am still unfamiliar with StackOverflow "etiquette"; if I should delete this post, let me know. Commented Dec 25, 2018 at 6:18

5 Answers 5

4

you can use typeof

var userInput = document.getElementById('input').value;
if(typeof userInput == 'number')){
  console.log("Is a number");
}else{
  console.log("Is not a number");
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. This was the simplest way of achieving the result without changing too much of the code.
Just a minor gotcha here. typeof NaN === 'number'. Read more stackoverflow.com/questions/2801601/…
2

Try it like this.

function validate(){
  var userInput = document.getElementById('input').value;
  if(!isNaN(userInput)){
    console.log("Is a number");
  }else{
    console.log("Is not a number");
  }
}
<html>
<body>
<input type="text" id="input" onchange="validate()">
</body>
</html>

Comments

2

Many good answers already here, but another way I can think is to compare the string with 0 like following

"string" >= 0 || "string" <= 0

This return true only if its a number. But the catch is that it will also return true if its an empty string. So you can use if you know for sure that user enters at least one character/number or you are already handling the empty input case.

1 Comment

I might be missing something here but '1' >= 0 && '1' <= 0 and 1 >= 0 && 1 <= 0 are both false. Do you mean "string" >= 0 || "string" <= 0?
1
<input type="text" onkeypress='return event.charCode >= 48 && event.charCode <= 57'>

accept only numbers.

3 Comments

User input doesn't have to be via keyboard. What if he copy & paste something?
You are right Ahmad Maleki... isNaN must be answer.
isNan or typeOf must be answer.
1

You can use typeof to validate.

> a = 10
10
> typeof a
'number'

> if(typeof a === 'number') { console.log("Number") } else { console.log("Not a Number") }
Number
undefined
>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.