Saturday, 11 June 2016

How to Enable or Disable Requiredfieldvalidator in Asp.Net Using jQuery

6/11/2016 - By Pranav Singh 0

This article will show you how you can enable or disable Requiredfieldvalidator in asp.net using jQuery. In this I have shown to make enable or disable Requiredfieldvalidator on selection of radio button. You can use Checkbox also.

So for this article first we will create a new asp.net application and add the below tag code.

<form id="form1" runat="server">
        <div>
            <asp:RadioButton ID="rbtEnable" runat="server" ValidationGroup="1" GroupName="1" Text="Enable Validation" /><br />
            <asp:RadioButton ID="rbtDisable" runat="server" ValidationGroup="1" GroupName="1" Text="Disable Validation" />
        </div>
        <br />
        <div>
            Enter Sone Value :
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="Please enter some value"></asp:RequiredFieldValidator>
            <br />
            <asp:Label ID="lblmessage" runat="server" Text="" Style="color: #FF0000"></asp:Label>
            <br />
            <asp:Button ID="Button1" runat="server" Text="Click" OnClick="Button1_Click" />
        </div>
    </form>

In above I have taken radiobuttons, textbox, RequiredFieldValidator, label and button control.
Now check the below jQuery code.

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script>
        $(document).ready(function () {
            //Enable
            $("#<%=rbtEnable.ClientID%>").click(function () {
                ValidatorEnable($("#<%=RequiredFieldValidator1.ClientID %>")[0], true);
            });
            //Disable
            $("#<%=rbtDisable.ClientID%>").click(function () {
                ValidatorEnable($("#<%=RequiredFieldValidator1.ClientID %>")[0], false);
            });
        });
    </script>

In above code I have enable and disable the validation control by using ValidatorEnable. This function need the validation control id object and status for enabling and disabling the validation control. True means enable and false means disable.

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script>
        $(document).ready(function () {
            //Enable
            $("#<%=rbtEnable.ClientID%>").click(function () {
                ValidatorEnable($("#<%=RequiredFieldValidator1.ClientID %>")[0], true);
            });
            //Disable
            $("#<%=rbtDisable.ClientID%>").click(function () {
                ValidatorEnable($("#<%=RequiredFieldValidator1.ClientID %>")[0], false);
            });
        });
    </script>


Here is the complete code of the page.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication7.WebForm2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>How to Enable or Disable Requiredfieldvalidator in Asp.Net Using jQuery</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script>
        $(document).ready(function () {
            //Enable
            $("#<%=rbtEnable.ClientID%>").click(function () {
                ValidatorEnable($("#<%=RequiredFieldValidator1.ClientID %>")[0], true);
            });
            //Disable
            $("#<%=rbtDisable.ClientID%>").click(function () {
                ValidatorEnable($("#<%=RequiredFieldValidator1.ClientID %>")[0], false);
            });
        });
    </script>

</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:RadioButton ID="rbtEnable" runat="server" ValidationGroup="1" GroupName="1" Text="Enable Validation" /><br />
            <asp:RadioButton ID="rbtDisable" runat="server" ValidationGroup="1" GroupName="1" Text="Disable Validation" />
        </div>
        <br />
        <div>
            Enter Sone Value :
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="Please enter some value"></asp:RequiredFieldValidator>
            <br />
            <asp:Label ID="lblmessage" runat="server" Text="" Style="color: #FF0000"></asp:Label>
            <br />
            <asp:Button ID="Button1" runat="server" Text="Click" OnClick="Button1_Click" />
        </div>
    </form>
</body>
</html>


 
Now we have done. Run the application to check the output.


Now select disable and add some value and then click on button


About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top