user3766081
user3766081

Reputation: 23

Contact form send even if the field(s) are empty

Please help...

I just got the below code from the internet and don't know how to fix the issue.

Issue is:- even if the form is empty or one of the fields is empty, I still receive the email.

I think I need to add some validation here but I don't know how. I am not a programmer and new to asp.

Here is the code.

the ASPX is:

<%@ Page Language="C#" AutoEventWireup="true" ValidateRequest = "false" CodeFile="contact.aspx.cs" Inherits="_Default" %>

<html>
<head runat="server">
</head>

<body>

            <form id="form1" runat="server">

                <div class="inp_title"><asp:Label ID="Label1" runat="server" Text="Name*" /></div>
                <div>
                    <asp:TextBox ID="txtName" runat="server" ValidationGroup = "contact" CssClass="inp_h" /><br />
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please enter your name." ControlToValidate = "txtName" Display="Dynamic" /></div>

                <div class="inp_title"><asp:Label ID="Label2" runat="server" Text="Subject*" /></div>
                <div>
                    <asp:TextBox ID="txtSubject" runat="server" /><br />
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="*" ControlToValidate = "txtSubject" Display="Dynamic" /></div>



                <div class="inp_title"><asp:Label ID="Label3" runat="server" Text="Email Address*" /></div>
                <div>
                    <asp:TextBox ID="txtEmail" runat="server" CssClass="inp_h" Display="Dynamic" /><br />
                    <asp:RegularExpressionValidator id="valRegEx" runat="server" ControlToValidate="txtEmail" ValidationExpression=".*@.*\..*" ErrorMessage="Please enter a valid email address." Display="Dynamic" />
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Please enter your email address." ControlToValidate = "txtEmail" />
                </div>


                <div class="inp_title-2"><asp:Label ID="Label4" runat="server" Text="Message*" /></div>
                <div>
                    <asp:TextBox ID="txtBody" runat="server" TextMode = "MultiLine" CssClass="inp_h" Display="Dynamic" /><br />
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="Please enter your message." ControlToValidate = "txtBody" />
                </div>


                <div class="inp_h"><asp:FileUpload ID="FileUpload1" runat="server" /></div>

                <asp:Button ID="btnSend" runat="server" Text="Send" OnClick="btnSend_Click" CssClass="form-buttons"/>

                <asp:Label ID="lblMessage" runat="server" Text="" ForeColor = "Green" />

            </form>

</body>
</html>

The code-behind is :

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Net.Mail;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
protected void btnSend_Click(object sender, EventArgs e)
{
    MailMessage mm = new MailMessage("[email protected]", "[email protected]");
    mm.Subject = txtSubject.Text;
    mm.Body = "Name: " + txtName.Text + "<br /><br />Email: " + txtEmail.Text + "<br /><br />Message:<br />" + txtBody.Text;
    if (FileUpload1.HasFile)
    {
        string FileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName)   ; 
        mm.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileName)); 
    }
    mm.IsBodyHtml = true;
    SmtpClient smtp = new SmtpClient();
    smtp.Host = "smtp.office365.com";
    smtp.EnableSsl = true;
    System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
    NetworkCred.UserName = "[email protected]";
    NetworkCred.Password = "emai1password";
    smtp.UseDefaultCredentials = true;
    smtp.Credentials = NetworkCred;
    smtp.Port = 587;
    smtp.Send(mm);
    lblMessage.Text = "Email Sent Sucessfully.";
}
}

Upvotes: 2

Views: 364

Answers (4)

user3768047
user3768047

Reputation: 11

just use

CausesValidation="true"

inside your button control, this will validate your controls client side. Like I said :

<asp:Button ... CausesValidation="true"/>

or you can validate it at server side too.

Upvotes: 0

Denny
Denny

Reputation: 1

You need to have a required field validator for the textbox

Upvotes: 0

Evonet
Evonet

Reputation: 3640

Here you go, this will not send the email if the fields have not been filled out if you're not using validation - otherwise just add 'causesvalidation=true' to your submit button as per the earlier answer

protected void btnSend_Click(object sender, EventArgs e)
{
    if (!string.IsNullOrWhiteSpace(txtName.Text) && !string.IsNullOrWhiteSpace(txtEmail.Text) && !string.IsNullOrWhiteSpace(txtBody.Text) )
    {
        MailMessage mm = new MailMessage("[email protected]", "[email protected]");
        mm.Subject = txtSubject.Text;
        mm.Body = "Name: " + txtName.Text + "<br /><br />Email: " + txtEmail.Text + "<br /><br />Message:<br />" + txtBody.Text;
        if (FileUpload1.HasFile)
        {
            string FileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName)       ; 
            mm.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileName)); 
        }
        mm.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.office365.com";
        smtp.EnableSsl = true;
        System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
        NetworkCred.UserName = "[email protected]";
        NetworkCred.Password = "emai1password";
        smtp.UseDefaultCredentials = true;
        smtp.Credentials = NetworkCred;
        smtp.Port = 587;
        smtp.Send(mm);
        lblMessage.Text = "Email Sent Sucessfully.";
    }
    else
    {
        lblMessage.Text = "You need to fill in the fields.";
    }
}

Upvotes: 1

Krunal Patil
Krunal Patil

Reputation: 3676

your problem is here :

<asp:Button ID="btnSend" runat="server" Text="Send" OnClick="btnSend_Click" CssClass="form-buttons"/>

use this instead : CausesValidation="true" in your button. This will validate your required field before post event is fired.

<asp:Button ID="btnSend" runat="server" Text="Send" OnClick="btnSend_Click" CssClass="form-buttons" CausesValidation="true"/>

Upvotes: 0

Related Questions