如何防止用户在文本框中键入特殊字符 [英] How to prevent users from typing special characters in textbox

查看:38
本文介绍了如何防止用户在文本框中键入特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要验证文本框上的密码条目,我有一些要求需要完成以允许创建用户配置文件,其中之一是如果密码包含除数字以外的任何其他内容和系统需要拒绝输入的字母,我尝试的一切似乎都失败了.这是我现在站的地方:

I need to validate a password entry on a textbox, I have a few demands to fullfill in order to allow the user profile to be created and one of them is to refuse registration if the password contains anything else different than numbers and the alphabet letters the system needs to deny the entry, everything I tried seems to fail. Here is where I'm standing right now:

     private void BUT_Signup_Click(object sender, EventArgs e)
    {
     string allowedchar = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

    if (!(TXT_NewPassword.Text.ToString().Contains(allowedchar)))
                    MessageBox.Show("No special characters on the password are allowed");

推荐答案

你可以使用 LINQ:

You could use LINQ:

if (!TXT_NewPassword.Text.All(allowedchar.Contains))
{
    // Not allowed char detected
}

这篇关于如何防止用户在文本框中键入特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