Skip to main content

What is the difference between client-side and server-side validations?

Client-side validations take place at the client side with the help of JavaScript and jQuery etc before the Web page is sent to the server whereas server-side validations take place at the server end and done using programming languages like C#.Net,VB.Net etc.
Client-side validation is faster than server-side because it takes place on client side (on browser) and the networking time from client to server is saved, whereas the server-side validation is done on the web server and server renders the data into html page and sends back to the client (browser). Thus it is bit slower.
Client side validation can be bypassed by disabling the browser's JavaScript, so we should also validate on the server side because it protects against the malicious user, who can easily bypass our JavaScript and submit dangerous input to the server.
Implementation: Let's create a sample web page to demonstrate the server side validation of asp.net controls.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head runat="server">
<title></title>
<style type="text/css">
.error
{
border: 1px solid red;
}
.errorstyle
{
font-size: 16px;
line-height: 22px;
}
.errorstyle ul li
{
margin-left: 5px;
color: ‪#‎ff0000‬;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div style="width:300px;">
<fieldset>
<legend>Validate TextBox</legend>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</fieldset>
<fieldset>
<legend>Validate DropDownList</legend>
<asp:DropDownList ID="DropDownList1" runat="server" Width="150px">
<asp:ListItem Value="0" Text="--Select--"></asp:ListItem>
<asp:ListItem Value="1" Text="Asp.Net"></asp:ListItem>
<asp:ListItem Value="2" Text="MVC"></asp:ListItem>
<asp:ListItem Value="3" Text="C#"></asp:ListItem>
<asp:ListItem Value="4" Text="jQuery"></asp:ListItem>
</asp:DropDownList>
</fieldset>
<fieldset>
<legend>Validate CheckBox</legend>
<asp:CheckBox ID="CheckBox1" runat="server" Text="Married" />
</fieldset>
<fieldset>
<legend>Validate CheckBoxList</legend>
<asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatColumns="2">
<asp:ListItem Value="1" Text="Asp.Net"></asp:ListItem>
<asp:ListItem Value="2" Text="MVC"></asp:ListItem>
<asp:ListItem Value="3" Text="C#"></asp:ListItem>
<asp:ListItem Value="4" Text="jQuery"></asp:ListItem>
</asp:CheckBoxList>
</fieldset>
<fieldset>
<legend>Validate RadioButton</legend>
<asp:RadioButton ID="RadioButton1" runat="server" Text="Married" />
</fieldset>
<fieldset>
<legend>Validate RadioButtonList</legend>
<asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatColumns="2">
<asp:ListItem Value="1" Text="Asp.Net"></asp:ListItem>
<asp:ListItem Value="2" Text="MVC"></asp:ListItem>
<asp:ListItem Value="3" Text="C#"></asp:ListItem>
<asp:ListItem Value="4" Text="jQuery"></asp:ListItem>
</asp:RadioButtonList>
</fieldset>
<fieldset>
<legend>Validate ListBox</legend>
<asp:ListBox ID="ListBox1" runat="server" Width="150px">
<asp:ListItem Value="1" Text="Asp.Net"></asp:ListItem>
<asp:ListItem Value="2" Text="MVC"></asp:ListItem>
<asp:ListItem Value="3" Text="C#"></asp:ListItem>
<asp:ListItem Value="4" Text="jQuery"></asp:ListItem>
</asp:ListBox>
</fieldset>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
<div class="errorstyle" >
<asp:Literal ID="ltrlErrorMsg" runat="server"></asp:Literal>
</div>
</div>
</form>
</body>
Asp.Net C# Code to validate controls server side
using System.Text;
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (IsControlsValid())
{
// Form is validated. Write your submit button code here.
}
}
private bool IsControlsValid()
{
bool isValid = true;
StringBuilder sb = new StringBuilder();
sb.Append("<ul>");
//Validate TextBox(can't be left empty)
if (String.IsNullOrEmpty(txtName.Text.Trim()))
{
isValid = false;
txtName.CssClass = "error";
sb.Append(String.Format("<li>{0}</li>", "Please enter in TextBox"));
}
else
{
txtName.CssClass = "";
}
//Validate DropDownList(Item must be selected)
if (DropDownList1.SelectedValue == "0")
{
isValid = false;
DropDownList1.CssClass = "error";
sb.Append(String.Format("<li>{0}</li>", "Please select DropDowlist item"));
}
else
{
DropDownList1.CssClass = "";
}
//Validate CheckBox(Must be checked)
if (!CheckBox1.Checked)
{
isValid = false;
CheckBox1.CssClass = "error";
sb.Append(String.Format("<li>{0}</li>", "Please select Checkbox"));
}
else
{
CheckBox1.CssClass = "";
}
//Validate RadioButton(Must be selected)
if (!RadioButton1.Checked)
{
isValid = false;
RadioButton1.CssClass = "error";
sb.Append(String.Format("<li>{0}</li>", "Please select RadioButton"));
}
else
{
RadioButton1.CssClass = "";
}
//Validate CheckBoxList(At least one item must be selected)
foreach (ListItem item in CheckBoxList1.Items)
{
if (item.Selected)
{
isValid = true;
break;
}
else
{
isValid = false;
}
}
if (isValid == false)
{
CheckBoxList1.CssClass = "error";
sb.Append(String.Format("<li>{0}</li>", "Please select at least one option from CheckBoxList"));
}
else
{
CheckBoxList1.CssClass = "";
}
//Validate RadioButton(Item must be selected)
foreach (ListItem item in RadioButtonList1.Items)
{
if (item.Selected)
{
isValid = true;
break;
}
else
{
isValid = false;
}
}
if (isValid == false)
{
RadioButtonList1.CssClass = "error";
sb.Append(String.Format("<li>{0}</li>", "Please select at least one option from RadioButtonList"));
}
else
{
RadioButtonList1.CssClass = "";
}
//Validate ListBox (Item must be selected)
foreach (ListItem item in ListBox1.Items)
{
if (item.Selected)
{
isValid = true;
break;
}
else
{
isValid = false;
}
}
if (isValid == false)
{
ListBox1.CssClass = "error";
sb.Append(String.Format("<li>{0}</li>", "Please select at least one item from ListBox"));
}
else
{
ListBox1.CssClass = "";
}
sb.Append("</ul>");
ltrlErrorMsg.Text = sb.ToString();
return isValid;
}

