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.
Some of my
previous articles are as follows: Dynamically
Display Images From Folder By Ajax Using jQuery In Asp.net ,C#, Assign
Client Side Click Event To Button Control Using jQuery In Asp.Net, Enable
Or Disable All GridView Button On Check Of CheckBox Using jQuery, Ajax
GridView Shorting Without Page Refresh Using jQuery In Asp.net and C#.Net, Programming
ASP.NET AJAX - Free Download PDF eBook.
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.
<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.
Here is the complete code of the page.
<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>
|
0 comments:
Please let me know your view