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.");
}
The range of values that
intcan take is from negative 2,147,483,648 through positive 2,147,483,647.0161 287 1234is within this range,08720 123 456isn’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.