Thursday, 26 July 2012

CustomValidator Control in ASP.NET | What is the use of CustomValidator Control in ASP.NET | How to use the CustomValidator Control in ASP.NET

Hi friends, in this article I will explain about CustomValidator Control, How to Validate with a Custom Function for ASP.NET Server Controls, ASP.NET CustomValidator Control.
CustomValidator   is used to writing application specific custom validation routines. We can use ASP.NET CustomValidator control to validate either client side or at server side.
I will explain CustomValidator   example in 2 ways
1.Client-side validation Example:
In this we write ClientValidationFunction property to write the validation function in client side like Javascript code.
Take one new website(file--- new website )
And Add New item and name it as cstmval.aspx
In the cstmval.aspx write the below code.
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Aspdotnet-Kishore Custom validator</title>
    <script language="javascript" type ="text/javascript" >
        function validateLength(s,e) {
            if (e.Value.length <= 10)
                e.IsValid = true;
            else
                e.IsValid = false;
        }
    </script>
</head>
<body>
<h1 style="color:Red">Aspdotnet- Custom Validator Example</h1>
    <form id="form1" runat="server">
    Enter Less than 10 characters<br />
    <asp:TextBox runat="server" ID="txtlength" TextMode="MultiLine" Rows="2"></asp:TextBox><br />
    <asp:CustomValidator runat="server"
          ID="cvlength"
          ControlToValidate="txtlength"
          ForeColor="Red"
          ClientValidationFunction="validateLength"
          Errormessage="Length Exceeds 10 characters">
    </asp:CustomValidator><br />
    <asp:Button runat="server" ID="btnSubmit" Text="Submit" />
    </form>
</body>
</html>
In the above code I write the one validateLength () function and used in ClientValidationFunction.
When we enter more than 10 characters and leave the textbox then it will  show the error Length Exceeds 10 characters.
1.Server-side validation Example:
In this we write ClientValidationFunction property to write the validation function in client side like Javascript code.
Take one new website(file--- new website )
And Add New item and name it as cstmval.aspx
In the cstmval.aspx write the below code.
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Aspdotnet-Kishore Custom validator</title>
    </head>
<body>
<h1 style="color:Red">Aspdotnet- Custom Validator Example</h1>
    <form id="form1" runat="server">
    Enter Less than 10 characters<br />
    <asp:TextBox runat="server" ID="txtlength" TextMode="MultiLine" Rows="2"></asp:TextBox><br />
    <asp:CustomValidator runat="server"
          ID="cvlength"
          ControlToValidate="txtlength"
          ForeColor="Red"
          OnServerValidate="validate_Length"
          Errormessage="Length Exceeds 10 characters">
    </asp:CustomValidator><br />
    <asp:Button runat="server" ID="btnSubmit" Text="Submit" />
    </form>
</body>
</html>
And write the below code in code behind.
In VB.NET:
Partial Class cstmval
    Inherits System.Web.UI.Page
    Protected Sub validate_Length(ByVal sender As Object, ByVal e As ServerValidateEventArgs)
        If (e.Value.Length <= 10) Then
            e.IsValid = True
        Else
            e.IsValid = False
        End If
    End Sub
End Class
In C#:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
partial class cstmval : System.Web.UI.Page
{
            protected void validate_Length(object sender, ServerValidateEventArgs e)
            {
                        if ((e.Value.Length <= 10)) {
                                    e.IsValid = true;
                        } else {
                                    e.IsValid = false;
                        }
            }
}

The output of the above programs either in client side or in  server side as shown in the below figure. The only difference is in client side when we leave the textbox then error will display but in server side the error will display after clicking on the submit button.

No comments:

Post a Comment

© 2012-2014 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.