8

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.

Sam
  • 7,252
  • 16
  • 46
  • 65
Shiva Krishna Bavandla
  • 25,548
  • 75
  • 193
  • 313
  • 2
    possible duplicate of [How to check if a number is float or integer?](http://stackoverflow.com/questions/3885817/how-to-check-if-a-number-is-float-or-integer) – Oliver Tappin Dec 01 '13 at 11:24

4 Answers4

14

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
}
Janith Chinthana
  • 3,792
  • 2
  • 27
  • 54
6

You can use a regular expression

var float= /^\s*(\+|-)?((\d+(\.\d+)?)|(\.\d+))\s*$/;
var a = $(".check_int_float").val();
if (float.test(a)) {
        // do something
    }
    //if it's NOT valid
    else {
   alert('Value must be float or int'); 
    }
Mahmoude Elghandour
  • 2,921
  • 1
  • 22
  • 27
1

You can use a regular expression to determine if the input is satisfying:

// Checks that an input string is a decimal number, with an optional +/- sign   character.
var isDecimal_re = /^\s*(\+|-)?((\d+(\.\d+)?)|(\.\d+))\s*$/;

function isDecimal (s) {
    return String(s).search (isDecimal_re) != -1
}

Keep in mind that the value from the input field is still a string and not a number type yet.

reto
  • 9,995
  • 5
  • 53
  • 52
0

I think the best idea would be to check like this ie, to check the remainder when dividing by 1:

function isInt(value) {
    return typeof value === 'Num' && parseFloat(value) == parseInt(value, 10) && !isNaN(value);
 } 
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • k in order to check if it was an integer, may be we can also use default `.isNumeric` in jquery, so now need to find if it was a float, also it there any default method like `.isNumeric` in jquery ? – Shiva Krishna Bavandla Dec 01 '13 at 11:28
  • @shivakrishna:- Yes there is a function .isNumeric in Jquery. http://api.jquery.com/jQuery.isNumeric/ – Rahul Tripathi Dec 01 '13 at 11:30
  • @rahuli mean to ask, whether there is also for finding a float ? – Shiva Krishna Bavandla Dec 01 '13 at 11:30
  • You may check for float like this: function isFloat(value) { return value === +value && value !== (value|0); } – Rahul Tripathi Dec 01 '13 at 11:31
  • @shivakrishna:- Sure, give it a try! – Rahul Tripathi Dec 01 '13 at 11:34
  • k now, if i enter 0.0 or 1.0, its returning false actually, so my point is a user can also enter the value as 0.0 or 1.0 as the float valuesinstead if 0 or 1 right ? so may be we should able to handle that scenarios too right ? – Shiva Krishna Bavandla Dec 01 '13 at 11:44
  • @shivakrishna:- How about this function: function isFloat(value){ return value!= "" && !isNaN(value) && Math.round(value) != value; } – Rahul Tripathi Dec 01 '13 at 11:54
  • also when i tried isFloat(32.56), it returns `true`, but when i tried the same from template like isFloat(value), it is returning false, because it was a string that coming from input field value – Shiva Krishna Bavandla Dec 01 '13 at 13:09
  • @shivakrishna:- Did you try with the function which I posted in comments above? – Rahul Tripathi Dec 01 '13 at 13:15
  • yup i tried the function isFloat(value){ return value!= "" && !isNaN(value) && Math.round(value) != value; } – Shiva Krishna Bavandla Dec 01 '13 at 18:54
  • So if we enter the value as 2.0, 3.0, 4.0 etc., it was returning me false, and when we give 2.1, 3.1,4.1 etc., its returning true, that means it is considering 2.0, 3.0 etc,(that ends with `.0`) also as integer, which i dont want, because user can also be able to enter the value as 0.0, 1.0 etc., and its working except this case actually – Shiva Krishna Bavandla Dec 01 '13 at 18:55