检查字符串是否仅包含数字 [英] Check if string contains only digits

查看:147
本文介绍了检查字符串是否仅包含数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查字符串是否只包含数字。我用过这个:

I want to check if a string contains only digits. I used this:

var isANumber = isNaN(theValue) === false;

if (isANumber){
    ..
}

但意识到它还允许 + - 。基本上,我想确保输入包含ONLY数字而不包含其他字符。由于 +100 -5 都是数字, isNaN()不是正确的方法。
也许我需要一个正则表达式?任何提示?

But realized that it also allows + and -. Basically, I wanna make sure an input contains ONLY digits and no other characters. Since +100 and -5 are both numbers, isNaN() is not the right way to go. Perhaps a regexp is what I need? Any tips?

推荐答案

怎么样

var isnum = /^\d+$/.test(val);

这篇关于检查字符串是否仅包含数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