 |
BOOK: Beginning ASP.NET Web Pages with WebMatrix  | This is the forum to discuss the Wrox book Beginning ASP.NET Web Pages with WebMatrix by Mike Brind, Imar Spaanjaars ; ISBN: 978-1-1180-5048-4 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Beginning ASP.NET Web Pages with WebMatrix section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

January 4th, 2012, 05:10 PM
|
Authorized User
|
|
Join Date: Aug 2010
Posts: 50
Thanks: 7
Thanked 0 Times in 0 Posts
|
|
Chapter 5: Validating Form Input
Hi,
I am having issue getting the error message to come up for the email address. When its empty and I select "Post" no error message. Thanks.
Below is my code
Code:
@{
Page.Title="Post Your Advertisement";
if(IsPost){
if(Request["title"].IsEmpty()){
ModelState.AddError("title", "Please provide a title");
}
if(Request["title"].Length > 200){
ModelState.AddError("title","Your title cannot exceed 200 characters");
}
if(Request["description"].IsEmpty()){
ModelState.AddError("description", "You must provide a description");
}
if(Request["duration"].IsEmpty()){
ModelState.AddError("duration", "Please choose a duration");
}
if(Request["price"].IsEmpty()){
ModelState.AddError("price", "Please provide a price");
}
if(Request["price"].IsDecimal () && !Request["Price"].IsEmpty()){
ModelState.AddError("price", "Please provide a valid number for the price");
}
if(Request["condition"].IsEmpty()){
ModelState.AddError("condition", "Please state the condition of your item");
}
if(Request["email"].IsEmpty()){
ModelState.AddError("email", "Please provide your email address");
}
if(!Request["email"].IsEmpty() && !Functions.IsValidEmail(Request["Email"])){
ModelState.AddError("email", "Please provide a valid email address");
}
if(!ModelState.IsValid){
ModelState.AddFormError("Please fix the errors below before submitting the form");
}
}
}
<form id=" ="post-advert" action="@Href("~/Sell")" method="post">
<fieldset>
<legend>Post Your Advertisement</legend>
@Html.ValidationSummary(true)
<div>
<label for="title">Title</label>
</div>
<div>
<input type="text" name="title" value="@Request["title"]" />
@Html.ValidationMessage("title")
</div>
<div>
<label for="description">Description</label>
</div>
<div>
<textarea name="description">@Request["description"]</textarea>
@Html.ValidationMessage("description")
</div>
<div>
<label for="duration">Duration</label>
</div>
<div>
<select name="duration">
<option value="">--Choose One--</option>
<option value="1" @Helpers.Selected("Duration","1")>1 Day</option>
<option value="3" @Helpers.Selected("Duration","3")>3 Days</option>
<option value="7" @Helpers.Selected("Duration","7")>7 Days</option>
<option value="14" @Helpers.Selected("Duration","14")>14 Days</option>
</select>
@Html.ValidationMessage("duration")
</div>
<div>
<label for="price">Price*</label>
</div>
<div>
<input type="text" name="price" value="@Request["price"]" />
@Html.ValidationMessage("price")
</div>
<div>
<label for="condition">Condition</label>
</div>
<div>
<input type="radio" name="condition" value="Fair" @Helpers.Checked("Condition","Fair") />Fair
<input type="radio" name="condition" value="Good" @Helpers.Checked("Condition","Good") />Good
<input type="radio" name="condition" value="As New" @Helpers.Checked("Condition","As New") />As New
@Html.ValidationMessage("condition")
</div>
<div>
<label for="email">Your Email Address</label>
</div>
<div>
<input type="text" name="email" value="@Request["email"]" />
</div>
<div>
<input type="submit" name="submit" value="Post" />
</div>
</fieldset>
</form>
|

