Follow us

Monday, January 17, 2011

Enabling/Disabling .NET validator control through Javascript

Introduction

Sometimes, we may fall under situation where it is desirable to enable/disable .NET validator controls programmatically. In such a situation, javascript ValidatorEnable() can be a best aid.

Syntax

ValidatorEnable() requires two arguments. First argument is Validator's ID and second is either true/false

ValidatorEnable(Validator Id , true/false);
  • To enable a validator, simply use "true" as a second argument.
  • To disable a validator, use "false" as a second argument.

Example

Add the javascript function.


Refer the page design source below:

<body>
    <form id="form1" runat="server">
    <h1>
        Enabling-Disabling a Validator Control using Javascript</h1>
    <h3>
        Condition :</h3>
    <ul>
        <li>If any of the address option is selected, Contact No. is mandatory.</li><li>Otherwise,
            it can be left blank.</li></ul>
    <table width="600px">
        <tr>
            <td width="20%">
                <asp:Label ID="Label2" runat="server" Text="Address"></asp:Label>
            </td>
            <td width="20%">
                <asp:RadioButton ID="rdoHome" runat="server" Text="Home" GroupName="Address" />
                <asp:RadioButton ID="rdoOffice" runat="server" Text="Office" GroupName="Address" />
            </td>
            <td width="20%">
            </td>
        </tr>
        <tr>
            <td width="20%">
                <asp:Label ID="Label1" runat="server" Text="Contact No"></asp:Label>
            </td>
            <td width="20%">
                <asp:TextBox ID="txtContactNo" runat="server"></asp:TextBox>
            </td>
            <td width="20%">
                <asp:RequiredFieldValidator ID="rfvContactNo" runat="server" ErrorMessage="Required"
                    ControlToValidate="txtContactNo" ValidationGroup="Submit" Enabled="false"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td width="20%">
            </td>
            <td width="20%">
                <asp:Button ID="btnSubmit" runat="server" Text="Submit" ValidationGroup="Submit"
                    OnClientClick="ValidatorOperation()" />
            </td>
            <td width="20%">
            </td>
        </tr>
    </table>
    </form>
</body>

Download

Sr.No Download Size
1 Enable/Disable .NET validator 4KB

Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

0 comments: on "Enabling/Disabling .NET validator control through Javascript"

Post a Comment