Asp.net Custom Validator control



The CustomValidator control is used to validate an input control with a user defined task, either from server side or client side. Normally this control is used when you feel that there is no other validation control fit in your requirement.

Following are main properties of the validation control.
ClientValidationFunctionGets or sets the validation function that will be used in client side (JavaScript function).
OnServerValidateMethod that fires after post back.
ControlToCompareGets or sets the ID of the control whose value will be compared with the currently entered value.
OperatorDataTypeCheck/Equal/GreaterThan/GreaterThanEqual/LessThan/LessThanEqual/NotEqual. Used to specify the comparison operation to peform. In case of DataTypeCheck, ControlToCompare properties are ingnored.
DisplayDynamic/Static. Used to indicate how the area of error message will be allocated.
Dynamic: Error message area will only be allocated when error will be displayed. Static: Error messagea area will be allocated in either case.
Enabledtrue/false. Gets or sets whether to enable the validation control or not.
ErrorMessageGets or sets the text of the error message that will be displayed when validation fails (This is displayed when ValidationSummary validatoin control is used.).
TextGets or sets the description of the error message text.
ValidationGroupGets or sets the validation group it belongs to. This is used to group a set of controls.
SetFocusOnErrortrue/false. Used to move focus on the control that fails the validation.

Example - 

// Form & Validation Control /////////////////////////
<asp:Label ID="lbl" AssociatedControlID="TextBox1" runat="Server" Text="Write into TextBox"></asp:Label>
    <asp:TextBox ID="TextBox1" runat="Server"></asp:TextBox>
    <asp:RequiredFieldValidator ID="req1" runat="Server" ControlToValidate="TextBox1" ErrorMessage="1st TextBox is Mandatory field" Text="<br>Please write something in 1st  Box."></asp:RequiredFieldValidator>
    <asp:CustomValidator ID="CustomValidator1" runat="Server" ControlToValidate="TextBox1" ClientValidationFunction="CheckForHardCodedValue" ErrorMessage="Value doens't match." Text="TextBox value must be [GOLD]" OnServerValidate="ValidateServerSide"></asp:CustomValidator>
    <asp:Button ID="btnSubmit" runat="Server" OnClick="WriteTextBoxValue" Text="Submit" />               
    <asp:ValidationSummary ID="ValidationSummary" runat="Server" ShowMessageBox="true" />

// JavaScript validation function /////////////////////////
function CheckForHardCodedValue(source, arguments)
    {
        var tID = '<%= TextBox1.ClientID %>';
        if (document.getElementById(tID).value == 'GOLD')
            arguments.IsValid = true;
        else
            arguments.IsValid = false;
    }

// Server side function to validate /////////////////////////
 protected void ValidateServerSide(object source, ServerValidateEventArgs args)
    {
        if (args.Value.Equals("GOLD"))
        {
            args.IsValid = true;
            lblMessage.Text += "Page is valid.<br />";
        }
        else
        {
            args.IsValid = false;
            lblMessage.Text += "Page is NOT valid.<br />";
        }
    }       
       







Share on Google Plus

About It E Research

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment