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
.
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 .
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 .
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.
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.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With