Simple registration form with server side validation in ASP.Net with C#

 Print 
 Posted By :  ASPArticles
 Posted Date: Mar 08, 2016
 Category :  ,
 Post Hits : 276

In this article, we are going to learn server side validation in asp.net. In real scenario, we use client side validation or server side validation or combination of both i.e. client side as well as server side validation. In server side, validation occurs at server side hence it is more secure than client side validation.

Related Articles

In Designer File

In designer file, create a registration form using TextBox, DropDownlist and Button Control. Designer file will look like as shown below.

server side validation designer mode

Below is complete designer code.

<div align="center">
            <table style="border: 1px solid black;">
                <tr>
                    <td colspan="2"><span style="color: red;">*</span>indicates required fields
                    </td>
                </tr>
                <tr>
                    <td>First Name</td>
                    <td>
                        <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
                        <span style="color: red;">*</span>
                    </td>
                </tr>
                <tr>
                    <td>Last Name</td>
                    <td>
                        <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
                        <span style="color: red;">*</span>
                    </td>
                </tr>
                <tr>
                    <td>Age</td>
                    <td>
                        <asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
                        <span style="color: red;">*</span>
                    </td>
                </tr>
                <tr>
                    <td>Gender</td>
                    <td>
                        <asp:DropDownList ID="ddlGender" runat="server">
                            <asp:ListItem Text="--select--" Value="--select--"></asp:ListItem>
                            <asp:ListItem Text="Male" Value="Male"></asp:ListItem>
                            <asp:ListItem Text="Female" Value="Female"></asp:ListItem>
                        </asp:DropDownList>
                        <span style="color: red;">*</span>
                    </td>
                </tr>
                <tr>
                    <td>Email</td>
                    <td>
                        <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
                        <span style="color: red;">*</span>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click">
                    </asp:Button>
                    </td>
                    <td>
                        <asp:Label ID="lblError" runat="server" ForeColor="Red">
                        </asp:Label>
                    </td>
                </tr>
            </table>
        </div>

Namespace Used

Include the below Namespace.

using System.Text.RegularExpressions;

Complete C# Code

         protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            lblError.Text = string.Empty;
        if (txtFirstName.Text.Trim() == string.Empty)
            {
                lblError.Text = "Please enter first name";
                txtFirstName.Focus();
        return;
            }
        if (txtLastName.Text.Trim() == string.Empty)
            {
                lblError.Text = "Please enter last name";
                txtLastName.Focus();
        return;
            }
        var age= txtAge.Text.Trim();
        var isNumeric = !string.IsNullOrEmpty(age) && age.All(Char.IsDigit);
           
        if (isNumeric==false || Convert.ToInt32(age) < 20 || Convert.ToInt32(age) > 30)
            {
                lblError.Text = "Please enter valid age between 20 and 30";
                txtAge.Focus();
        return;
            }
        if (ddlGender.SelectedIndex == 0)
            {
                lblError.Text = "Please select gender";
                ddlGender.Focus();
        return;
            }
        //regex is used to check valid mail
        if (!Regex.IsMatch(txtEmail.Text.Trim(), @"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$"
            , RegexOptions.IgnoreCase))
            {
                lblError.Text = "Please enter valid Email";
                txtEmail.Focus();
        return;
            }
        /*
            
              call your Save method to save data
             
             
            */
            lblError.Text = "Record submitted successfully!!";
            lblError.ForeColor = System.Drawing.Color.Green;
            ClearControls();
        }
        public void ClearControls()
        {
            txtFirstName.Text = txtLastName.Text = txtAge.Text = txtEmail.Text = string.Empty;
            ddlGender.SelectedIndex = 0;
            
        }
        

When page loads, form will look like as shown below.

server side validation page load

When you click on submit button without filling form, error message will be shown on Label control.

server side validation on submit click

In the next article, we are going to learn client side validation in asp.net. Simple registration form with client side validation in asp.net





blog comments powered by Disqus