There are two ways to judge integers: regular judgment and literal judgment.
Since word-by-word judgment is too inefficient, it will not be described here. Interested readers can Google it themselves.
1. Regular judgment
var r = /^ ?[1-9][0-9]*$/; //Positive integer
console.log(r.test(1.23));
Effectiveness test:
http://jsfiddle.net/wzsdp9Lc/
Extended function list
"^\d $" //Non-negative integer (positive integer 0)
"^[0-9]*[1-9][0-9]*$" //Positive integer
"^((-\d )|(0 ))$" //Non-positive integer (negative integer 0)
"^-[0-9]*[1-9][0-9]*$" // Negative integer
"^-?\d $" //Integer
"^\d (
\.\d )?$" //Non-negative floating point number (positive floating point number 0)
"^(([0-9] \.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\. [0-9] )|([0-9]*[1-9][0-9]*))$" //Positive floating point number
"^((-\d (
\.\d )?)|(0 (\.0 )?))$" //Non-positive floating point number (negative floating point number 0)
"^(-(([0-9] \.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]* \.[0-9] )|([0-9]*[1-9][0-9]*)))$" //Negative floating point number
"^(-?\d )(
\.\d )?$" //Floating point number
2. Rounding judgment
The idea of this method is to determine whether it is equal to the original value after rounding
var num=1.23;
if (parseInt(num) != num) {
console.log(num "is a non-integer");
}
else{
console.log(num "is an integer");
}
Effectiveness test
http://jsfiddle.net/euvn0L1g/1/