Comments

Popular posts from this blog

Top 10 ASP.NET Web API Interview Questions

What is ASP.NET Web API? ASP.NET Web API is a framework that simplifies building HTTP services for broader range of clients (including browsers as well as mobile devices) on top of .NET Framework. Using ASP.NET Web API, we can create non-SOAP based services like plain XML or JSON strings, etc. with many other advantages including: Create resource-oriented services using the full features of HTTP Exposing services to a variety of clients easily like browsers or mobile devices, etc. What are the Advantages of Using ASP.NET Web API? Using ASP.NET Web API has a number of advantages, but core of the advantages are: It works the HTTP way using standard HTTP verbs like  GET ,  POST ,  PUT ,  DELETE , etc. for all CRUD operations Complete support for routing Response generated in JSON or XML format using  MediaTypeFormatter It has the ability to be hosted in IIS as well as self-host outside of IIS Supports Model binding and Validation Support for OD...

First, FirstOrDefault, Single, SingleOrDefault In C#

For people who are new to LINQ, it is difficult to understand the difference between First, FirstOrDefault, Single, SingleOrDefault. In this blog, I will explain what to use and when.     I will take a simple example to make you understand practically how these methods work.   Consider a class Employee with properties as Id, Name, and Department. class  Employee {    public   int  Id {  get ;  set ; }    public   string  Name {  get ;  set ; }    public   string  Department{  get ;  set ; } } I have a list of Employees: List<Employee> employeeList =  new  List<Employee>(){    new  Employee() { Id = 1, Name =  "Sunny" , Department =  "Technical"  },    new  Employee() { Id=2, Name= "Pinki" , Department = "HR" },    new  Employee() { Id=3, Name= "Tensy" , De...

Extension methods in C#

Consider the class C# 1 2 3 4 5 6 7 8 9 10 11 12 13          namespace ExtensionMethod      {          public class testClass {              public string sayHello ( ) {              return "Hello" ;            }        }      }     Invoke the above from your form using C# 1 2 3 4 5 6          testClass test = new testClass ( ) ;      MessageBox . Show ( test . sayHello ( ) ) ;     This will show “Hello” in message box. Consider the scenario where you don...