JEYAGANESH

JEYAGANESH
Software Developer

Tuesday, 6 November 2012

Enquiry / Contact Form with Javascript Validation Using Asp.net c#


Contact.aspx
-------------------

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>::Contact / Enquiry Page with JavaScript Form Validation:: Jeyaganesh asp.net Developer</title>
    <script language="javascript" type="text/javascript">
   function validate() {
       if (document.getElementById("<%=TextBox1.ClientID%>").value == "") {
           alert("Name Feild can not be blank");
           document.getElementById("<%=TextBox1.ClientID%>").focus();
           return false;
       }

       if (document.getElementById("<%=TextBox2.ClientID %>").value == "") {
           alert("Email id Feild can not be blank");
           document.getElementById("<%=TextBox2.ClientID %>").focus();
           return false;
       }

       if (document.getElementById("<%=TextBox3.ClientID %>").value == "") {
           alert("Mobile No Feild can not be blank");
           document.getElementById("<%=TextBox3.ClientID %>").focus();
           return false;
       }
       if (document.getElementById("<%=TextBox4.ClientID %>").value == "") {
           alert("Feedback Feild can not be blank");
           document.getElementById("<%=TextBox3.ClientID %>").focus();
           return false;
       }
       var emailPat = /^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;
       var emailid = document.getElementById("<%=TextBox2.ClientID %>").value;
       var matchArray = emailid.match(emailPat);
       if (matchArray == null) {
           alert("Your Email Address Seems Incorrect. Please try again.");
           document.getElementById("<%=TextBox2.ClientID %>").focus();
           return false;
       }


       return true;
   }
</script>
</head>
<body>
    <form id="form1" action="" runat="server">
<table width="500" border="0" style="text-align:left; font-size:16px; color:#C6913D; font-weight:bold;">
  <tr>
    <td align="left">Enquiry Form</td>
    <td></td>
  </tr>
  <tr>
    <td>Name</td>
    <td>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
  </tr>
  <tr>
    <td>Email Id</td>
    <td><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>
  </tr>
  <tr>
    <td>Mobile No</td>
    <td><asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></td>
  </tr>
  <tr>
    <td>Comments</td>
    <td><asp:TextBox ID="TextBox4" runat="server" TextMode="MultiLine" Width="300px" Height="150px"></asp:TextBox></td>
  </tr>

  <tr>
    <td></td>
    <td><br/>
        <asp:Button ID="Button1" runat="server" Text="Submit" class="learn-more"
             OnClientClick="return validate()" onclick="Button1_Click"/>
    <%--<a title="Submit" class="learn-more" href="#">Submit</a>--%></td>
  </tr>
</table>
</form>
</body>
</html>

