asp.net - Contact form send even if the field(s) are empty -



asp.net - Contact form send even if the field(s) are empty -

please help...

i got below code net , don't know how prepare issue.

issue is:- if form empty or 1 of fields empty, still receive email.

i think need add together validation here don't know how. not programmer , new asp.

here 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 come in 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 come in valid email address." display="dynamic" /> <asp:requiredfieldvalidator id="requiredfieldvalidator3" runat="server" errormessage="please come in 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 come in 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 :

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("email1@domain.com", "email2@domain.com"); 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 = "email1@domain.com"; networkcred.password = "emai1password"; smtp.usedefaultcredentials = true; smtp.credentials = networkcred; smtp.port = 587; smtp.send(mm); lblmessage.text = "email sent sucessfully."; } }

your problem here :

<asp:button id="btnsend" runat="server" text="send" onclick="btnsend_click" cssclass="form-buttons"/>

use instead : causesvalidation="true" in button. validate required field before post event fired.

<asp:button id="btnsend" runat="server" text="send" onclick="btnsend_click" cssclass="form-buttons" causesvalidation="true"/>

asp.net

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -