判断number时最好用正则

来源:互联网 发布:js页面载入事件 编辑:程序博客网 时间:2024/05/01 02:57
var text = '每天';
text = parsetInt(text, 10);
if(typeof text == 'number'){
    console.log('ok');
}else{
    console.log('no');
}
//输出ok,期待no



解法1:
var text = '10/每天';
text = parsetInt(text, 10);
if(typeof  text != 'number' || isNaN(text)){
            console.log('no');//输出no
        }



解法2:
var text = '每天';
var reg = new RegExp(/\d{1,19}/i);
        if(!reg.test(text)){
            text = '';
        }
0 0
原创粉丝点击