January 4th, 2012, 06:37 PM
|
Wrox Author
|
|
Join Date: Dec 2011
Posts: 57
Thanks: 1
Thanked 19 Times in 19 Posts
|
|
You seem to have omitted @Html.ValidationMessage("email"), which is the placeholder for the email validation message. It should appear just under the following line of code:
<input type="text" name="email" value="@Request["email"]" />
|

January 5th, 2012, 04:52 PM
|
Authorized User
|
|
Join Date: Mar 2011
Posts: 74
Thanks: 21
Thanked 2 Times in 2 Posts
|
|
Validating Multiple Similar TextAreas
hozdaman thanks for posting your code above as it will help me figure out a better way to validate some of my TextAreas. I have a 16 question multiple choice online quiz which is validated for the answers, however there are 16 associated comment (TextArea) sections. I want to limit these TextAreas in case somebody enters with a cut and paste "... all work and no play makes Jack a dull boy..." thousands of times. If it CAN be done it WILL be done!
So, is there a better way than writing the code 16 below times (different field names of course)? All work and no play does make Jack a dull boy!
Code:
if(Request["Q1Comments"].Length > 200){
ModelState.AddError("Q1Comments","Your comment cannot exceed 200 characters");
}
|

January 5th, 2012, 04:56 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
If all your controls are number sequentially, you can set up a for loop:
Code:
for (int i = 0; i < 16; i++)
{
if(Request["Q" + i.ToString() + "Comments"].Length > 200)
{
ModelState.AddError("Q" + i.ToString() + "Comments","Your comment cannot exceed 200 characters");
}
}
Alternatively, you can set up an array of field names and loop over that.
Hope this helps,
Imar
|
The Following User Says Thank You to Imar For This Useful Post:
|
|

January 5th, 2012, 05:54 PM
|
Authorized User
|
|
Join Date: Mar 2011
Posts: 74
Thanks: 21
Thanked 2 Times in 2 Posts
|
|
Quote:
Originally Posted by Imar
Hi there,
If all your controls are number sequentially, you can set up a for loop:
Code:
for (int i = 0; i < 16; i++)
{
if(Request["Q" + i.ToString() + "Comments"].Length > 200)
{
ModelState.AddError("Q" + i.ToString() + "Comments","Your comment cannot exceed 200 characters");
}
}
Alternatively, you can set up an array of field names and loop over that.
Hope this helps,
Imar
|
Thanks Imar! Love this WebMatrix book!
|

January 6th, 2012, 11:55 AM
|
Wrox Author
|
|
Join Date: Dec 2011
Posts: 57
Thanks: 1
Thanked 19 Times in 19 Posts
|
|
Or, if all your controls start with or end with the same string in their name, you can use string.StartsWith or string.EndsWith in a foreach loop
Code:
foreach(string item in Request.Form){
if(item.EndsWith("Comments")){
if(Request[item].Length > 200){
ModelState.AddError(item, "Less than 200 characters, please");
}
}
}
|

January 6th, 2012, 01:17 PM
|
Authorized User
|
|
Join Date: Mar 2011
Posts: 74
Thanks: 21
Thanked 2 Times in 2 Posts
|
|
Quote:
Originally Posted by Mike Brind
Or, if all your controls start with or end with the same string in their name, you can use string.StartsWith or string.EndsWith in a foreach loop
Code:
foreach(string item in Request.Form){
if(item.EndsWith("Comments")){
if(Request[item].Length > 200){
ModelState.AddError(item, "Less than 200 characters, please");
}
}
}
|
Thanks! Will try this out. All of my question's comment TextAreas are named Q1Comments, Q2Comments, Q3Comments etc., so they "EndsWith" "Comments" so will try this! Will be interesting to see if the code "splits off" the "Q1", "Q2", "Q3" etc. and runs the logic.
Last edited by jpjamie; January 6th, 2012 at 01:20 PM..
|

