javascript中判断变量是否为数字类型抄的方法有两种:

方法一:

function isnum(s)

{

if(s!=null){

var r,re;

re = /\d*/i; //\d表示数字,*表示匹配多个数字

r = s.match(re);

return (r==s)?true:false;

}

return false;

}

方法二:

function isnum(s)

{

if (s!=null && s!="")

{

return !isnan(s);

}

return false;

}

两种方法都可以检查参数是否为知数字。

第一种使用正则表达式检查,对于检查的内容更加灵活,但显得麻烦;

第二种方法,直接调用系统的道isnan() ,对所有数字均有效,简单快捷。

原文地址:https://www.weidianyuedu.com/content/5320849711140.html

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