On my webpage there is an input box that should only allow user to enter a positive int/float number (i.e. 1234, 123.4, 12.34, 1.234 should all be allowed).
To make sure the value is valid, I have a function to validate the value before sending it to the server:
function isPositiveFloat(s){
return String(Number(s)) === s && Math.floor(Number(s)) > 0;
}
The function works great expect for one scenario: isPositiveFloat(1.0)
will return false, as it turns out Number(1.0)
will convert it to 1
, therefore made the validation failed.
Any suggestions on how should I resolve this issue? Is using regex the only way to go?
Thanks in advance for any help!