# VBForums CodeBank > CodeBank - C# >  C# : isNumeric (FW 1.1)

## Shuja Ali

To check if a string is a valid number we either have to use the Try..Catch..Finally with Integer.Parse or try Double.TryParse.

I just saw a method using regular expressions sometime back 

```
		private bool isNumeric(string stringToCheck) 
		{
			System.Text.RegularExpressions.Regex _isNumber = 
						new System.Text.RegularExpressions.Regex
						(@"(^[-+]?\d+(,?\d*)*\.?\d*([Ee][-+]\d*)?$)|(^[-+]?\d?(,?\d*)*\.\d+([Ee][-+]\d*)?$)");
			return _isNumber.Match(stringToCheck).Success;
		}
```

We can call this function like this 

```
if (isNumeric("1234"))
			{
				//string is a valid number
			}
			else
			{
				//string is not a valid number
			}
```

----------


## iPrank

Thanks for the code with RegExp.  :Thumb: 

Here is a old codebank thread on this topic.

and here is another way, (using loop and checking every charecter).

I don't feel easy wth RegExp.  :Big Grin:  
I find the loop idea easier and easily expandable.

----------


## Shuja Ali

Thanks for posting the links. I did have a look at the one in the first link before posting this. 

Regular expressions are not all that hard.

----------


## iPrank

I just tested it. Your code doesn't work with all locales.
I changed my locale to *German (Germany)*
In this locale comma (,) is decimal sign and dot (.) is digit separetor.
So, in this locale,
123,456,78.90 is non-numeric and 123.456.78,90 is numeric. But your function returns wrong results. 
How can we check locale in C# ?
BTW, I'm using FW2.0 .

----------


## Shuja Ali

I haven't tested it with locales. But I will look into it.

Also I am still stuck to FW 1.1.  :Mad:

----------


## Kasracer

VB Code:
public static bool IsNumeric(string ToBeChecked)
{
    double dbl = 0;
    return double.TryParse(ToBeChecked , System.Globalization.NumberStyles.Any , null , out dbl);
}

Works better

----------


## lil_bugga

> VB Code:
> public static bool IsNumeric(string ToBeChecked)
> {
>     double dbl = 0;
>     return double.TryParse(ToBeChecked , System.Globalization.NumberStyles.Any , null , out dbl);
> }
> 
> Works better


Hiya, can you explain how this works/what its doing so I can follow it a lil bit easier

Cheers

----------


## jmcilhinney

> Hiya, can you explain how this works/what its doing so I can follow it a lil bit easier
> 
> Cheers


Perhaps you should go to the MSDN library and read the documentation for the Double.TryParse method for yourself.

I'd also like to point out that the Double.TryParse method was invented in the first place specifically for the implementation of the VB IsNumeric function.  The IsDate function in VB.NET 2003 still uses the "parse and catch the exception on failure" approach, which has terrible performance.  The authors of VB.NET decided this was acceptable for dates but numbers would be used more often and the performance hit was unacceptable, so Double.TryParse was born.  It therefore makes sense to use Double.TryParse in C# too, although this thread was specifically intended to display an alternative, not a replacement.

----------

