@{ // Note that client validation as implemented here will work only with // ASP.NET Web Pages 2. var message=""; // Specify validation requirements for different fields. Validation.RequireField("coursename", "Class name is required"); Validation.RequireField("credits", "Credits is required"); Validation.Add("coursename", Validator.StringLength(5)); Validation.Add("credits", Validator.Integer("Credits must be an integer")); Validation.Add("credits", Validator.Range(1, 5, "Credits must be between 1 and 5")); Validation.Add("startDate", Validator.DateTime("Start date must be a date")); if (IsPost) { // Before processing anything, make sure that all user input is valid. if (Validation.IsValid()) { var coursename = Request["coursename"]; var credits = Request["credits"].AsInt(); var startDate = Request["startDate"].AsDateTime(); message += @"For Class, you entered " + coursename; message += @"
For Credits, you entered " + credits.ToString(); message += @"
For Start Date, you entered " + startDate.ToString("dd-MMM-yyyy"); // Further processing here } } } Validation Example with Client Validation

Validation Example with Client Validation

This example page asks the user to enter information about some classes at school.

@Html.ValidationSummary()
@Html.ValidationMessage("coursename")
@Html.ValidationMessage("credits")
@Html.ValidationMessage("startDate")
@if(IsPost){

@Html.Raw(message)

}