If you want to use the IsNumeric function that comes with VB, then you
can set a reference to Microsoft.VisualBasic.dll in your project. IsNumeric
is defined as a static method on the Information class in the
Microsoft.VisualBasic namespace.
However, if you want a more ".NET" way (which isn't linked explicitly
for VB, some people have issue with this), you can use the static TryParse
method on the Double value type. This will attempt to parse a string, but
you will have to be a little more specific about what is acceptable and what
is not (through the use of number styles).
1 Use the Parse method on any of the Int32, Int64, etc classes. If it
throws an exception it's not numeric.
2. Add a reference to the Microsoft.VisualBasic.dll assembly and then use
the IsNumeric function on the Microsoft.VisualBasic.Information class.
3. Write a regex statement that checks whether the string has anything but
numbers.
Thanks for your reply. Can you tell me where I can get
more information on regular expressions as your example is
not exactly what I am looking for but close.
I'm not familiar with Regex(@"\d+|\d+\.\d+") this.
You can get several books of regular expressions, in the MSDN you can also
find some doc related to .NET implementation of it, if you haven;t do
nothing with it I advise you to buy a book about them and read it first,
they can get very complex
Also take a look at this link, it's on JScript but the concepts are the same
Seeing as noone mentioned this, and even though I'm not sure how it would
perform ...
string numeric = "0123456789"
string num = textBox.Text;
for(int x = 0; x < num.Length; x++)
{
if( numeric.IndexOf(num[x]) == -1 )
{
// not a number, one of the characters was not 0 1 2 3 4 5 6 7 8 9
}
}
If you want a decimal point, add "." or "," to numeric.
Use a bool flag to make sure there is only one.
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.