1

I have a strange problem, I'm using the code below to test for a phone number, in my tests my code worked fine. The program when live yesterday and someone used the phone number 08720 123 456 and my code failed. Can you tell me why it would return TRUE for 0161 287 1234 and FALSE for 08720 123 456?

Fax Number 1: 0161 287 1234 ---> IsNumber returns TRUE Fax Number 2: 08720 123 456 ---> IsNumber returns FALSE

static bool IsNumber(string value)
        {
            // Return true if this is a number.
            int number1;
            return int.TryParse(value, out number1);
        }

 bool testForFax = IsNumber(faxOrEmail);
            if (testForFax == true)
            {
                backgroundWorker2.RunWorkerAsync(); //send fax
            }
            else
            {
                MessageBox.Show("Please enter a fax number.");
            }
goodeye
  • 2,389
  • 6
  • 35
  • 68
Steve Wood
  • 51
  • 1
  • 1
  • 4

8 Answers8

5

The int.TryParse method tries to parse integer numbers not phones and faxes. You can't just throw any string to it and expect to get an integer. There shouldn't be any spaces in the string if you expect it to be successfully parsed into an integer.

Both strings return false:

class Program
{
    static void Main()
    {
        Console.WriteLine(IsNumber("0161 287 1234")); // false
        Console.WriteLine(IsNumber("08720 123 456")); // false

        Console.WriteLine(IsNumber("01612871234")); // true
        Console.WriteLine(IsNumber("08720123456")); // false because you overflowed Int32 which can store a maximum value of 2147483648
    }

    static bool IsNumber(string value)
    {
        int number1;
        return int.TryParse(value, out number1);
    }
}

So to validate a phone number you could use a regular expression.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
5

The range of values that int can take is from negative 2,147,483,648 through positive 2,147,483,647.

0161 287 1234 is within this range, 08720 123 456 isn't.

You should not be parsing a phone number this way - it is not, mathematically speaking, a number (can you add/subtract phone numbers in a meaningful way?).

You should validate it using a regular expression.

Community
  • 1
  • 1
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • this is the answer, your 08........ number is to large to fit in an int, you probably need to use an int64 – Moonlight Jan 13 '12 at 10:46
  • 1
    @Moonlight - No, he sholdn't be using a cast to an integral type to test for valid phone numbers. – Oded Jan 13 '12 at 10:51
  • for the method he used, he needed an int64 instead of the int32. i dont suggest it is the best/nicest way to do it – Moonlight Jan 13 '12 at 11:04
1

Because the 08720 123 456 exceeds the Max size of an integer. This is max: 2,147,483,647.

Don't use int.TryParse, but use a Regular Expression to check if the value is a Phone Number.

RBDev
  • 761
  • 5
  • 7
0

You can strip off the whitespaces and check for numbers.

Using regular expressions may be another good choice.

Sufian Latif
  • 13,086
  • 3
  • 33
  • 70
0

An Ints range is -2,147,483,648 to 2,147,483,647. So 0161 287 1234, would be parsed correctly as 1612871234, whereas 08720 123 456 is out of range and will not parse. So I suggest you use a RegEx to match a pattern of chars or something.

craig1231
  • 3,769
  • 4
  • 31
  • 34
0

The max value of an integer is 2147483647, your second number exceeds this value.

samn
  • 2,699
  • 4
  • 20
  • 24
0

How about this?

string number = "08720 123 456";

bool testForFax = number.All(c => Char.IsNumber(c) || Char.IsWhitespace(c))
Oliver
  • 43,366
  • 8
  • 94
  • 151
0
public static bool IsFaxNumber(string faxNo)
    {
        bool returnValue = false;
        var items = faxNo.Split(' ');
        if (faxNo.Length == 13 && items.Length == 3)
            if (items[0].Length == 4 && items[1].Length == 3 && items[2].Length == 4)
                returnValue = true;
        return returnValue;

    }
sjngm
  • 12,423
  • 14
  • 84
  • 114
karthik
  • 74
  • 9
  • 21
  • 45