初学者:正则表达式如何检查该值只是数字 [英] beginner: regex how to check that value is only numbers

查看:56
本文介绍了初学者:正则表达式如何检查该值只是数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


i必须检查文本字段的值是否为数字(他们必须填写

a数字在7到10位数之间) ,

所以:1234567是允许的

和123456789999是不允许的;


也,没有其他特征允许:

错:12345ABV

错误:12#$ as1123


仅允许1234567或12345678或123456789或1234567899


i尝试了类似


------------------源代码------ --------------------------

patroon = / [0-9] + /

if(!patroon.test(invoer)){

alert(不允许使用某些字符:+ + invoer)

document.getElementById(veld) .focus()

返回false

}

------------------- ----------------------------------------------


当我填写:qqqqqqq它有效,但当我在12345wert它接受它。


感谢

解决方案

as1123


仅允许1234567或12345678或123456789或1234567899


i尝试了一些东西喜欢


------------------源代码----------------- ---------------

patroon = / [0-9] + /

if(!patroon.test(invoer) ){

提醒(不允许使用某些字符: + invoer)

document.getElementById(veld).focus()

返回false

}

- -------------------------------------------------- -------------


当我填写:qqqqqqq它有效,但当我在12345wert它接受它。


感谢


nescio在2006年1月24日上午4:35发表以下内容:

hello,

我必须检查文本字段的值是否为数字(它们必须填写数字在7到10位之间),
所以:1234567是允许的
不允许使用123456789999;




函数checkNumber(numToCheck){

if(parseInt(numToCheck,10)== numToCheck&& (numToChec k.length> 7)&& numToCheck.length< 10)

{alert(''valid'')}

else

{alert(''invalid'')}

}


-

Randy

comp.lang.javascript常见问题 - http://jibbering.com/faq &新闻组每周

Javascript最佳实践 - http://www.JavascriptToolbox .com / bestpractices /


Randy Webb于2006年1月24日在comp.lang.javascript中写道
< blockquote class =post_quotes> nescio在2006年1月24日凌晨4:35发表以下内容:

你好,

我必须检查一下文本字段的值是一个数字(他们必须填写7到10位数之间的数字),
所以:允许1234567
和123456789999是不允许的;



function checkNumber(numToCheck){
if(parseInt(numToCheck,10)== numToCheck&&(numToChec k.length> 7)&& numToChe
ck.length< 10) {alert(''valid'')}

{alert(''invalid'')}
}




numToCheck ='''1234567'';


result = / ^ \d {7,10}


hello,

i must check if the value of a text field is a number (they have to fill in
a number between 7 and 10 digits),
so: 1234567 is allowed
and 123456789999 is not allowed;

also, there are no other characteres allowed:
wrong: 12345ABV
wrong: 12#$as1123

only 1234567 or 12345678 or 123456789 or 1234567899 is allowed

i have tried something like

------------------ source code --------------------------------
patroon = /[0-9]+/
if(!patroon.test(invoer)){
alert("some characters are not allowed: " + invoer)
document.getElementById(veld).focus()
return false
}
-----------------------------------------------------------------

when i fill in: qqqqqqq it works, but when i fil in 12345wert it accepts it.

thanks

解决方案

as1123

only 1234567 or 12345678 or 123456789 or 1234567899 is allowed

i have tried something like

------------------ source code --------------------------------
patroon = /[0-9]+/
if(!patroon.test(invoer)){
alert("some characters are not allowed: " + invoer)
document.getElementById(veld).focus()
return false
}
-----------------------------------------------------------------

when i fill in: qqqqqqq it works, but when i fil in 12345wert it accepts it.

thanks


nescio said the following on 1/24/2006 4:35 AM:

hello,

i must check if the value of a text field is a number (they have to fill in
a number between 7 and 10 digits),
so: 1234567 is allowed
and 123456789999 is not allowed;



function checkNumber(numToCheck){
if(parseInt(numToCheck,10)==numToCheck&&(numToChec k.length>7)&&numToCheck.length<10)
{alert(''valid'')}
else
{alert(''invalid'')}
}

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/


Randy Webb wrote on 24 jan 2006 in comp.lang.javascript:

nescio said the following on 1/24/2006 4:35 AM:

hello,

i must check if the value of a text field is a number (they have to
fill in a number between 7 and 10 digits),
so: 1234567 is allowed
and 123456789999 is not allowed;



function checkNumber(numToCheck){
if(parseInt(numToCheck,10)==numToCheck&&(numToChec k.length>7)&&numToChe
ck.length<10) {alert(''valid'')}
else
{alert(''invalid'')}
}



numToCheck = ''1234567'';

result = /^\d{7,10}


这篇关于初学者:正则表达式如何检查该值只是数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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