ASP.NET validation is incredibly helpful and permits developers to confirm data is correct before it gets sent to a data store. Validation can run either on the client side or on the server side and each have their advantages and disadvantages. MVC and Webforms handle validation slightly otherwise and i will not get into those differences during this article. Instead, i'd prefer to explain some best practices and suggestions for you once implementing ASP.NET validation.

Bind Validation To HTML elements
To implement validation, you initially add an HTML element to a page, then a validation element points to it HTML part. In MVC, you'd write one thing like this for a model field known as Name.

<div class="editor-field col-md-10"> 
    <div class="col-md-6"> 
        @Html.TextBoxFor(model => model.Name, new { @class = "form-control" }) 
    </div> 
    <div class="col-md-6"> 
        @Html.ValidationMessageFor(model => model.Name) 
    </div> 
</div> 


In Webforms, write the following code:
<asp:DropDownList ID="Name" runat="server" AutoPostBack="true" FriendlyName="Option:" 
AddBlankRowToList="false" DisplayBlankRowText="False" CausesValidation="true"> 
<asp:ListItem Text="Option 1" Value="1"></asp:ListItem> 
<asp:ListItem Selected="True" Text="Option 2" Value="2"></asp:ListItem>
</asp:DropDownList> 
<asp:CustomValidator ID="valCheck" ControlToValidate="Name" Display="Static" runat="server" ForeColor="red" 
ErrorMessage="Validation Error Message" SetFocusOnError="True" 
OnServerValidate="CheckForAccess" /> 

This ends up in HTML elements with attributes that may enable the client-side validation framework to execute.

Check For Validation Errors On Postback
Whereas validation ought to run on the client-side before the form is submitted to the server, it's still a good observe to see for errors on the server. There are variety of reasons for this, however the most reason should be that the client is an unknown quantity. There are completely different browsers, JavaScript will be disabled, developer tools enable people to change HTML and thus may result in unexpected data being denote. So, check your validation before you start process your type data on the server. And it's as simple as wrapping your server-side logic in a single code block.

MVC
[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Edit(ViewModel model) 

if (ModelState.IsValid()) 

    // your code to process the form 
}  
else 

    ModelState.AddModelError(String.Empty, "Meaningful Error message); 
    return View(model); 

return RedirectToAction("Index"); 


Webforms
protected void Page_Load(object sender, EventArgs e) 

if (IsPostback) 

    if (!IsValid) 
    { 
        // validation failed. Do something 
        return; 
    } 
     
    // do your form processing here because validation is valid 
}  
else  

    // non-postback 

Do one thing useful with your Validation Errors
If you discover that validation errors have gotten through to your server, you would like to capture them and show them to the user. a quick way that I even have found to try and do this is with a loop using the validator collection and look for errors. There are variety of things you will do like add the error messages to the Validation summary box at the top of your form, write to an audit log or build them seem in a dialog box.

foreach (IValidator aValidator in this.Validators) 

if (!aValidator.IsValid) 

    Response.Write("<br />" + aValidator.ErrorMessage); 



Validation is a critical part of form submission and should be used to it's full potential.

HostForLIFE.eu ASP.NET 5 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.