Deepak Jawalkar Deepak Jawalkar - 7 months ago 14
Javascript Question

how to avoid negative,float,and decimal values in form input using javascript validation?

I want to avoid negative values and decimal values in form input, like

1.25
,
4.5
etc using javascript validation.

For negative numbers I am using
type="number"
and
min="0"
in input. But how to handle decimal values?

Any help would be great

Answer

The HTML5 support for the in-form validation might not be available across all browsers.

if(number !== parseInt(number,10)){
  //detects floating point numbers like 1.3  
} else if (number > -1) {
  // detects negative numbers
} 
Comments