I am very new to web programming stumbled onto the webmatrix program and it gave me alot of very good example code. The only problem is i found my self getting razor and asp.net confused alot in the syntax. My goal is to have a form validate and then send
via smtp.
I am getting hung up when it comes to validating Multiple Fields. I am really looking for a example of 2 form fields that validate. Here is what i did so far. I am very new to coding so please take it easy on me. This page works the best out of all that
i wrote but on submit it validates all fields but not correctly and clears all fields saying they are all wrong even if only a 1 or 2 are wrong and shoots errors on all fields. (This code is probably horriable written i apologized in advance . Any feed back
would be greatly appreciated.)
@using System.Text.RegularExpressions;
@{
Layout = "~/Shared/_SiteLayout.cshtml";
Page.Title = "Contact";
}
<div id="featured_content">
<hgroup class="title">
<h1>@Page.Title.</h1>
<h2>Tell us your thoughts!</h2>
</hgroup>
@{
var valer = false;
var error = "Please enter your name";
var errorTwo = "Subject is a required field";
var errorThree = "E-mail is a required field";
var errorFour = "Message is a required field";
var errorSubL = "Subject Can only be 30 Characters Long.";
var contactdatetime = DateTime.Now;
var nme = "";
var sub = "";
var email ="";
var msg = "";
if(IsPost)
{
nme = Request["name"];
sub = Request["subject"];
email = Request["email"];
msg = Request["message"];
if (nme.IsEmpty() || nme == "Name:")
{
valer = true;
}
if(sub.IsEmpty() || sub == "Subject:")
{
valer = true;
}
if(email.IsEmpty() || email == "E-mail:" )
{
valer = true;
}if(msg.IsEmpty() || msg == "Message.."){
valer = true;
} if(valer == false)
{
WebMail.SmtpServer = "";
WebMail.SmtpPort = 587;
WebMail.EnableSsl = true;
WebMail.UserName = "";
WebMail.Password = "";
WebMail.From = "";
if (WebMail.UserName.IsEmpty() || WebMail.Password.IsEmpty() || WebMail.From.IsEmpty()) {
Response.Redirect("~/Success/ContactUsSuccess?NoEmail=1");
}
else {
try {
WebMail.Send(to: "",
subject:"Subject message of the User"+ sub +"<br />" ,
body:"The name of user:"+ nme + "<br/> The E-Mail Address of the end user:@nbsp" + email + "<br /> The Subject Line:@nbsp" + sub + "<br />The Message:@nbsp" + msg +"Time of transmission:@nbsp"+contactdatetime);
Response.Redirect("~/Success/ContactUsSuccess");
} catch {
ModelState.AddFormError("There was an error and your request could not be processed at this time");
}
}
}
}
}
<form id="contact-form" method="post" enctype="multipart/form-data" action="">
<fieldset>
<h3> Having Trouble? Let us Help. Tell us your question below!</h3>
<p>
<label for="name">Name:</label>
<input type="text" name="name" value="Name:" maxlength="30"
onblur="if(this.value=='') this.value='Name:'"
onfocus="if(this.value=='Name:') this.value=''" />
@if (valer) {
<label for="name" class="message error">@error</label>
}
</p>
<p>
<label for="subject">Subject:</label>
<input type="text" name="subject" value="Subject:"
onblur="if(this.value=='') this.value='Subject:'"
onfocus="if(this.value=='Subject:') this.value=''" />
@if (valer) {
<label for="subject" class="message error">@errorTwo</label>
}
</p>
<p>
<label for="email">E-mail:</label>
<input type="text" name="email" value="E-mail:" maxlength="50"
onblur="if(this.value=='') this.value='E-mail:'"
onfocus="if(this.value=='E-mail:') this.value=''" />
@if (valer) {
<label for="email" class="message error">@errorThree</label>
}
</p>
<p>
<label for="message">Message:</label>
<textarea name="message" cols=40 rows=6 value="Message.." maxlength="50"
onblur="if(this.value=='') this.value='Message..'"
onfocus="if(this.value=='Message..') this.value=''"></textarea>
@if (valer) {
<label for="message" class="message error">@errorFour</label>
}
</p>
<p>
<input type="submit" value="Submit" title="Register" />
</p>
</fieldset>
</form>
This page works the best out of all that i wrote but on submit it validates all fields but not correctly and clears all fields saying they are all wrong even if only a 1 or 2 are wrong and shoots errors on all fields.
The page marks all fields as wrong because displays all error messages if valer is true.
Furthermore, it clears all fields because the values of all input tags are initializated every time the page is refreshed.
I try to give you some suggestions with the code that follows:
@{
Layout = "~/Shared/_SiteLayout.cshtml";
Page.Title = "Contact";
var valer = false;
var error = "";
var errorTwo = "";
// ...
var nme = "Name:";
var sub = "Subject:";
// ...
if(IsPost)
{
nme = Request["name"];
sub = Request["subject"];
// ...
if (nme.IsEmpty() || nme == "Name:")
{
valer = true;
error = "Please enter your name";
}
if(sub.IsEmpty() || sub == "Subject:")
{
valer = true;
errorTwo = "Subject is a required field";
}
//...
}
}
<div id="featured_content">
<hgroup class="title">
<h1>@Page.Title.</h1>
<h2>Tell us your thoughts!</h2>
</hgroup>
<form id="contact-form" method="post" enctype="multipart/form-data" action="">
<fieldset>
<h3> Having Trouble? Let us Help. Tell us your question below!</h3>
<p>
<label for="name">Name:</label>
<input type="text" name="name" value="@nme" maxlength="30"
onblur="if(this.value=='') this.value='Name:'"
onfocus="if(this.value=='Name:') this.value=''" />
@if (valer) {
<label for="name" class="message error">@error</label>
}
</p>
<p>
<label for="subject">Subject:</label>
<input type="text" name="subject" value="@sub"
onblur="if(this.value=='') this.value='Subject:'"
onfocus="if(this.value=='Subject:') this.value=''" />
@if (valer) {
<label for="subject" class="message error">@errorTwo</label>
}
</p>
<!-- ... -->
<p>
<input type="submit" value="Submit" title="Register" />
</p>
</fieldset>
</form>
</div>
rterysen
I am very new to web programming stumbled onto the webmatrix program and it gave me alot of very good example code. The only problem is i found my self getting razor and asp.net confused alot in the syntax. My goal is to have a form validate and then send via
smtp.
Maybe the goal is a little too challenging for your first attempt.
Marked as answer by Dino He - MSFT on May 21, 2012 07:23 AM
rterysen
0 Points
6 Posts
form validation with WebMatrix
May 14, 2012 08:44 PM|LINK
Hello Everyone,
I am very new to web programming stumbled onto the webmatrix program and it gave me alot of very good example code. The only problem is i found my self getting razor and asp.net confused alot in the syntax. My goal is to have a form validate and then send via smtp.
I am getting hung up when it comes to validating Multiple Fields. I am really looking for a example of 2 form fields that validate. Here is what i did so far. I am very new to coding so please take it easy on me. This page works the best out of all that i wrote but on submit it validates all fields but not correctly and clears all fields saying they are all wrong even if only a 1 or 2 are wrong and shoots errors on all fields. (This code is probably horriable written i apologized in advance . Any feed back would be greatly appreciated.)
@using System.Text.RegularExpressions; @{ Layout = "~/Shared/_SiteLayout.cshtml"; Page.Title = "Contact"; } <div id="featured_content"> <hgroup class="title"> <h1>@Page.Title.</h1> <h2>Tell us your thoughts!</h2> </hgroup> @{ var valer = false; var error = "Please enter your name"; var errorTwo = "Subject is a required field"; var errorThree = "E-mail is a required field"; var errorFour = "Message is a required field"; var errorSubL = "Subject Can only be 30 Characters Long."; var contactdatetime = DateTime.Now; var nme = ""; var sub = ""; var email =""; var msg = ""; if(IsPost) { nme = Request["name"]; sub = Request["subject"]; email = Request["email"]; msg = Request["message"]; if (nme.IsEmpty() || nme == "Name:") { valer = true; } if(sub.IsEmpty() || sub == "Subject:") { valer = true; } if(email.IsEmpty() || email == "E-mail:" ) { valer = true; }if(msg.IsEmpty() || msg == "Message.."){ valer = true; } if(valer == false) { WebMail.SmtpServer = ""; WebMail.SmtpPort = 587; WebMail.EnableSsl = true; WebMail.UserName = ""; WebMail.Password = ""; WebMail.From = ""; if (WebMail.UserName.IsEmpty() || WebMail.Password.IsEmpty() || WebMail.From.IsEmpty()) { Response.Redirect("~/Success/ContactUsSuccess?NoEmail=1"); } else { try { WebMail.Send(to: "", subject:"Subject message of the User"+ sub +"<br />" , body:"The name of user:"+ nme + "<br/> The E-Mail Address of the end user:@nbsp" + email + "<br /> The Subject Line:@nbsp" + sub + "<br />The Message:@nbsp" + msg +"Time of transmission:@nbsp"+contactdatetime); Response.Redirect("~/Success/ContactUsSuccess"); } catch { ModelState.AddFormError("There was an error and your request could not be processed at this time"); } } } } } <form id="contact-form" method="post" enctype="multipart/form-data" action=""> <fieldset> <h3> Having Trouble? Let us Help. Tell us your question below!</h3> <p> <label for="name">Name:</label> <input type="text" name="name" value="Name:" maxlength="30" onblur="if(this.value=='') this.value='Name:'" onfocus="if(this.value=='Name:') this.value=''" /> @if (valer) { <label for="name" class="message error">@error</label> } </p> <p> <label for="subject">Subject:</label> <input type="text" name="subject" value="Subject:" onblur="if(this.value=='') this.value='Subject:'" onfocus="if(this.value=='Subject:') this.value=''" /> @if (valer) { <label for="subject" class="message error">@errorTwo</label> } </p> <p> <label for="email">E-mail:</label> <input type="text" name="email" value="E-mail:" maxlength="50" onblur="if(this.value=='') this.value='E-mail:'" onfocus="if(this.value=='E-mail:') this.value=''" /> @if (valer) { <label for="email" class="message error">@errorThree</label> } </p> <p> <label for="message">Message:</label> <textarea name="message" cols=40 rows=6 value="Message.." maxlength="50" onblur="if(this.value=='') this.value='Message..'" onfocus="if(this.value=='Message..') this.value=''"></textarea> @if (valer) { <label for="message" class="message error">@errorFour</label> } </p> <p> <input type="submit" value="Submit" title="Register" /> </p> </fieldset> </form>GmGregori
Contributor
5564 Points
749 Posts
Re: form validation with WebMatrix
May 14, 2012 10:01 PM|LINK
The page marks all fields as wrong because displays all error messages if valer is true.
Furthermore, it clears all fields because the values of all input tags are initializated every time the page is refreshed.
I try to give you some suggestions with the code that follows:
@{ Layout = "~/Shared/_SiteLayout.cshtml"; Page.Title = "Contact"; var valer = false; var error = ""; var errorTwo = ""; // ... var nme = "Name:"; var sub = "Subject:"; // ... if(IsPost) { nme = Request["name"]; sub = Request["subject"]; // ... if (nme.IsEmpty() || nme == "Name:") { valer = true; error = "Please enter your name"; } if(sub.IsEmpty() || sub == "Subject:") { valer = true; errorTwo = "Subject is a required field"; } //... } } <div id="featured_content"> <hgroup class="title"> <h1>@Page.Title.</h1> <h2>Tell us your thoughts!</h2> </hgroup> <form id="contact-form" method="post" enctype="multipart/form-data" action=""> <fieldset> <h3> Having Trouble? Let us Help. Tell us your question below!</h3> <p> <label for="name">Name:</label> <input type="text" name="name" value="@nme" maxlength="30" onblur="if(this.value=='') this.value='Name:'" onfocus="if(this.value=='Name:') this.value=''" /> @if (valer) { <label for="name" class="message error">@error</label> } </p> <p> <label for="subject">Subject:</label> <input type="text" name="subject" value="@sub" onblur="if(this.value=='') this.value='Subject:'" onfocus="if(this.value=='Subject:') this.value=''" /> @if (valer) { <label for="subject" class="message error">@errorTwo</label> } </p> <!-- ... --> <p> <input type="submit" value="Submit" title="Register" /> </p> </fieldset> </form> </div>Maybe the goal is a little too challenging for your first attempt.