0

I have a textbox that I am using to the end user to submit their phone number. I want to be able to evaluate the textbox each time the user hit the submit button. My code work great the first time the end user clicks the button. If they had to fix an error in the textbox and click the button again, the Number is not evaluated again by the TryParse and the Number is set to 0. Can some please tell me I have use the TryParse more than once?

string NumberLength = TextBoxPhoneNumber.Text;
int Number;
if (int.TryParse(NumberLength, out Number))
{
    //I parsed the number out. Now lets get the length
    NumberLength = Number.ToString(CultureInfo.InvariantCulture);

    if (NumberLength.Length > 10)
    {
        LblInfo.Visible = true;
        LblInfo.ForeColor = Color.Red;
        LblInfo.Text = "Phone number can not be longer than 10 digits!";
        boolIsValid = false;
    }
    else if (NumberLength.Length < 10)
    {
        LblInfo.Visible = true;
        LblInfo.ForeColor = Color.Red;
        LblInfo.Text = "Phone number can not be shorter than 10 digits!";
        boolIsValid = false;
    }
}
jessehouwing
  • 106,458
  • 22
  • 256
  • 341
nate
  • 1,418
  • 5
  • 34
  • 73
  • 1
    A phonenumber should not be converted to int, since it 9 out of 10 times starts with a 0, that will be erased. Integers never start with a leading zero '0', so 10 will be 9 == invalid. The number 0612345678 will be 612345678 and invalid. – Max Dec 20 '14 at 19:38
  • 1
    The problem is not on the `TryParse` use. – Mephy Dec 20 '14 at 19:38
  • Can you show us more of the code that resides in? – Zipper Dec 20 '14 at 19:38
  • @Max Lets say the first time the user enters a letter into the textbox 'f3178593214', the Number is going to return a 0. If the user fixes this letter, and hit submit again, then the Number returned would be '3178593214', but it is never evaluated again, so it the Number returned is a 0 – nate Dec 20 '14 at 19:45
  • The error must be in the code that call this function. The TryParse is correct as is. – jessehouwing Dec 20 '14 at 19:47
  • @jessehouwing I debugged the code, there is not a error. If the user enters a string in the textbox, and clicks submit again, the string is not evaluated again – nate Dec 20 '14 at 19:55

3 Answers3

1

Try adding an else to the end of the if to turn the error message off again like this:

string NumberLength = TextBoxPhoneNumber.Text;
int Number;
if (int.TryParse(NumberLength, out Number))
{
    //I parsed the number out. Now lets get the length
    NumberLength = Number.ToString(CultureInfo.InvariantCulture);

    if (NumberLength.Length > 10)
    {
        LblInfo.Visible = true;
        LblInfo.ForeColor = Color.Red;
        LblInfo.Text = "Phone number can not be longer than 10 digits!";
        boolIsValid = false;
    }
    else if (NumberLength.Length < 10)
    {
        LblInfo.Visible = true;
        LblInfo.ForeColor = Color.Red;
        LblInfo.Text = "Phone number can not be shorter than 10 digits!";
        boolIsValid = false;
    }
    else
    {
        LblInfo.Visible = false;
        boolIsValid = true;
    }
}
else
{
    LblInfo.Visible = true;
    LblInfo.ForeColor = Color.Red;
    LblInfo.Text = "Phone number can only contain digits!";
    boolIsValid = false;
}
Martin Brown
  • 24,692
  • 14
  • 77
  • 122
0

Why You don`t use Regex Method ?

 using System.Text.RegularExpressions;


 Regex rgx = new Regex("\d");
 if(rgx.IsMatch(number))
 {
     //True
 }
xwpedram
  • 467
  • 1
  • 8
  • 20
  • Never heard of this before, looking into it – nate Dec 20 '14 at 20:10
  • @xwpedram it would help if you gave him an expression to go with it. Here are a few to choose from http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation – prospector Dec 21 '14 at 00:42
-1

if the string enter second time is of numeric type TRYPARSE will convert it into int value but if the value enter is of non- numeric then the method will retrun the 0. please put some validation to accept only numeric string in your text-box!!!

nitin
  • 156
  • 2
  • 13