方法一:(正则表达式)   

     /*
         * @author 李效伦
         *
         * 判断一个字符串是不是合法
         */   

        public bool Islegal()
        {
            Regex regExp = new Regex("[~!@#$%^&*()=+[\\]{}''\";:/?.,><`|!·¥…—()\\-、;:。,》《]");
            return !regExp.IsMatch(txtNickName.Text.Trim());
        }

方法二

         /*
         * @author 李效伦
         *
         * 判断一个字符串是不是合法
         */
        private bool Islegal2()
        {
            char[] charstr = { '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '<', '>', '?', ':', '"', '{', '}' };
            char[] textstr = txtNickName.Text.ToString().Trim().ToCharArray();
            int count = 0;/*统计非法字符的个数*/
            for (int i = 0; i < charstr.Length; i++)
            {
                for (int j = 0; j < textstr.Length; j++)
                {
                    if (charstr[i] == textstr[j])
                    {
                        count++;
                    }
                }
            }
            if (count > 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

相关文章:

  • 2021-07-19
  • 2022-12-23
  • 2021-06-20
  • 2022-12-23
  • 2022-01-07
猜你喜欢
  • 2021-07-25
  • 2022-12-23
  • 2022-01-07
  • 2022-01-08
  • 2022-12-23
  • 2021-12-28
  • 2022-12-23
相关资源
相似解决方案