Simple Form Validation using Asp.Net Validation Controls

Aug 04, 2018 Asp.Net 54
In this Article i will Explain how to validate form using Asp.Net Validation Controls with source code.

Form Validation:

Validation is the process of checking data against a standard or requirement OR checking the information entered by a person when sending information using an online service.


Form Validation in Asp.Net:

Asp.Net Provides a set of Validation Controls to Validate the different types of form User inputs.


There are Five types of validation controls in ASP.NET

1. RequiredFieldValidator
2. CompareValidator
3. RangeValidator
4. RegularExpressionValidator
5. CustomValidator

1. RequiredFieldValidator :

RequiredFieldValidator is use to check that there must be some value specify within the control.

Important Properties of RequiredFieldValidator:

ControlToValidate: Set the TextBox Id which you want to Validate.

ErrorMessage: Set the Error Message to display the Error.


2. CompareValidator :

CompareValidator is use to compare the values of two fields whether it is same or not.

Important Properties of CompareValidator:

ControlToValidate: Set the TextBox Id which you want to Validate.

ControlToCompare: Set the TextBox Id wherewith you want to Compare.


3. RangeValidator:

RangeValidator is use to check whether or not the value of an input control is inside a specified range.

Important Properties of RangeValidator:

ControlToValidate: Set the TextBox Id which you want to Validate.

MaximumValue: Set the maximum value of Range.

MinimumValue: Set the minimum value of Range.


4. RegularExpressionValidator:

RegularExpressionValidator is use to check whether or not the text matches a certain pattern. It is use to Validate the Email id, Mobile etc.

Important Properties of RegularExpressionValidator:

ControlToValidate: Set the TextBox Id which you want to Validate.

ValidationExpression: Set the regular expression pattern.


5. CustomValidator:

CustomValidator is use to customize and implement data validation according to our condition and requirement.

Important Properties of CustomValidator:

ControlToValidate: Set the TextBox Id which you want to Validate.

ValidateEmptyText: It is use to set a Boolean value indicating whether empty text should be validated or not.

ClientValidationFunction: It is use to set the name of custom client side script function used for validation.



Lets take a simple example to understand the form validation controls.


first of all design a simple form as shown in below image to test the form validation controls.

Simple Form Validation using Asp.Net Validation Controls

Asp.Net Code:


    <form id="form1" runat="server">
    <div><h2>form validation with validation controls</h2></div>
    <div><b>Name :</b><asp:textbox runat="server" ID="text1"></asp:textbox>
        <asp:RequiredFieldValidator ID="rf1" runat="server" ErrorMessage="enter name" ControlToValidate="text1"></asp:RequiredFieldValidator></div>
    <div><b>Email :</b><asp:textbox runat="server" ID="text2"></asp:textbox>
        <asp:RequiredFieldValidator ID="rf2" runat="server" ErrorMessage="enter email" ControlToValidate="text2"></asp:RequiredFieldValidator>
        <asp:RegularExpressionValidator ID="RegularExp1" runat="server" ErrorMessage="enter a valid email" ControlToValidate="text2" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator></div>
    <div><b>Password :</b><asp:textbox runat="server" ID="text3"></asp:textbox>
        <asp:RequiredFieldValidator ID="rf3" runat="server" ErrorMessage="enter password" ControlToValidate="text3"></asp:RequiredFieldValidator>
        <asp:RegularExpressionValidator ID="RegularExp2" runat="server" ErrorMessage="pass should be of 8 digit" ControlToValidate="text3" ValidationExpression="([a-zA-Z0-9]{4,8})$"></asp:RegularExpressionValidator></div>
    <div><b>Confirm Password :</b><asp:textbox runat="server" ID="text4"></asp:textbox>
        <asp:RequiredFieldValidator ID="rf4" runat="server" ErrorMessage="enter password again" ControlToValidate="text4"></asp:RequiredFieldValidator>
        <asp:CompareValidator ID="Comp1" runat="server" ErrorMessage="pass does't match" ControlToValidate="text4" ControlToCompare="text3"></asp:CompareValidator></div>
    <div><b>Mobile No. :</b><asp:textbox runat="server" ID="text5"></asp:textbox>
        <asp:RequiredFieldValidator ID="rf5" runat="server" ErrorMessage="enter mobile no." ControlToValidate="text5"></asp:RequiredFieldValidator>
        <asp:RegularExpressionValidator ID="RegularExp3" runat="server" ErrorMessage="enter a valid mobile no. of 10 digit" ControlToValidate="text5" ValidationExpression="\d{10}"></asp:RegularExpressionValidator></div>
        <div><b></b><asp:Button ID="Button1" runat="server" Text="Submit" /></div>


Now Run the code with Ctrl+F5 or F5



Now enter the incomplete form inputs and try to click on submit button to test the form user inputs, then form will show validation errors as shown in below image:


Simple Form Validation using Asp.Net Validation Controls


Share This Article :


Add Comment

Security Code :
8 + 2 =