We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


Checking for invalid characters in string

thelink12 asked
Medium Priority
Last Modified: 2010-04-16
Below is the code on my Handler page for a web form. How can I check the non-email fields to make sure they do not contain an '@'. The incoming fields/keys I want to check are name, address, company. Thanks.

<script language="C#" runat="server">

public StringBuilder sb;
private void Page_Load(object sender, System.EventArgs e)
      Response.CacheControl = "no-cache";
      Response.Expires = -1;

      string sFrom = "";
      sb = new StringBuilder("<table border=\"0\">");

      // get values from post
      NameValueCollection coll = Request.Form;
      String[] arr1 = coll.AllKeys;

      for (int i = 0; i < arr1.Length; i++)
            string key = arr1[i];
            // prevent script insertion
            string val = coll[key].ToString().Trim().Replace("<"," ").Replace(">"," ");
            if (key == "email" && val != "")
            sFrom = val;
            sb.Append("<tr><td>" + key + ":</td><td>" + val + "</td></tr>");
      if (sFrom == "")
      MailMessage mail = new MailMessage();
      mail.To = "webmaster@abc.com";
      mail.From = sFrom;
      mail.Subject = "Website Contact";
      mail.BodyFormat = MailFormat.Html;
      mail.Body = "<html><body>" + sb.ToString() + "</body></html>";
//      mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
//      mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "");
//      mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "");
      SmtpMail.SmtpServer = "EXCH.DOMAIN.LOCAL";
Watch Question


            string name;
           if( name.Contains("@"))
     MessageBox.show("name contains @");

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.