CustomValidator client side validation example in ASP.NET

CustomValidator client side validation example in ASP.NET 

We can use CustomValidator(ASP.NET Validation control) for client side also. For this one we have to write one javascript function in the client side and need to pass source and args to that function.

For client side: CustomValidator required ClientValidationFunction, ControlToValidate, id, runt at and error message.  At the time of form submission, this javascript method will fire and check for the length of input. If input length is correct form submission will happen otherwise not.

This is one of the good ASP.NET Interview Question.

Output:
CustomValidator client side validation example in ASP.NET
CustomValidator client side validation example in ASP.NET 



ASP.NET Code:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">


</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    
    <script type="text/javascript">
        function fnCountCharacters(source, args) {
            var data = document.getElementById('<%=txtData.ClientID%>');
            if (data.value.length < 8) {
                args.IsValid = false;
            }
            else {
                args.IsValid = true;
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <table style="width:40%;">
            <tr>
                <td>
                    <b>Enter Data:</b></td>
                <td>
                    <asp:TextBox ID="txtData" runat="server"></asp:TextBox>
                </td>

            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    <asp:CustomValidator ID="CustomValidator1" runat="server" 
                        ErrorMessage="Enter minimun 8 characters" 
                        ClientValidationFunction="fnCountCharacters"></asp:CustomValidator>
                </td>

            </tr>
            <tr>
                <td>
                    <asp:Button ID="btnValidate" runat="server" Text="Validate Data" />
                </td>
                <td>
                    &nbsp;</td>

            </tr>
        </table>
    
    </div>
    </form>
</body>
</html> 
Share this post :

Post a Comment

Please give your valuable feedback on this post. You can submit any ASP.NET article here. We will post that article in this website by your name.

 
Support : Ranga Rajesh Kumar
Copyright © 2012. ASP.NET Examples - All Rights Reserved
Site Designed by Ranga Rajesh Kumar