1

I have textbox in my windows phone 7. I want to validate that user input normal character or some special character or ASCII.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Arslan Pervaiz
  • 1,575
  • 4
  • 29
  • 62

4 Answers4

2

You can determine if the key that is pressed is a letter, digit, or special character by doing the following:

private void textBox1_KeyPress(object sender, KeyEventArgs e)
{
  if (Char.IsLetter(e.KeyChar))
  {
    // The character is a letter
  }
  else if (Char.IsDigit(e.KeyChar))
  {
    // The character is a digit
  }
  else
  {
    // The character is a special character
  }
}
A. Abiri
  • 10,750
  • 4
  • 30
  • 31
1

I have done in this way..

public int CountChars(string value)
        {
            int result = 0;
            foreach (char c in value)
            {
              if (c>127)
                {
                    result = result + 10; // For Special Non ASCII Codes Like "ABCÀßĆʣʤʥ"
                }

                else
                {
                    result++; // For Normal Characters Like "ABC"
                }
            }
            return result;
        }
Arslan Pervaiz
  • 1,575
  • 4
  • 29
  • 62
0

simple use a mask text box with a mask!!!

Abadis
  • 2,671
  • 5
  • 28
  • 42
  • visual studio has a controler called masked text box.read here =>http://www.codeproject.com/Articles/1534/Masked-C-TextBox-Control – Abadis Jul 14 '12 at 08:03
  • Oups You Are Totally Going In Wrong Direction. I Am Talking About Windows Phone 7 Perceptive. Not Web or Windows Form Base Application. Thanks – Arslan Pervaiz Jul 14 '12 at 08:06
0

You can use this function to get data about the text in the textBox:

 private void validator(string value, out int letterCount, out int digitCount, out int specialCharCount)
        {
            letterCount=digitCount=specialCharCount=0;
            foreach (char c in value)
            {
                if (Char.IsLetter(c))
                    letterCount++;
                else if (Char.IsDigit(c))
                    digitCount++;
                else
                    specialCharCount++;

            }
        }

Call it as:

 int a, b, c;
 validator(textBox1.Text, out a, out b, out c);

textBox1 is your textBox. It will populate the values of a,b,c and using these values you can perform calculations as you desire.

naqvitalha
  • 793
  • 5
  • 9