Validation controls in ASP.NET

Q.2) Explain validation controls in detail.
There are six validation controls are available in the ASP.NET

- RequiredFieldValidator
- RangeValidator
- CompareValidator
- RegularExpressionValidator
- CustomValidator
- ValidationSummary


Ans

RequiredFieldValidator

You can create a field as a mandatory field by adding a RequiredFieldValidator control to the page and linking it to the required control. For example, you can specify that users must fill in a Name text box before they can submit a registration form. You have to set two important properties when using the RequiredFieldValdiator control otherwise you will get error.

ControlToValidate: The ID of the control for which the user must provide a value.

ErrorMessage: This property is used to show the error message, If the user forgets to enter the data errors that will appear.

Note: You have to set the above two given property in all the ASP.NET validation control. These two properties are common to all validation control.

Example:

< asp: Textbox id="txtName" runat="server">< /asp: Textbox>
< asp: RequiredFieldValidator id="RequiredFieldValidator1" runat="server"
ControlToValidate="txtName"
ErrorMessage="Name is a required field."
ForeColor="Red">
< /asp: RequiredFieldValidator>

CompareValidator

CompareValidator control is used to perform three different types of validation tasks.
- To perform a data type check.
- To compare the value entered into a form field against a given fixed value.
- To compare the value of one form field against another field.

Important Properties:

ValueToCompare: The fixed value against which to compare.

ControlToCompare: The ID of a control against which to compare.

Type: The type of value for comparison. Given values are String, Integer, Double, Date and Currency.

Operator: Given values are DataTypeCheck, Equal, GreaterThan, GreaterThanEqual, LessThan, LessThanEqual, and NotEqual.

Example

< asp: Textbox id="txtAge" runat="server">< /asp: Textbox>

< asp: CompareValidator id="CompareFieldValidator1" runat="server"
ForeColor="Red"
ControlToValidate="txtAge"
ValueToCompare="0"
Type="Integer"
Operator="GreaterThanEqual"
ErrorMessage="Please enter number greater than zero.">
< /asp: CompareValidator >

RangeValidator

It verifies that user input data is within a specified range of values. The input value should falls between a certain minimum and maximum value otherwise it will give error.

Don’t forget to set the Type property according to input data when using the RangeValidator control. By default, the Type property has the value String.

Important Properties:

MinimumValue: The minimum value of the validation range.
MaximumValue: The maximum value of the validation range.
Type: Given values are String, Integer, Double, Date, and Currency.

Example

< asp: Textbox id="TextBox1"
runat="server"/>
< asp: RangeValidator id="Range1"
ControlToValidate="TextBox1"
MinimumValue="1"
MaximumValue="10"
Type="Integer"
EnableClientScript="false"
Text="The value must be from 1 to 10!"
runat="server"/>


RegularExpressionValidator

The RegularExpressionValidator control performs its validation according to the regular expression. You can check a predefined pattern, such as a phone number, postal code, e-mail address, dates, and so on. The control uses the ValidationExpression property to set a valid regular expression that is applied to the input data.

Example

< asp:RegularExpressionValidator
id=”regEmail”
ControlToValidate=”txtEmail”
Text=” (Invalid email)”
ValidationExpression=”\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
Runat=”server” />

ValidationSummary

This control summarizes the error messages from all validation controls on a Web page in a single location. ValidationSummary control is useful when working with large forms. If a user enters the wrong value in the field located at the end of the page, then the user might never see the error message. ValidationSummary control displays a list of errors at the top of the form.

Important Properties:

DisplayMode: BulletList, List, and SingleParagraph

HeaderText: Display header text above the validation summary.

ShowMessageBox: It is used to display a popup alert box

ShowSummary: It hides the validation summary in the page.

CustomValidator

CustomValidator control enables you to create your own validation and that validation can run with the other validation control on the page. The CustomValidator control performs validation-based code you write. You can write validation code that will be executed on the client side using JavaScript,or with server-side validation .
Post your comment

    Discussion

  • RE: Validation controls in ASP.NET -Shalak S. Gurav (05/19/16)
  • Please solve mobile computing May 15, Oct 15, May 16 Paper also solve DAA paper and send on site
    Thanks alot