16 February 2014

Custom Validator -Validation Controls

       Custom Validator control is used if no other controls can perform the validation you need. Custom Validator control can perform client side and/or server side validation.

      Here are the main properties of the Custom Validator control.

  •           ControlToValidate: The ID of the form field to validate.
  •           Text: Error message to be displayed.
  •          ClientValidationFunction: Client side function to call for client side validation.This control also has an event that can be called after post back.
  •          OnServerValidate: This event will be raised after post back. 
Example1:

Client Side Validation:

To restrict the user from entering text for more than 20 characters
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="_default" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script language="javascript">
        function validateLength(sender, args) {
            if (args.Value.length <= 20)
                args.IsValid = true;
            else
                args.IsValid = false;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    Enter no more than 20 characters<br />
    <asp:TextBox runat="server" ID="txtComments" TextMode="MultiLine" Rows="5"></asp:TextBox><br />
    <asp:CustomValidator runat="server" ID="cvComments" ControlToValidate="txtComments" ForeColor="Red"
        ClientValidationFunction="validateLength" Text="Do not enter more than 20 characters">
    </asp:CustomValidator><br />
    <asp:Button runat="server" ID="btnSubmit" Text="Submit" />
    </form>
</body>
</html>

Server Side Validation:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="_default" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    Enter no more than 20 characters<br />
    <asp:TextBox runat="server" ID="txtComments" TextMode="MultiLine" Rows="5"></asp:TextBox><br />
    <asp:CustomValidator runat="server" ID="cvComments" ControlToValidate="txtComments" ForeColor="Red"
        OnServerValidate="cvComments_ValidateLength" Text="Do not enter more than 20 characters">
    </asp:CustomValidator><br />
    <asp:Button runat="server" ID="btnSubmit" Text="Submit" />
    </form>
</body>
</html>

C# Code:

public void cvComments_ValidateLength(object sender, ServerValidateEventArgs e)
{
    if (e.Value.Length <= 20)
        e.IsValid = true;
    else
        e.IsValid = false;
}
           We specified an event OnServerValidate instead of the property ClientValidationFunction on the Custom Validator control in this example. This example behaves the same as the first example. The only difference is that the validation happens on the server after post back. We defined a C# method in the code-behind file called cvComments_ValidateLength which will be called upon post back.

         Note: Since the server side validation only occurs on postback, any other client side validation using RequiredFieldValidator or other controls will happen before the form posts back. So the Custom Validator control validation does not occur until all the client side data is validated.

No comments:

Post a Comment