[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

checking string for non-numeric values

Posted on 2006-05-10
6
Medium Priority
?
476 Views
Last Modified: 2008-02-01
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.
0
Comment
Question by:yanci1179
6 Comments
 
LVL 5

Accepted Solution

by:
paulb1989 earned 600 total points
ID: 16650309
public bool IsNumerical(string str)
{
      foreach (char c in str)
      {
            if (!Char.IsNumber(c))
                  return false;
      }
                  
      return true;
}
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 16650468
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16650832
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 35

Assisted Solution

by:mrichmon
mrichmon earned 800 total points
ID: 16651337
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16651356
"And another's on, and another's on, another one rides the bus"!!!

Bob
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 16651361
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Loops Section Overview
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question