【问题标题】:Allow only alphabetic name in text box in c#c#中的文本框中只允许字母名称
【发布时间】:2015-06-19 11:47:37
【问题描述】:

我只需要允许用户在文本框中输入字母名称,例如 jhon 或 Kamran Khan。我已经从不同的链接获得了信息,但还没有任何工作。 在我的代码文本框中,获取以数字开头的值,例如 3kamran,但我不需要允许 3 或任何其他数字。 我正在使用 c# windows 应用程序,我的代码看起来像。

private void txtName_TextChanged(object sender, EventArgs e)
{
    if (System.Text.RegularExpressions.Regex.IsMatch(txtName.Text, "^[a-zA-Z]+$"))
    {
        MessageBox.Show("Enter Valid Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        txtName.Text.Remove(txtName.Text.Length - 1);
        txtName.Clear();
        txtName.Focus();
     }
}

【问题讨论】:

  • 删除^,虽然这不允许您输入双桶名称 - 即 Mary-Jane(或其中包含变音符号等的任何名称)
  • 尝试将@符号作为您的正则表达式匹配并修剪文本框文本,即 Regex.IsMatch(txtName.Text.Trim(), @"^[a-zA-Z]+$")
  • 创建一个包含所有 walid 字符的 SCII 数组,然后查看是否有无效字符。
  • 您应该使用 key_pressed 事件,并检查 Key == IsAlpha 属性设置句柄 true 或 false。

标签: c#


【解决方案1】:
   private void txtName_TextChanged(object sender, EventArgs e)
   {

    //here is the problem add ! in font 

    if(!String.IsNullOrWhiteSpace(txtName)) // This will prevent exception when textbox is empty   
    {
    if (!System.Text.RegularExpressions.Regex.IsMatch(txtName.Text, "^[a-zA-Z]+$"))
        {
            MessageBox.Show("Enter Valid Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            txtName.Text.Remove(txtName.Text.Length - 1);
            txtName.Clear();
            txtName.Focus();
         }
    }
   }

【讨论】:

  • 当我输入无效名称时,它可以正常工作,但是当文本框变空时,它会抛出错误“StartIndex 不能小于零。参数名称:startIndex”我认为这是由于文本长度。那么接下来该怎么解决呢。
  • 您能否举例说明,据我所知,删除此 txtName.Text.Remove(txtName.Text.Length - 1);如果不需要,请从代码中获取。
  • 现在这也检查空文本框并显示消息框两次。
  • 在此检查中包含 if 条件 if(!String.IsNullOrWhiteSpace(txtName.Text)) // 这将防止文本框为空时出现异常 { // 您的代码在其中。 }
  • 检查 txtName.Text 是否为空或空格。我忘记在检查中添加 .Text
【解决方案2】:
if (!System.Text.RegularExpressions.Regex.IsMatch(textBox1.Text, "^[a-zA-Z]"))
{
    MessageBox.Show("This textbox accepts only alphabetical characters");
    textBox1.Text.Remove(textBox1.Text.Length - 1);
}

【讨论】:

    【解决方案3】:

    我认为问题在于,您还希望支持空格(例如您的示例 Kamran Khan)。在这种情况下,您的表达方式必须是:

    [a-zA-Z ]+
    

    (注意右括号前的空格)

    同时做一个txtName.Text.Remove(txtName.Text.Length - 1); 是非常脆弱的。如果用户在文本的开头输入一个数字会发生什么?

    【讨论】:

      【解决方案4】:

      最好不要使用正则表达式,而是检查字符串中是否包含任何数字。

      一个解决方案如下,它使用System.Linq

      if(txtName.Text.Any(char.IsDigit))
          // Contains numbers
      

      附带说明:给孩子取一个包含数字的名字并非完全违法

      当前正则表达式不匹配的名称示例

      • 玛丽-简
      • 弗雷德里克
      • 三月

      【讨论】:

        【解决方案5】:

        如果您想获得 Regex 免费版本,那么最简单的方法是像您一样处理 TextChanged 事件,但请检查输入的内容。也适用于“粘贴”。

        string oldText = string.Empty;
        private void txtName_TextChanged(object sender, EventArgs e)
        {
            if (txtName.Text.All(chr => char.IsLetter(chr)))
            {
                oldText = txtName.Text;
                txtName.Text = oldText;
        
                txtName.BackColor = System.Drawing.Color.White;
                txtName.ForeColor = System.Drawing.Color.Black;
            }
            else
            {
                txtName.Text = oldText;
                txtName.BackColor = System.Drawing.Color.Red;
                txtName.ForeColor = System.Drawing.Color.White;
            }
            txtName.SelectionStart = txtName.Text.Length;
        }
        

        【讨论】:

          【解决方案6】:

          你只需要

          private void TextBox_KeyUp(object sender, KeyEventArgs e)
          {
               e.Handled = char.IsLetter((char) e.Key);
          }
          

          【讨论】:

            猜你喜欢
            • 2010-10-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-12-16
            • 1970-01-01
            相关资源
            最近更新 更多