Server side validation code for Asp.Net TextBox,DropDownList,CheckBoxList,RadioButtonList,ListBox,CheckBox and RadioButton controls

  ASP.NET Projects

Complete ASPX HTML Code:-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" 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>

Complete Code Behind:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Text;
using System.Web.UI.WebControls;

public partial class Default4 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    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;
    }
}

 

6,036 thoughts on - Server side validation code for Asp.Net TextBox,DropDownList,CheckBoxList,RadioButtonList,ListBox,CheckBox and RadioButton controls

LEAVE A COMMENT