Check that variable is a number

前端 未结 10 1590
陌清茗
陌清茗 2020-12-29 20:38

How can I check that a variable is a number, either an integer or a string digit?

In PHP I could do:

if (is_int($var)) {
    echo \'$var is integer\'         


        
10条回答
  •  执念已碎
    2020-12-29 21:21

    You should use

    simple way to check whether given value is number by using "if condition"

    function isInteger(value)      
    {       
        num=value.trim();         
        return !(value.match(/\s/g)||num==""||isNaN(num)||(typeof(value)=='number');        
    }
    

    it will return true if the value which we are passing is an integer..

    solved for     
    value=""      //false null     
    value="12"    //true only integers       
    value="a"     //false     
    value=" 12"   //false      
    value="12 "   //false         
    value=" "     //false space        
    value="$12"   //false special characters 
    value="as12"    //false
    

提交回复
热议问题