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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular Expression to check the length of a textbox.

Status
Not open for further replies.

LauraCairns

Programmer
Jul 26, 2004
173
GB
I have the following regular expression and at present it checks that a field contains only letters and is more than 2 characters in length. I would like to change this slightly to also check that the text entered is 25 characters or less. I'm not very good with regular expressions and was wondering if someone could help me to do this.
My expression looks like the following: -

ValidationExpression="^[A-Za-z]{2,}$"

If i can't do this with a regular expression is there any other way I can do this.

Cheers fo any help anyone can give me.
 
I am not to good at regular expressions either. You might just want to use the maxlength property of the text box.

Thanks,
Patrick Green
 
this might help

Match MyRegMatch = Regex.Match(_input,@"^[A-Za-z]{2,25}$");
if(MyRegMatch.Success)
{
Console.WriteLine("good string " + _input);
Console.WriteLine(MyRegMatch.Groups[0].Value);
}
else
{
Console.WriteLine("bad string " + _input);
}

^[A-Za-z]{2,25}$ should work for your textbox ref

Marty
 
I would go with the regex that you have that checks for alpha chars and minimum length. I would not, however, rely on regex for max length. Why? Because it will confuse a user. If they can enter more than 25 characters, then according to them, the system should be able to take more than 25 characters.

I would set the maxlength of the input field to 25 in combination with what you currently have.

-----------------------------------------------
"The night sky over the planet Krikkit is the least interesting sight in the entire universe."
-Hitch Hiker's Guide To The Galaxy
 
AtomicChip,
No argument with your logic, and by that same logic the user should only be able to type in A-Za-z. So we need a little javascript for the textbox that only the accepts A-Za-z.
Marty
 
I would totally agree. Thanks for pointing that out - I posted that halfway through my first coffee this morning (otherwise I probably would have caught that myself). Now... let's talk about some cross-browser compatability ;)

-----------------------------------------------
"The night sky over the planet Krikkit is the least interesting sight in the entire universe."
-Hitch Hiker's Guide To The Galaxy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top