Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check a value is float or int in jquery

Tags:

jquery

int

I have the following html field, for which i need to check whether the input value is float or int,

<p class="check_int_float" name="float_int" type="text"></p>


$(document).ready(function(){
   $('.check_int_float').focusout(function(){

       var value  = this.value
       if (value is float or value is int)
          {
           // do something
          }      
       else
          {
           alert('Value must be float or int');   
          }  

   });

});

So how to check whether a value is float or int in jquery.

I need to find/check both cases, whether it is a float, or int, because later if the value was float i will use it for some purpose and similarly for int.

like image 416
Shiva Krishna Bavandla Avatar asked Dec 01 '13 11:12

Shiva Krishna Bavandla


People also ask

How do I check that a number is float or integer?

Check if float is integer: is_integer() float has is_integer() method that returns True if the value is an integer, and False otherwise. For example, a function that returns True for an integer number ( int or integer float ) can be defined as follows. This function returns False for str .

How check value is integer or not in jQuery?

Answer: Use the jQuery. isNumeric() method You can use the jQuery $. isNumeric() method to check whether a value is numeric or a number. The $. isNumeric() returns true only if the argument is of type number, or if it's of type string and it can be coerced into finite numbers, otherwise it returns false .

How do you check whether a number is integer or float in JavaScript?

isInteger() In the above program, the passed value is checked if it is an integer value or a float value. The typeof operator is used to check the data type of the passed value. The isNaN() method checks if the passed value is a number.

How do you check if a variable is a float?

The is_float() function checks whether a variable is of type float or not. This function returns true (1) if the variable is of type float, otherwise it returns false.


1 Answers

use typeof to check the type, then value % 1 === 0 to identify the int as bellow,

if(typeof value === 'number'){
   if(value % 1 === 0){
      // int
   } else{
      // float
   }
} else{
   // not a number
}
like image 139
Janith Chinthana Avatar answered Sep 29 '22 00:09

Janith Chinthana