Checking for invalid characters in string

Posted on 2006-03-22
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";
Question by:thelink12
1 Comment
LVL 10

Accepted Solution

dkloeck earned 1000 total points
ID: 16262806

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

839 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