How to check for letter

in my if statement i want to check to see if the user has entered a letter, how would i go about doing that since i cannot convert string to char.....and it seems C# doesnt let you do IsNumeric(string value) like in VB....any help with this

 

[code languge="C#"]

double y = 0;

if (x.Length <= 8 || x.Length > 13)

{

MessageBox.Show("Your phone number does not contain the correct " +

"number of digits. Please format number " +

"as ###-###-####","Incorrect Number Format", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

return x;

}

[/code]




Answer this question

How to check for letter

  • Duque Vieira

    If I were you I'd use regular expression to validate the phonenumber. IMHO it looks cleaner than iterating through the string. Have no idea which is to prefer performance-wise though.

    Regex.IsMatch(x, @"^\d{8-13}$");

    The above snippet will return true if x is a string of 8-13 numbers. Beware, I didn't test is ;)

    /David


  • nanocity

    Thanks Matthew and I agree. you shouldnt import a dll or a reference from another language to be used in this language - its just not the correct way of doing things :-)

  • Steve1999

    overall, if you want to check if a string contains a letter:

    foreach(Char curChar in theString)

    {

       if (!Char.IsLetter(curChar))

       {

          //current char is not a letter

       }

    }

     

    in regards to your problem, you maybe better off using a masked textbox.

    http://msdn2.microsoft.com/en-us/library/bbabas53.aspx

    you could do this, but wouldnt recommend it:

     

    foreach(Char curChar in theString)

    {

       if (!Char.IsNumber(curChar) && !curChar.ToString().Equals("-"))

       {

          //text entered isnt correct.

       }

    }



  • arogan

    This is perfect posibility for using MaskedBox control. You will not only control the valid keys entered, but also show the phone number in the right format. Format that you suggest in messagebox is just the right format for Format property of MaskedBox.

  • Rhodri Jenkins

    Hi,

    If you want to use the IsNumeric function from Visual Basic in your C# code for that

    1. Add a reference to "Microsoft.VisualBasic" in your project and then
    2. Use Microsoft.VisualBasic.Information.IsNumeric(strNumber); to check if your string is numeric or not.


  • Jagdeep Sihota

    To bad i am using 1.1

  • sidhuvirgoster

    I think Ahmed's solution is far better than using the Visual Basic import (which will probably just call char.IsNumber() anyway).
  • akodad

    You can use the Microsoft MaskedEditBox as Com component, and you already can find it under COM tab in adding toolbox items dialog. It's not the perfect solution, but it will safe you from lot of work. And when you move from 1.1 to 2.0 one day, you will just replace with new .NET MaskedBox. I know that many developers doesn't want to use Masked controls, because more properties should be set, but when you learn how to use them, you will just love them from that moment on.

  • How to check for letter