4

I want to use a form input with type="number" and only allow numbers to be entered.

<input type="number" class="form-control fc-input"/>

I don't have a submit button, instead the value is checked after the input loses focus. However when you use type="number" and a non-number is entered, both the valueAsNumber and the value attributes of the input will be useless (NaN respectively ""). The problem with this is that I want to differentiate between the user entering an empty string (""), and the user entering a non-number value (e.g. 123abc). When the input is empty I want to execute function a, and if it's just a non-number I want to execute function b (otherwise do c).

Is there a good way to differentiate between NaN input and empty ("") input like this?

4
  • 1
    Perhaps I'm misunderstanding your question, but why not if(value === NaN) and if(value === "")? Commented Nov 3, 2014 at 19:58
  • 2
    Funny thing about NaN is that it is not equal to itself... NaN === NaN // false Commented Nov 3, 2014 at 20:00
  • @Jordan: The if(value === NaN) will always be false, no matter what value is. Nothing is === to NaN, including NaN. Commented Nov 3, 2014 at 20:04
  • @Jordan The problem is that whenever I enter both an empty string (remove all input) and when I enter a NaN value ("abc") the value will always be "", and at the same the the valueAsNumber will always be NaN, therefore I can not differentiate between the two cases. Commented Nov 3, 2014 at 20:09

1 Answer 1

10

The problem is input type of number does a lot of stuff under the covers and the specification for it does not expose the actual invalid value to you. So an empty input and an invalid string look the same. So you need to do some investigation work using validity

var result = document.getElementById("result");

document.getElementById("num").addEventListener("keyup", function() {
  var isValid = this.validity.valid;
  var len = this.value.length;

  if (isValid && !len) {
      result.innerHTML = "No value";
  } else if (!isValid) {
      result.innerHTML = "Invalid number";
  } else {
      result.innerHTML = "Valid number: " + this.valueAsNumber;
  }

});
<input type="number" id="num" />
<span id="result"></span>

Problem with the code above is if you make it required, the empty check will fail. If it is required the if check would need to be

if (!isValid && this.validity.valueMissing) {
Sign up to request clarification or add additional context in comments.

2 Comments

I was completely unaware of the validity attribute. It does exactly what I need, thank you!
If you are using the validity, then you should use the "input" event instead as all browsers that support validity also support the input event

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.