检查字符串是否包含空格和字母以外的任何内容 [英] check if a string contains anything except spaces and letters

查看:88
本文介绍了检查字符串是否包含空格和字母以外的任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开展一个项目,我需要对 仅允许字母和空格进行验证

除了应返回false之外的任何内容。我怎么能这样做?

i有我的工作代码

  bool  usernamecheckchar = txboxName.Text.Any(x = > !char.IsLetter(x)); 
if (usernamecheckchar == true
{
// 输入的文字可能只包含空格和字符
bool usernamecheckspaces = txboxName.Text.Contains( );
if (usernamecheckspaces == true
{

}
其他
{

}
}
else
{

}

解决方案

使用regex:

  if (!Regex.Match(txboxName.Text, @  [^ \sa-zA-Z])。成功)
{
// 仅限空格和字母
...
}


i am working on a project where i need to make validation to allow only letters and spaces
anything except that should return false.. how can i do this ?
i have my working code

bool usernamecheckchar = txboxName.Text.Any(x => !char.IsLetter(x));
            if (usernamecheckchar == true)
            {
                //entered text may contain spaces and characters only
                bool usernamecheckspaces = txboxName.Text.Contains(" ");
                if (usernamecheckspaces == true)
                {

                }
                else
                {

                }
            }
            else
            {

            }

解决方案

Use a regex:

if (!Regex.Match(txboxName.Text, @"[^\sa-zA-Z]").Success)
   {
   // Spaces and alphabetics only
   ...
   }


这篇关于检查字符串是否包含空格和字母以外的任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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