正则表达式(RegularExpressionValidator)throgh编码未提供正确的输出. [英] Regex (RegularExpressionValidator) throgh coding not giving proper output..

查看:138
本文介绍了正则表达式(RegularExpressionValidator)throgh编码未提供正确的输出.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

RegularExpressionValidator

TextBox上的RegularExpressionValidator控件,
[0-9] {0,5}
1)仅允许数字.
2)它的长度最多可以为5.
简而言之,v只能在文本框中输入数字,最大长度为5.
类似于"87564","12345ssd"-有效.
"12k","4abcd","1234s"-无效.


这就是它的工作方式.
但是当我尝试使用类似..
的编码时
string strData ="5abcd";
if(System.Text.RegularExpressions.Regex.IsMatch(strData,"[0-9] {0,5}"))
{
MessageBox.Show(有效");
}
其他
{
MessageBox.Show("Invalid");
}


这将返回有效.
哪个不应该..
任何人都可以帮忙!!

RegularExpressionValidator

RegularExpressionValidator control on TextBox,
[0-9]{0,5}
1) it only allows digits.
2) its length can be upto 5.
In short, v can enter only numeric in text box upto length 5.
Like "87564", "12345ssd" - Valid.
"12k", "4abcd","1234s"- Invalid.


This is how it works of.
But when i try it using coding like..

string strData="5abcd";
if(System.Text.RegularExpressions.Regex.IsMatch(strData, "[0-9]{0,5}"))
{
MessageBox.Show("Valid");
}
else
{
MessageBox.Show("Invalid");
}


This returns Valid.
Which should not be..
Can anybody help!!

推荐答案

我怀疑它工作正常.
我假设您想抱怨Regex.IsMatch(strData, "[0-9]{0,5}")总是返回true?
您期望什么?当然,它将返回true(只要strData不为null)!提示我的字符串中可以有0到5位数字":零位数字是通过.因此,其中没有(或更多)数字的任何字符串都将通过.
I suspect it is working fine.
I assume you want to complain that the Regex.IsMatch(strData, "[0-9]{0,5}") is always returning true?
What did you expect? Of course it will return true (as long as strData is not null)! "there can be 0 to 5 digits in my string" is the clue: zero digits is a pass. So any string which has no (or more) digits in it will pass.


[已解决]
我得到了正确的答案.
使用正则表达式时,我们应将格式指定为
^ [0-9] {0,5}
[SOLVED]
I got the correct answer.
While using Regex, we should specify the Format as
^[0-9]{0,5}


而不是只有[0-9] {0,5}.
^胡萝卜显示开始比赛
instead of only [0-9]{0,5}.
The ^ carrot shows begning match
and


这篇关于正则表达式(RegularExpressionValidator)throgh编码未提供正确的输出.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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