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!

Returning Value of a textbox 2

Status
Not open for further replies.

Smitty020

MIS
Jun 1, 2001
152
US
I'm trying to work on some form validation, and I was wondering what the best way is to validate what was entered into a textbox. How can I check to see whether or not the value is a number?

 
if you only want digits
Code:
Match MyRegMatch = Regex.Match(_input,@"^\d+$");	
if(MyRegMatch.Success)  
{
 you have at least one digit
}
you could also put the regex in a RegularExpressionValidator
on your form.
Marty
 
Smitty020,
If you're new to Regular Expressions, they may seem like greek to you. Regular Expressions are very powerful and efficient but their syntax is rather contorted. Given their power though, you should spend time familiarizing yourself with them.

However, if you're not in the mood to do study RegEx, another cheaper and less efficient, yet easier-to-read solution to your problem would be the following:
Code:
[COLOR=blue]string[/color] text = yourTextBox.Text;
[COLOR=blue]try[/color]
{				
   [COLOR=blue]double[/color] yourNum = [COLOR=blue]double[/color].Parse(text);
   [COLOR=green]// text IS a number ...[/color]
}
[COLOR=blue]catch[/color]
{
   [COLOR=green]// text IS NOT a number ...[/color]
}
JC

Friends are angels who lift us to our feet when our wings have trouble remembering how to fly...
 
Thanks guys.........
I looked into Regular expressions, and I think I need to spend some more time with them....thanks for the direction though. I used something similar to JCruz's suggestion for now.

Cheers,
Smitty
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top