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.
yanci1179Asked:
Who is Participating?
 
paulb1989Connect With a Mentor Commented:
public bool IsNumerical(string str)
{
      foreach (char c in str)
      {
            if (!Char.IsNumber(c))
                  return false;
      }
                  
      return true;
}
0
 
Fernando SotoRetiredCommented:
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
 
Bob LearnedCommented:
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
mrichmonConnect With a Mentor 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
 
Bob LearnedCommented:
"And another's on, and another's on, another one rides the bus"!!!

Bob
0
 
mrichmonCommented:
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.

All Courses

From novice to tech pro — start learning today.