Checking for invalid characters in string

Posted on 2006-03-22
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 = "";
      mail.From = sFrom;
      mail.Subject = "Website Contact";
      mail.BodyFormat = MailFormat.Html;
      mail.Body = "<html><body>" + sb.ToString() + "</body></html>";
//      mail.Fields.Add("", "1");
//      mail.Fields.Add("", "");
//      mail.Fields.Add("", "");
      SmtpMail.SmtpServer = "EXCH.DOMAIN.LOCAL";
Question by:thelink12
    1 Comment
    LVL 10

    Accepted Solution


                string name;
               if( name.Contains("@"))
    {"name contains @");

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
    We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now