checking string for non-numeric values

I am storing a phone number as a string is there a function checking if the new phone number entered contains non-numeric values?

I think in V6 is us to be IsNumeric

thanks.
Who is Participating?

Commented:
public bool IsNumerical(string str)
{
foreach (char c in str)
{
if (!Char.IsNumber(c))
return false;
}

return true;
}
0

RetiredCommented:
Hi yanci1179;

Here is a solution using Regular Expressions.

// Test string
string input = "123d0349";

if( System.Text.RegularExpressions.Regex.IsMatch(input, @"^\d+\$") )
MessageBox.Show("Input only has digits.");
else
MessageBox.Show("Input has NON digits in it.");

Fernando
0

Commented:
You can still using the Visual Basic functions, by adding a reference to the Micro\$oft Visual Basic run time library, and calling the IsNumeric function.

Bob
0

Commented:
In case the ultimate goal here is to determine if the phone number is valid you could use:

Regex PhoneRegex = new Regex(@"^(\d{3}|\(\s*\d{3}\s*\))\s*-?\s*\d{3}\s*-?\s*\d{4}\$|^\d{3}\s*-?\s*\d{4}\$|^\d{1}\s*-?\s*\d{4}\$");

if(PhoneRegex.IsMatch(TextField.Text.Trim()))
{
phone = TextField.Text.Trim()
}
else
{
Response.Write("invalid phone numer");
}
0

Commented:
"And another's on, and another's on, another one rides the bus"!!!

Bob
0

Commented:
This recognizes

111-111-1111
111 111 1111
(111) 111 1111
etc.

Note it also recognizes 7 digit only phone numbers and 5 digit phone numbers - which our oganization uses since we know the pre-fix.  For only complete 10 digit phone numbers use:
Regex PhoneRegex = new Regex(@"^(\d{3}|\(\s*\d{3}\s*\))\s*-?\s*\d{3}\s*-?\s*\d{4}\$");

I would then strip out any non-numeric characters before storage, instead of throwing an error, since it is a valid phone number
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.