GIDForums  

Go Back   GIDForums > Computer Programming Forums > .NET Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 17-May-2005, 08:14
richiemac richiemac is offline
New Member
 
Join Date: May 2005
Posts: 16
richiemac is on a distinguished road

Checking textbox data is numeric!!


Hi all,

My question is fairly straight forward - I think. :-?

I have a textbox that a user will input numeric data into. I obviously need to validate the data against any wrong doing. Does .net provide anything in its framework to check for numeric data in the textBox->Text string? i.e. some kind of isNumeric () method.

Essentially something like,

CPP / C++ / C Code:
if ( myTextBox->Text.isNumeric () )
   System::String* str = myTextBox->Text;
else
   // Invalid data message
  #2  
Old 17-May-2005, 09:03
richiemac richiemac is offline
New Member
 
Join Date: May 2005
Posts: 16
richiemac is on a distinguished road
Ok so I've found a solution. If there is a better way then please let me know.

Here it is for any interested parties...

CPP / C++ / C Code:
#using<Microsoft.VisualBasic.dll>
...
...
...

if ( Microsoft::VisualBasic::Information::IsNumeric ( myTextBox->Text ) )
   myInt = System::Convert::ToInt32 ( myTextBox->Text );
else
   MessageBox::Show ( "Value not numeric" );
  #3  
Old 17-May-2005, 23:56
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 917
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Hello, richiemac.
Your solution is correct, thanks for sharing it with us! In case you would rather not reference another dll, you could do it another way(s). For example, how about just trying to do the conversion and catch the exceptions? That's a hack, I know, but don't knock it if it works

CPP / C++ / C Code:
Int32 myInt = 0;
try
{
	myInt = System::Convert::ToInt32 ( myTextBox->Text );
}
catch ( System::FormatException * pEx )
{
	myInt = 0; // or another default value
}
catch ( System::OverflowException * pEx )
{
	myInt = 0; // or another default value
}
MessageBox::Show(myInt.ToString());

In case you would rather have a "cleaner" solution, you could use a regular expression to check the string. Here is a solution resembling your original one, but without any additional dll:

CPP / C++ / C Code:
Int32 myInt = 0;
if ( System::Text::RegularExpressions::Regex::IsMatch(myTextBox->Text, 
	"^[-0-9]*.[.0-9].[0-9]*$") )
{	
	myInt = System::Convert::ToInt32 ( myTextBox->Text );				
}
else
{
	MessageBox::Show("Not a number");
}	 

If any other thoughts on this, I'm looking forward to reading them!

Best regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #4  
Old 18-May-2005, 04:13
richiemac richiemac is offline
New Member
 
Join Date: May 2005
Posts: 16
richiemac is on a distinguished road
Ok, so if I put everything together then, I can now check my user input for up to 3 valid hex characters by using this expression,

CPP / C++ / C Code:
if ( ! System::Text::RegularExpressions::Regex::IsMatch (this->address2_TB->Text, "[0-9a-fA-f]{1,3}" ))
{
   MessageBox::Show ( "Address 2: Invalid value" );
}
else
   // do some stuff

This works fine until I enter something like fdssthe in the textBox. I then get the following exception,

An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

Additional information: Additional unparsable characters are at the end of the string.
or : Could not find parsable characters.

which is confusing as I assumed I would get my MessageBox pop up informing me of invalid values.

Any ideas???????
  #5  
Old 18-May-2005, 05:51
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 917
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Thumbs up

Quote:
Originally Posted by richiemac
An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

Additional information: Additional unparsable characters are at the end of the string.
or : Could not find parsable characters.

Quote:
Originally Posted by richiemac
CPP / C++ / C Code:
else
   // do some stuff

You get that exception because of what you do in "some stuff"
You probably did something in your else that treated the string as if it had a maximum length of 3.
Now, the problem with your expression is that it doesn't match what it should. There are 2 special characters, ^ and $ which specify the beginning and the end of the string to match. The way you used it, it matches any of the characters in the string. If you have a valid character, it will ignore the rest. Now, if you add an ending $, the expression will not accept a string that doesn't end in a valid (or its understanding of "valid") character. If you will add the ^ in front, only 3 character hexadecimal strings will pass the filter. BTW, I believe you meant to use "[0-9a-fA-F]{1,3}", not f. This way, even W will match, because it is between A and f.
Please try the following and observe the differences:

CPP / C++ / C Code:
if ( ! System::Text::RegularExpressions::Regex::IsMatch 
	//(this->myTextBox->Text, "[0-9a-fA-F]{1,3}" ))
	//(this->myTextBox->Text, "^[0-9a-fA-F]{1,3}" ))
	//(this->myTextBox->Text, "[0-9a-fA-F]{1,3}$" ))
	(this->myTextBox->Text, "^[0-9a-fA-F]{1,3}$" ))
{
	MessageBox::Show ("Address 2: Invalid value");
}
else
	MessageBox::Show(this->myTextBox->Text);

Fyi, I didn't get this thing going from the first try (that's a first ) , so it was an interesting exercise for me. I hope to read more of your questions in the future.

Kind regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #6  
Old 18-May-2005, 05:58
richiemac richiemac is offline
New Member
 
Join Date: May 2005
Posts: 16
richiemac is on a distinguished road
Thanx buddy,

I'll give that a look. Oh, and yes there will be plenty of questions.
  #7  
Old 19-May-2005, 06:54
richiemac richiemac is offline
New Member
 
Join Date: May 2005
Posts: 16
richiemac is on a distinguished road
Worked a treat.

Thanx once again... ;-)
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 13:12
Mrs stacy12 C Programming Language 14 05-Feb-2005 18:02
fltk-2.0 cvs Plumb FLTK Forum 20 13-Nov-2004 07:10
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 15:13

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:22.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.