正则表达式只允许10位数以下的数字? [英] Regex to only allow numbers under 10 digits?
本文介绍了正则表达式只允许10位数以下的数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试编写一个正则表达式来验证输入是一个纯正的整数(最多10位数,但我在其他地方应用该逻辑)。
I'm trying to write a regex to verify that an input is a pure, positive whole number (up to 10 digits, but I'm applying that logic elsewhere).
现在,这是我正在使用的正则表达式(我从获得)这里):
Right now, this is the regex that I'm working with (which I got from here):
^(([1-9]*)|(([1-9]*).([0-9]*)))$
在此函数中:
if (/^(([1-9]*)|(([1-9]*).([0-9]*)))$/.test($('#targetMe').val())) {
alert('we cool')
} else {
alert('we not')
}
然而,我似乎无法让它工作,我不确定它是正则表达式还是函数。我需要禁止%,.和'以及。我只想要数字字符。任何人都可以指出我正确的方向吗?
However, I can't seem to get it to work, and I'm not sure if it's the regex or the function. I need to disallow %, . and ' as well. I only want numeric characters. Can anyone point me in the right direction?
推荐答案
你可以这样做:
/^[0-9]{1,10}$/
代码:
Code:
var tempVal = $('#targetMe').val();
if (/^[0-9]{1,10}$/.test(+tempVal)) // OR if (/^[0-9]{1,10}$/.test(+tempVal) && tempVal.length<=10)
alert('we cool');
else
alert('we not');
参考 现场演示
Refer LIVE DEMO
这篇关于正则表达式只允许10位数以下的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文