Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular Expression Problem 1

Status
Not open for further replies.

James1981

Programmer
Nov 2, 2002
76
US
Hi,

I am using the following Regular expression to validate an TextBox:

Regex r = new Regex("[a-zA-Z]{3,20}");
r.IsMatch(txtInput.Text);

This should only let alpha chars be entered, right? Well when i run the code i can enter any characters (numbers, non-standard chars) into the box as long as I have 3 alpha chars in the textbox as well.

Is there an obvious mistake i am making?

Cheers
James
 
James: Try

Regex r = new Regex("[a-zA-Z]\w{3,20}");
 
This doesn't compile, the compiler gives me an error:

"Unrecognised Escape Character"

:-(

I have used \w already in the RegularExpressionValidator and it worked fine, but when you use Regex it doesn't seem to work!

Any ideas?

James
 
James: Post more of your code (I haven't used the Regex function). I would like to test it.
 
What i posted at the start of this thread is pretty much the code I am using. You need to use the following library tho:

System.Text.RegularExpressions

James
 
James
try
("^[a-zA-Z]{3,20}$")

The problem if you omit the ^ and $ is that is match will return true for any match of the pattern in the string. The ^ forces the engine to look at the start of the string and the $ forces it to look at the end. So both force it to examine the entire string only for a match
Mark [openup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top