Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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!!
Posted
Updated 11-Apr-10 18:40pm
v4

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.
 
Share this answer
 
[SOLVED]
I got the correct answer.
While using Regex, we should specify the Format as
^[0-9]{0,5}$ instead of only [0-9]{0,5}.
The ^ carrot shows begning match
and $ dollar shows ending match.

Anyways,thanks, OriginalGriff for your reply.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900