Contact.aspx.cs
------------------


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Net.Mail;
using System.Net;
using System.Threading;
using System.IO;
using System.Text;
using System.Data.SqlClient;


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

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {

            MailMessage mail1 = new MailMessage();
            mail1.To.Add("to mailid");
            mail1.From = new MailAddress("fromemailid@hotmail.com");
            mail1.Subject = "Feed Back On " + DateTime.Now.ToString();
            mail1.IsBodyHtml = true;
            string Body1;

            Body1 = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";
            Body1 += "<title></title>"

        + "</head>"
        + "<body>"

        + "<table align=\"center\" style=\"width: 650px; height: 377px;\">"
        + "<tr>"
        + "<td>"
        + "Name"
        + "</td>"
        + "<td align=\"center\">:</td>"
        + "<td>"
        + TextBox1.Text
        + "</td>"
       + "</tr>"
        + "<tr>"
       + "<td>"
       + "</td>"
       + "<td align=\"center\"></td>"
       + "<td>"
       + "</td>"
       + "</tr>"

        + "<tr>"
       + "<td>"
       + "</td>"
       + "<td align=\"center\"></td>"
       + "<td>"
       + "</td>"
       + "</tr>"

       + "<tr>"
       + "<td>"
       + "Email Id</td>"
       + "<td align=\"center\">:</td>"
       + "<td>"
       + TextBox2.Text + "</td>"
       + "</tr>"

       + "<tr>"
       + "<td>"
       + "</td>"
       + "<td align=\"center\"></td>"
       + "<td>"
       + "</td>"
       + "</tr>"
        + "<tr>"
       + "<td>"
       + "</td>"
       + "<td align=\"center\"></td>"
       + "<td>"
       + "</td>"
       + "</tr>"

       + "<tr>"
       + "<td>"
       + "Mobile No</td>"
       + "<td align=\"center\">:</td>"
       + "<td>"
       + TextBox3.Text + "</td>"
       + "</tr>"

        + "<tr>"
       + "<td>"
       + "</td>"
       + "<td align=\"center\"></td>"
       + "<td>"
       + "</td>"
       + "</tr>"
        + "<tr>"
       + "<td>"
       + "</td>"
       + "<td align=\"center\"></td>"
       + "<td>"
       + "</td>"
       + "</tr>"

       + "<tr>"
       + "<td>"
       + "Feedback</td>"
       + "<td align=\"center\">:</td>"
       + "<td>"
       + TextBox4.Text + "</td>"
       + "</tr>"

       + "</table>"
       + "</body>"
       + "</html>";


            mail1.Body = Body1;
            // mail.Body += Body.ToString();      
            mail1.IsBodyHtml = true;

            mail1.Priority = MailPriority.Normal;
            SmtpClient smtp1 = new SmtpClient();
            smtp1.Port = 25;
            smtp1.Host = "smtp.live.com";
            smtp1.Credentials = new System.Net.NetworkCredential("fromemailid@hotmail.com", "password");
            smtp1.EnableSsl = true;
            smtp1.Send(mail1);

           ClientScript.RegisterStartupScript(this.GetType(), "Alert", "<script>alert('Your Information Sending Successfully');</script>");

        }
        catch (Exception ex)
        {
             ClientScript.RegisterStartupScript(this.GetType(), "Alert", "<script>alert('Email Not Sent');</script>");
        }
    }
}

You can set your own domain mailid and Password:

SET Port No:25

SET Smtp host: mail.yourdomain.com or webmail.yourdomain.com (EX:mail.ganesh.com)

SET EnableSsl=false




JAVASCRIPT FORM VALIDATION FOR ASP.NET WEB FORM


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>::JavaScript Form Validation:: Jeyaganesh asp.net Developer</title>
    <script language="javascript" type="text/javascript">
   function validate() {
       if (document.getElementById("<%=TextBox1.ClientID%>").value == "") {
           alert("Name Feild can not be blank");
           document.getElementById("<%=TextBox1.ClientID%>").focus();
           return false;
       }

       if (document.getElementById("<%=TextBox2.ClientID %>").value == "") {
           alert("Email id Feild can not be blank");
           document.getElementById("<%=TextBox2.ClientID %>").focus();
           return false;
       }

       if (document.getElementById("<%=TextBox3.ClientID %>").value == "") {
           alert("Mobile No Feild can not be blank");
           document.getElementById("<%=TextBox3.ClientID %>").focus();
           return false;
       }
       if (document.getElementById("<%=TextBox4.ClientID %>").value == "") {
           alert("Feedback Feild can not be blank");
           document.getElementById("<%=TextBox3.ClientID %>").focus();
           return false;
       }
       var emailPat = /^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;
       var emailid = document.getElementById("<%=TextBox2.ClientID %>").value;
       var matchArray = emailid.match(emailPat);
       if (matchArray == null) {
           alert("Your Email Address Seems Incorrect. Please try again.");
           document.getElementById("<%=TextBox2.ClientID %>").focus();
           return false;
       }


       return true;
   }
</script>
</head>
<body>
    <form id="form1" action="" runat="server">
<table width="500" border="0" style="text-align:left; font-size:16px; color:#C6913D; font-weight:bold;">
  <tr>
    <td align="left">Enquiry Form</td>
    <td></td>
  </tr>
  <tr>
    <td>Name</td>
    <td>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
  </tr>
  <tr>
    <td>Email Id</td>
    <td><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>
  </tr>
  <tr>
    <td>Mobile No</td>
    <td><asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></td>
  </tr>
  <tr>
    <td>Comments</td>
    <td><asp:TextBox ID="TextBox4" runat="server" TextMode="MultiLine" Width="300px" Height="150px"></asp:TextBox></td>
  </tr>

  <tr>
    <td></td>
    <td><br/>
        <asp:Button ID="Button1" runat="server" Text="Submit"
             OnClientClick="return validate()"/>
    </td>
  </tr>
</table>
</form>
</body>
</html>