Tuesday, 9 September 2014

Custom Validator(Client Side)

Here I will explain how to use customer validator in a asp.net project to validate a controls.

<head runat="server">    <title></title>   <script type="text/javascript" language="javascript">        function IsEven(source, args)        {            if (args.Value % 2 == 0) {                args.IsValid = true;}           else {  args.IsValid = false;}        }   </script></head>
<div>       <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>        <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="TextBox1"            ErrorMessage="please enter even no" ClientValidationFunction="IsEven"></asp:CustomValidator>        <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="IsEven" />        <br />        <br />        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>   
Customervalidator (Server side)

Custom text:<br /><asp:TextBox runat="server" id="txtCustom"/><asp:CustomValidator runat="server" id="cusCustom" controltovalidate="txtCustom" onservervalidate="cusCustom_ServerValidate" errormessage="The text must be exactly 8 characters long!" /><br /><br/>   protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {        if (args.Value.Length== 8)            args.IsValid = true;        else            args.IsValid = false;    }


Post a Comment

Copyright © DotNet-Ashok