Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old January 4th, 2012, 04:10 PM
Authorized User
Points: 221, Level: 4
Points: 221, Level: 4 Points: 221, Level: 4 Points: 221, Level: 4
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2010
Posts: 50
Thanks: 7
Thanked 0 Times in 0 Posts
Default 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>
Reply With Quote
  #2 (permalink)  
Old January 4th, 2012, 05:37 PM
Wrox Author
Points: 412, Level: 7
Points: 412, Level: 7 Points: 412, Level: 7 Points: 412, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2011
Posts: 57
Thanks: 1
Thanked 19 Times in 19 Posts
Default

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"]" />
Reply With Quote
  #3 (permalink)  
Old January 5th, 2012, 03:52 PM
Authorized User
Points: 313, Level: 6
Points: 313, Level: 6 Points: 313, Level: 6 Points: 313, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2011
Posts: 74
Thanks: 21
Thanked 2 Times in 2 Posts
Default 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");
        }
Reply With Quote
  #4 (permalink)  
Old January 5th, 2012, 03:56 PM
Imar's Avatar
Wrox Author
Points: 71,164, Level: 100
Points: 71,164, Level: 100 Points: 71,164, Level: 100 Points: 71,164, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,948
Thanks: 79
Thanked 1,555 Times in 1,532 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
jpjamie (January 5th, 2012)
  #5 (permalink)  
Old January 5th, 2012, 04:54 PM
Authorized User
Points: 313, Level: 6
Points: 313, Level: 6 Points: 313, Level: 6 Points: 313, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2011
Posts: 74
Thanks: 21
Thanked 2 Times in 2 Posts
Default

Quote:
Originally Posted by Imar View Post
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!
Reply With Quote
  #6 (permalink)  
Old January 6th, 2012, 10:55 AM
Wrox Author
Points: 412, Level: 7
Points: 412, Level: 7 Points: 412, Level: 7 Points: 412, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2011
Posts: 57
Thanks: 1
Thanked 19 Times in 19 Posts
Default

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");
        }
    }
}
Reply With Quote
  #7 (permalink)  
Old January 6th, 2012, 12:17 PM
Authorized User
Points: 313, Level: 6
Points: 313, Level: 6 Points: 313, Level: 6 Points: 313, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2011
Posts: 74
Thanks: 21
Thanked 2 Times in 2 Posts
Default

Quote:
Originally Posted by Mike Brind View Post
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 12:20 PM.
Reply With Quote
  #8 (permalink)  
Old November 3rd, 2014, 02:30 PM
Registered User
Points: 28, Level: 1
Points: 28, Level: 1 Points: 28, Level: 1 Points: 28, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2014
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default @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.
Reply With Quote
  #9 (permalink)  
Old November 3rd, 2014, 02:45 PM
Imar's Avatar
Wrox Author
Points: 71,164, Level: 100
Points: 71,164, Level: 100 Points: 71,164, Level: 100 Points: 71,164, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,948
Thanks: 79
Thanked 1,555 Times in 1,532 Posts
Default

Did you add the Helpers to your project?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #10 (permalink)  
Old November 3rd, 2014, 03:06 PM
Registered User
Points: 28, Level: 1
Points: 28, Level: 1 Points: 28, Level: 1 Points: 28, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2014
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Validating Input XML ssbsts XSLT 4 January 5th, 2011 11:59 AM
Chapter 9 Validating User Input Don Hurlbert BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 7 August 18th, 2010 02:42 PM
validating input bukky VBScript 1 April 1st, 2004 03:30 AM
validating user input hosefo81 Javascript How-To 12 March 3rd, 2004 08:32 AM
Validating user input stu9820 VB.NET 2002/2003 Basics 2 January 15th, 2004 11:51 AM



All times are GMT -4. The time now is 01:15 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.