简单的验证字符串中是否包含特殊符号

            string keyword = "tom%";//或者是传来的name值
            if ("%*.~><=-".IndexOf(keyword) >= 0)
            {
                MessageBox.Show("您输入的内容中包含特殊字符。");
                return false;
            }

 或者这样

      protected void Page_Load(object sender, EventArgs e)
        {
           if (checkstring("tom%"))
            {
                MessageBox.Show("地址出现特殊符号,发生错误!");
                return;
            }
        }   

      public bool checkstring(string str)
        {
            string[] Er = { "/", "*", "<", "," };
            char[] s = str.ToCharArray();
            int len = Er.Length;
            bool btrue = false;
            for (int i = 0; i < s.Length; i++)
            {
                for (int num = 0; num < len; num++)
                {
                    if (s[i].ToString() == Er[num])
                    {
                        btrue = true;
                        break;
                    }
                }
            }
            return btrue;
        }
原文地址:https://www.cnblogs.com/zwnet/p/2648813.html