November 3rd, 2014, 03:30 PM
|
Registered User
|
|
Join Date: Nov 2014
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
@Helpers problem
@{
Page.Title = "Post Your Advertisement";
if(IsPost){
if(HttpContext.Current.Request["title"].IsEmpty()){
ModelState.AddError("title", "Please provide a title");}
}
if(HttpContext.Current.Request["title"].Length > 200){
ModelState.AddError("title", "Your title cannot exceed 200 characters");
}
if(HttpContext.Current.Request["description"].IsEmpty()){
ModelState.AddError("description", "You must provide a description");
}
if(HttpContext.Current.Request["duration"].IsEmpty()){
ModelState.AddError("duration", "Please choose a duration");
}
if(HttpContext.Current.Request["price"].IsEmpty()){
ModelState.AddError("price", "You must provide price");
}
if(!HttpContext.Current.Request["price"].IsDecimal() && !HttpContext.Current.Request["price"].IsEmpty()){
ModelState.AddError("price", "Provide a valid number for the price");
}
if(HttpContext.Current.Request["condition"].IsEmpty()){
ModelState.AddError("condition", "State the condition for your item");
}
if(HttpContext.Current.Request["email"].IsEmpty()){
ModelState.AddError("email", "You must provide a valid email");
}
if(!ModelState.IsValid){
ModelState.AddFormError("Please fix the errors");
}
}
<body>
<form id="post-advert" action="@Href("~/Sell")" method="post" >
<fieldset>
<legend>Post Your Advertisement</legend>
@Html.ValidationSummary(true)
<div>
<label for="title">Title</label>
</div>
<div>
<input type="text" name="title" value="@Request["title"]" />
@Html.ValidationMessage("title")
</div>
<div>
<label for="description">Description</label>
</div>
<div>
<textarea name="description">@Request["description"]</textarea>
@Html.ValidationMessage("description")
</div>
<div>
<label for="duration">Duration</label>
</div>
<div>
<select name="duration">
<option value="">--Choose one</option>
<option value="1" @Helpers.Selected("Duration", "1")>1 Day</option>
<option value="3" @Helpers.Selected("Duration", "3")>3 Days</option>
<option value="7" @Helpers.Selected("Duration", "7")>7 Days</option>
<option value="14" @Helpers.Selected("Duration", "14")>14 Days</option>
</select>
@Html.ValidationMessage("duration")
</div>
<div>
<label for="price">Price</label>
</div>
<div>
<input type="text" name="price" value="@Request["price"]"/>
@Html.ValidationMessage("price")
</div>
<div>
<label for="condition">Price</label>
</div>
<div>
<input type="radio" name="condition" value="@Request["condition"]"
@Helpers.Checked("condition","Fair")/> Fair
<input type="radio" name="condition" value="Good"
@Helpers.Checked("condition", "Good")/>Good
<input type="radio" name="condition" value="As New"
@Helpers.Checked("condition", "As New")/>As New
@Html.ValidationMessage("condition")
</div>
<div>
<label for="email">Your Email Address</label>
</div>
<div>
<input type="text" name="email" value="@Request["email"]"/>
@Html.ValidationMessage("email")
</div>
<div>
<input type="submit" name="submit" value="Post" />
</div>
</fieldset>
</form>
</body>
@section footer{
This is a footer for the Sell page
}/>
It says that <Option value="1" @Helpers.Selected("Duration", "1")>1 day</option> is out of context.
|

November 3rd, 2014, 03:45 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Did you add the Helpers to your project?
Imar
|

November 3rd, 2014, 04:06 PM
|
Registered User
|
|
Join Date: Nov 2014
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I did not used [code] or [/code]
I was following your example from your book (I had to make some changes from your book i.e. if(HttpContext.Current.Request["email"].IsEmpty()){
ModelState.AddError("email", "You must provide a valid email");
} which isn't in the book), and I keep getting context errors. My request objects work, but not my function (@Helpers.Selected) and I think I'll get the same error with the Helpers.Checked function.
|
|
 |