Kamran Ahmed

Types of Validation Controls in Asp




Control Name
Description
RequiredFieldValidator
Ensures that the user does not skip an entry.
CompareValidator
Compares a user's entry with a constant value or a property value of another control using a comparison operator (less than, equal to, greater than, and so on).
RangeValidator
Checks that a user's entry is between specified lower and upper boundaries. You can check ranges within pairs of numbers, alphabetic characters, or dates. Boundaries can be expressed as constants.
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.
CustomValidator
Checks the user's entry using validation logic that you code yourself. This type of validation allows you to check for values derived at run time.
ValidationSummary
Displays the validation errors in summary form for all of the validators on a page.

Displaying Validation Errors
When the user's input is processed (for example, when the form is submitted), the Web Forms page framework passes the user's entry to the associated validation control or controls. The validation controls test the user's input and set a property to indicate whether the entry passed the validation test. After all validation controls have been processed, the IsValid property on the page is set; if any of the controls shows that a validation check failed, the entire page is set to invalid.
If a validation control is in error, an error message may be displayed in the page by that validation control or in a ValidationSummary control elsewhere on the page. The ValidationSummary control is displayed when the IsValid property of the page is false. It polls each of the validation controls on the page and aggregates the text messages exposed by each. The following example illustrates displaying errors with a ValidationSummary control.
Working with CompareValidator
The CompareValidator server control compares the values of two controls. CompareValidator uses three key properties to perform its validation. ControlToValidate and ControlToCompare contain the values to compare. Operator defines the type of comparison to perform--for example, Equal or Not Equal. CompareValidator performs the validation by evaluating these properties as an expression, as follows:
   ( ControlToValidate  ControlToCompare ) 
If the expression evaluates true, the validation result is valid.

Working with RangeValidator
The RangeValidator server control tests whether an input value falls within a given range. RangeValidator uses three key properties to perform its validation. ControlToValidate contains the value to validate. MinimumValue and MaximumValue define the minimum and maximum values of the valid range.
Working with Regular Expressions
The RegularExpressionValidator server control 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.
RegularExpressionValidator uses two key properties to perform its validation. ControlToValidate contains the value to validate. ValidationExpression contains the regular expression to match.
Performing Custom Validation
The CustomValidator server control calls a user-defined function to perform validations that the standard validators can't handle. The custom function can execute on the server or in client-side script, such as JScript or VBScript. For client-side custom validation, the name of the custom function must be identified in the ClientValidationFunction property. The custom function must have the form
   function myvalidator(source, arguments)
Note that source is the client-side CustomValidator object, and arguments is an object with two properties, Value and IsValid. The Value property is the value to be validated and the IsValid property is a Boolean used to set the return result of the validation.
For server-side custom validation, place your custom validation in the validator's OnServerValidate delegate.
<asp:CustomValidator id="CustomValidator1" runat="server"
        ControlToValidate="Text1"
                ClientValidationFunction="ClientValidate"
        OnServerValidate="CustomValidator1_ServerValidate"
        Display="Static"
        Font-Name="verdana" Font-Size="10pt">
           Not an even number!
    </asp:CustomValidator>

<script language="javascript">

       function ClientValidate(source, arguments)
       {
          // even number?
          if (arguments.Value%2 == 0)
            arguments.IsValid = true;
          else
            arguments.IsValid = false;
       }
    </script>


protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
int num = Convert.ToInt32(args.Value);
if (num % 2 == 0)
{
            args.IsValid = true;
            Label1.Text = "Valid";
}
      else
      {
            args.IsValid = false;
            Label1.Text = "InValid";
}
}

No comments:

Post a Comment