![]() |
|
#1
|
|||
|
|||
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:
|
|
#2
|
|||
|
|||
|
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:
|
|
#3
|
||||
|
||||
|
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:
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:
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
|
|||
|
|||
|
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:
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
|
||||
|
||||
|
Quote:
Quote:
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:
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
|
|||
|
|||
|
Thanx buddy,
I'll give that a look. Oh, and yes there will be plenty of questions. |
|
#7
|
|||
|
|||
|
Worked a treat.
Thanx once again... ;-) |
Recent GIDBlog
Python ebook by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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