js 判断是不是 数字

var score = $("input[name='pscoreVoc[" + i + "].score']").val();
                if (score >= 0 && score <= 100) {
                    console.log('>>' + score);
                }
                if (!isNaN(score)) {
                    console.log("是数字");
                } else {
                    console.log("不是数字");
                }函数

 

使用isNaN()函数判断是不是数字时遇到的问题,当变量是空串时,isNaN()的返回值仍是false,但空串却不是数据,查了一下,才知道原来isNaN()把空串或空格做0处理的。字符串

或者使用  typeof 进行来判断input

 

alert(typeof 1);                // 返回字符串"number"  
alert(typeof "1");              // 返回字符串"string"  
alert(typeof true);             // 返回字符串"boolean"  
alert(typeof {});               // 返回字符串"object"  
alert(typeof []);               // 返回字符串"object "  
alert(typeof function(){});     // 返回字符串"function"  
alert(typeof null);             // 返回字符串"object"  
alert(typeof undefined);        // 返回字符串"undefined"
相关文章
相关标签/搜索