Tuesday, March 16, 2010

IsNumeric in C#

If you need the functionality of IsNumeric in C# and don't want to use the Microsoft.VisualBasic dll then you could use these functions.  I haven't tried them out so can't say how effective they are, just here in case I need something like it in the future.

I found these online.  I can't find the link where I found the one below (my browser just crashed)
using System;
using System.Text;
using System.Text.RegularExpressions;

private bool IsTextValidated(string strTextEntry)
      Regex objNotWholePattern = new Regex("[^0-9]");
      return !objNotWholePattern.IsMatch(strTextEntry)
           && (strTextEntry != "");     

public static bool IsNumeric(object numberString)
char[] ca = numberString.ToString().ToCharArray();
for (int i = 0; i < ca.Length; i++)
if (!char.IsNumber(ca[i]))
if (ca[i] != '.')
return false;
if (numberString.ToString().Trim() == "")
return false;
return true;

No comments: