What are the validation controls?

A set of server controls included with ASP.NET that test user input in HTML and Web server controls for programmer-defined requirements. Validation controls perform input checking in server code. If the user is working with a browser that supports DHTML, the validation controls can also perform validation using client script.

What are the validation controls available in ASP.net?

Required entry RequiredFieldValidator Ensures that the user does not skip an entry.
Comparison to a value CompareValidator Compares a user’s entry against a constant value, or against a property value of another control, using a comparison operator (less than, equal, greater than, and so on).
Range checking RangeValidator Checks that a user’s entry is between specified lower and upper boundaries. You can check ranges within pairs of numbers, alphabetic characters, and dates.
Pattern matching RegularExpressionValidator Checks that the entry matches a pattern defined by a regular expression. This type of validation allows you to check for predictable sequences of characters, such as those in social security numbers, e-mail addresses, telephone numbers, postal codes, and so on.
User-defined CustomValidator Checks the user’s entry using validation logic that you write yourself. This type of validation allows you to check for values derived at run time.

What are validation controls in ASP.NET? How do validation controls work? What are validation groups in ASP.NET 2.0?

Validation controls in ASP.NET are server side controls that validate input values client-side. Sounds strange? Well, the best approach to validate any input value is to validate it client-side to avoid any postback and load to the server. This approach is followed to reduce the load on the server.

ASP.NET validation controls greatly improve the perfomance of a web application and may be used by web developers to reduce plenty of code that they used to write previously to validate input values using javascript or vbscript.

Input validations improvise security of a web application, by preventing SQL Injection attacks.

There are 6 validations controls in both ASP.NET 1.1 and ASP.NET 2.0
1) RequiredFieldValidator – as the name suggests, this control makes sure that the input box is not left blank.
2) CompareValidator – This control validates values in two controls, and checks for equality
3) RangeValidator – This validation control makes sure that the value entered in the control falls within a range specified. This specification may be done using its properties.
4) RegularExpression – Based on a regular expression, values entered in an input box must match the ones specified by this control’s RegulareExpression property
5) CustomValidator – This type of validator control may be customized & created by a developer as per need
6) ValidationSummary – This control may be used to show the summary of the information gathered using rest of the other validation controls

To use a validation control, set a validation control on a form. Associate it with a server control by using its ControlToValidate property. Note that the server control has a property called CausesValidation and this is set to true for the validation control to trigger. This property is true by default for every server control. The validation control also has an ErrorMessage property that is assigned some text to explain what the error is, in case the validation control is trigerred. The validation control actually calls a client-side script that comes with ASP.NET, and this script does the job of validation for us. All this validation is done client-side.

Suppose we want to use the RequiredFieldValidator control so that a textbox is’nt left blank, for this, see the inline code below…
Code used for RequiredFieldValidator used above
<asp:Button ID=”btnGo” runat=”server” Text=”Go” />
<asp:TextBox ID=”txtSomeText” runat=”server” Text=”” CausesValidation=”true”></asp:TextBox>
<asp:RequiredFieldValidator ID=”rfvTextbox” runat=”server” ControlToValidate=”txtSomeText”
ErrorMessage=”Please Enter Some Text” ></asp:RequiredFieldValidator>
The Page.IsValid property of the page is TRUE only if there are no validation errors returned.

Validation controls are supported by Internet Explorer, Firefox and Opera.

ASP.NET 2.0 provides a method

How can we force all the validation controls to run?

Page.Validate

Which method is used to force all the validation controls to run ?

Page.Validate()

How many types of validation controls are provided by ASP.NET ?

There are six main types of validation controls :-

  • RequiredFieldValidator

it checks whether the control have any value. It’s used when you want the control should
not be empty.

  • RangeValidator

It checks if the value in validated control is in that specific range. Example
TxtCustomerCode should not be more than eight length.

  • CompareValidator

It checks that the value in controls should match some specific value. Example Textbox
TxtPie should be equal to 3.14.

  • RegularExpressionValidator

When we want the control value should match with a specific regular expression.

  • CustomValidator

It is used to define UserDefined validation.

  • ValidationSummary

It displays summary of all current validation errors.
Note:- It’s rare that some one will ask step by step all the validation controls. Rather they
will ask for what type of validation which validator will be used. Example in one of the
interviews i was asked how you display summary of all errors in the validation
control…just uttered one word Validation summary.

Can a DropDownList fire validation controls?

Yes, a DropDownList control can fire validation control when the CausesValidation property of that validation control is set to true and the AutoPostBack property is set to true.