Sunday 2 August 2015

What are the different validators in ASP.NET?

Validations are the most important part of websites and software. By using validations we can restrict users to enter any Invalid data into your database. There are many  validation techniques available do validate form data like JQUERY validations, server side validations. In Asp .Net microsoft has provided us great validation controls.  In this series of articles we will explore validation controls provided by microsoft.


  1. Required field Validator
  2. Range Validator
  3. Compare Validator
  4. Custom Validator
  5. Regular expression Validator
  6. Summary Validator

1.     Required field Validator
The RequiredFieldValidator is actually very simple, and yet very useful. You can use it to make sure that the user has entered something in a TextBox control.

Step1: Create TextBox control, Validation Control and Button.
Step2: Use ErrorMessage property of RequiredFieldValidator and enter message you want to display.
Step3: Use ControlToValidate property of RequiredFieldValidator and enter id of TextBox which you want to validate.
Step4: Use ForeColor property of RequiredFieldValidator and enter name of the color you want to use

<table border="0" cellpadding="0" cellspacing="0">
        <tbody><tr>
            <td>
                <b>Enter Your Name </b>
                <asp:textbox runat="server" id="txtTestForRequiredField">
            </asp:textbox></td>
        </tr>
        <tr>
            <td>
                <asp:requiredfieldvalidator id="RequiredFieldValidator1" forecolor="Red" errormessage="Please enter your name" controltovalidate="txtTestForRequiredField" runat="server">
            </asp:requiredfieldvalidator></td>
        </tr>
        <tr>
            <td>
                <asp:button id="Button1" text="Save" runat="server">
            </asp:button></td>
        </tr>
    </tbody></table>.




No comments:

Post a Comment