• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 408
  • Last Modified:

Checking for invalid characters in string

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>");
      }
      sb.Append("</table>");
            
      if (sFrom == "")
      {
      Response.Redirect("contact.html");
      }
      else{
      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";
      SmtpMail.Send(mail);
      }
}
</script>
0
thelink12
Asked:
thelink12
1 Solution
 
dkloeckCommented:

            string name;
           if( name.Contains("@"))
{
     MessageBox.show("name contains @");
}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now