View Single Post
  #1 (permalink)  
Old January 4th, 2012, 05:10 PM
hozdaman hozdaman is offline
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