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!

Validating textbox for string input

Status
Not open for further replies.

sil3nt2004

Programmer
Aug 17, 2004
5
ZA
Hi,

I'm using to following method to validate for numeric input:

private bool IsNumeric(string strText)
{
try
{
int test = int.Parse(strText);
return true;
}
catch (Exception e)
{
return false;
}
}

How can I check for string input using a similar method?

Any help will be appreciated.

Thanx,
Ryno.
 
Ryno,

not sure if you could automatically check it, since you can convert pretty much anything to a string, but here is a little function that I wrote to do the same thing.

Code:
public bool ValidAlpha(string AlphaStr)  // ++++++ Checks to make sure a string is Alphabetic ++++++
{
	foreach (char c in AlphaStr)
	{
		if (!char.IsLetter(c))
			return false;
	}
	return true;
}

Good luck,
Kevin

- "The truth hurts, maybe not as much as jumping on a bicycle with no seat, but it hurts.
 
You might want to look at regular expressions.


//Include this at the top of the file
using System.Text.RegularExpressions;



//No guarantees this code works because I'm doing it from memory



Regex IsStringAlphabetic = new Regex(@"[A-Z][a-z]*");
Regex IsNumeric = new Regex(@"[0-9]*");




string myText = "Test";
string myNumber = "123";




if (IsStringAlphabetic.Match(myText).Success)
{
//The string is only letters if it reaches this point
}

if (IsNumeric.Match(myNumber).Success)
{
//The string is only numeric if it reaches this point
}


 
Thank you guys, both solutions solved my problem.

Appreciate it!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top