【问题标题】:validate keyboard input for ASCII and special characters验证 ASCII 和特殊字符的键盘输入
【发布时间】:2012-07-14 07:43:22
【问题描述】:

我的 windows phone 7 中有文本框。我想验证用户是否输入了普通字符或某些特殊字符或 ASCII。

【问题讨论】:

  • 能否用文字举例说明您的期望
  • 是的,当然。一分钟让我为你做这件事..
  • asciitable.com 这里看到扩展的 ASCII 码从 128 码到 175..

标签: c# windows-phone-7 xaml


【解决方案1】:

您可以通过执行以下操作来确定按下的键是字母、数字还是特殊字符:

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
  }
}

【讨论】:

  • 它不工作.. 它也需要 ASCII 作为字母。 :)
  • 如果您想知道如何确定 ascii 与 unicode,那么 chibacity 对这个问题的回答可能会对您有所帮助:stackoverflow.com/questions/4459571/…
  • 我已经这样做了,并提出了我的答案。再次感谢:)
【解决方案2】:

我是这样做的..

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;
        }

【讨论】:

    【解决方案3】:

    简单的使用带有蒙版的蒙版文本框!!!

    【讨论】:

    • visual studio 有一个名为 masked text box 的控制器。在此处阅读 =>codeproject.com/Articles/1534/Masked-C-TextBox-Control
    • 哎呀,你完全走错了方向。我说的是 Windows Phone 7 Perceptive。不是 Web 或 Windows 窗体基础应用程序。谢谢
    【解决方案4】:

    您可以使用此函数获取有关文本框中文本的数据:

     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++;
    
                }
            }
    

    称它为:

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

    textBox1 是你的文本框。它将填充a,b,c 的值,并使用这些值您可以根据需要执行计算。

    【讨论】:

      猜你喜欢
      • 2014-10-27
      • 1970-01-01
      • 2015-09-13
      • 1970-01-01
      • 1970-01-01
      • 2013-05-16
      • 2018-02-22
      • 1970-01-01
      • 2011-03-09
      相关资源
      最近更新 更多