Custom Validation Control in Asp.net C#

CUSTOM VALIDATION


The CustomValidator control allows writing application specific custom validation routines for both the client side and the server side validation.

CustomValidator 

1.Server Side 

 2.Client Side.

DEMO

Html Coding 


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
<table><tr><td>
    <asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
    </td><td>
        <asp:TextBox ID="TextBox1"
runat="server"></asp:TextBox>
        <asp:CustomValidator ID="CustomValidator1"
runat="server"
            ControlToValidate="TextBox1"
ErrorMessage="Available lenght is 5 "
             OnServerValidate="CustomValidator1_ServerValidate">
</asp:CustomValidator>
    </td>
    <tr><td colspan="2">;
        <asp:Button ID="Button1" runat="server"  Text="Save" />
        </td></tr>

       </tr></table>
    </div>
    </form>
</body>
</html>


C# Coding


 protected void CustomValidator1_ServerValidate(object sender, ServerValidateEventArgs e)
    {
        if (e.Value.Length == 5)
        {
            e.IsValid = true;
        }
        else
        {
            e.IsValid = false;
        }
    }




The client side validation is accomplished through the ClientValidationFunction property. The client side validation routine should be written in a scripting language, like JavaScript or VBScript, which the browser can understand.

It doesn't come with a predefined way of working; you write the code for validating your self.




First  - Add New -WebForm - Select Labels & Button & Textboxs - Next  - Add Custom Validation From Validation ToolBox


Next - Add the Property Window - Control to Validate & Error Message








Next - Double Click -Custom Validator Tool- Write Coding 









                               

  Click - F5 Button - Run the Program


                               
   Given to  the Correct Length

 



                            

                      Given to the  InCorrect  Length







0 comments:

Post a Comment