0

I know this topic has been quite active in SO, but now I seem to have found something strange with input fields with type="number" and decimal numbers. My code is something like this:

function Sum(){
  var f = document.myform;
  var num1 = f.num1.value;
  if (isNumber(num1)){
    alert(parseFloat(num1));
  }
}

function isNumber(n) {
    return (!isNaN(parseFloat(n)) && isFinite(n) && n >= 0);
}
#num1 {
  display:none;
}
<form name="myform" enctype="application/x-www-form-urlencoded" action="javascript:void(0);">
  <input id="num1" type="number" value="1.5">
  <input id="run_button" class="button" type="submit" onclick="Sum()"/>
</form>

Every time I put decimal numbers in number fields, I get this error, but if the numbers are without the dot, the error does not appear. You can test in test form.

enter image description here

João Pimentel Ferreira
  • 14,289
  • 10
  • 80
  • 109
  • 1
    Works in Chrome for me -> https://jsfiddle.net/adeneo/143226rj/1/ – adeneo Jul 24 '17 at 19:48
  • Isn't this related to [this](https://stackoverflow.com/questions/22148080/an-invalid-form-control-with-name-is-not-focusable), i.e. elements hidden with `display:none` having issues with validation etc ? – adeneo Jul 24 '17 at 19:51
  • @adeneo I don't want to bother you, but try this [test form](http://autocosts.work/XX). It works fine with normal numbers, but with decimal numbers (with dot), it doesn't work. – João Pimentel Ferreira Jul 24 '17 at 19:54

1 Answers1

3

This solved my problem

<input type='number' min='0' step='Any'/>
João Pimentel Ferreira
  • 14,289
  • 10
  • 80
  • 109