i have a button in my page .and in my script i do some works in button click event.
but i have a problem with validation.i use from require validation for my textbox in page.when i click in button it show message in validation summery and after on execute works that i write in script.how can i prevent this?
I am assuming that you want to run the code that is written on button click
only if all controls are valid.
There is a client side property called Page_IsValid set by asp.net client side validators after validation is done. You can use that on your button click to show the alert.
First do not disable your page validators. Let them work as expected.
Use the Page_IsValid javascript variable that is set by the validators to check if the page validation is passed.
Page_IsValid is set only by those validators that are called based on the validation group of the button.
By calling Page_ClientValidate we are forcing the page to call all the validators instead of those, that only belong to the validationgroup of the button clicked.
veronika.nap...
Member
108 Points
155 Posts
problem with require validation and botton click
Jun 08, 2011 03:00 PM|LINK
hi friends.
i have a button in my page .and in my script i do some works in button click event.
but i have a problem with validation.i use from require validation for my textbox in page.when i click in button it show message in validation summery and after on execute works that i write in script.how can i prevent this?
please help me.
urenjoy
Star
12377 Points
1862 Posts
Re: problem with require validation and botton click
Jun 08, 2011 04:19 PM|LINK
Try to set CausesValidation="false" of button
If it doesn't work then use
OnClientClick="javascript:Page_ValidationActive = false;"
veronika.nap...
Member
108 Points
155 Posts
Re: problem with require validation and botton click
Jun 08, 2011 04:28 PM|LINK
thanks,but if i set it false ,it can not check my textbox value.i need to check my textbox when i click in button.how can i do this?
veronika.nap...
Member
108 Points
155 Posts
Re: problem with require validation and botton click
Jun 08, 2011 04:59 PM|LINK
this is my code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="jquery.WebForm4" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script> <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script> <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { $('#<%=Button1.ClientID%>').click(function () { alert("hi"); }); }); </script> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="javascript:Page_ValidationActive = false;" /> </div> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="RequiredFieldValidator">*</asp:RequiredFieldValidator> </form> </body> </html>jkirkerx
Contributor
3750 Points
873 Posts
Re: problem with require validation and botton click
Jun 08, 2011 11:58 PM|LINK
onClientClick = "return page_Validation();"
it will run the javascript or jquery, and if the script returns true, it will fire the event handler.
function page_Validate() {
$(document).ready(function () {
var theLength;
theLength = $(txtbox).val().length;
if(theLength >= 3) {
// then good
return true;
} else {
return false;
}
});
}
hiren.sharma
Participant
1460 Points
311 Posts
Re: problem with require validation and botton click
Jun 10, 2011 08:53 AM|LINK
Do something like this, set cause validation of button to false and in your javacript code write Page_ClientValidate()
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="jquery.WebForm4" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script> <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script> <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { $('#<%=Button1.ClientID%>').click(function () { alert("hi"); Page_ClientValidate(); //This fires validation on client Side }); }); </script> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="Button" CauseValidation="false" OnClientClick="javascript:Page_ValidationActive = false;" /> </div> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="RequiredFieldValidator">*</asp:RequiredFieldValidator> </form> </body> </html> HOPE THIS HELPS HAPPY Coding !!!Try out My Blog | Visit My Website
hvemuri
Member
314 Points
53 Posts
Re: problem with require validation and botton click
Jun 10, 2011 11:46 AM|LINK
I am assuming that you want to run the code that is written on button click only if all controls are valid.
There is a client side property called Page_IsValid set by asp.net client side validators after validation is done. You can use that on your button click to show the alert.
$('#<%=Button1.ClientID%>').click(function () { if(Page_IsValid){ alert("hi"); } });Hari Vemuri
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
veronika.nap...
Member
108 Points
155 Posts
Re: problem with require validation and botton click
Jun 10, 2011 12:54 PM|LINK
@@dear hvemuri really thanks.
but when i have to button and i define validation group it dosent work?how can i solve it?
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="jquery.WebForm4" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script> <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script> <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { $('#<%=Button1.ClientID%>').click(function () { if (!Page_ClientValidate()) { return false; } else { alert("hi"); } //This fires validation on client Side }); $('#<%=Button2.ClientID%>').click(function () { if (!Page_ClientValidate()) { return false; } else { alert("hibtn2"); } //This fires validation on client Side }); }); </script> <title></title> </head> <body> <form id="form1" runat="server"> <div> <br /> </div> <asp:TextBox ID="TextBox1" runat="server" ValidationGroup="1"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="Button" CauseValidation="false" OnClientClick="javascript:Page_ValidationActive = false;" ValidationGroup="1" /> <br /> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="RequiredFieldValidator" ValidationGroup="1">*</asp:RequiredFieldValidator> <br /> <asp:TextBox ID="TextBox2" runat="server" ValidationGroup="2"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TextBox2" ErrorMessage="RequiredFieldValidator" ValidationGroup="2"></asp:RequiredFieldValidator> <asp:Button ID="Button2" runat="server" Text="Button" OnClientClick="javascript:Page_ValidationActive = false;" ValidationGroup="2" CausesValidation="False" /> </form> </body> </html>hvemuri
Member
314 Points
53 Posts
Re: problem with require validation and botton click
Jun 10, 2011 01:46 PM|LINK
First do not disable your page validators. Let them work as expected.
Use the Page_IsValid javascript variable that is set by the validators to check if the page validation is passed.
Page_IsValid is set only by those validators that are called based on the validation group of the button.
By calling Page_ClientValidate we are forcing the page to call all the validators instead of those, that only belong to the validationgroup of the button clicked.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="jquery.WebForm4" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script> <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script> <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { $('#<%=Button1.ClientID%>').click(function () { if (!Page_IsValid) { return false; } else { alert("hi"); } }); $('#<%=Button2.ClientID%>').click(function () { if (!Page_IsValid) { return false; } else { alert("hibtn2"); } }); }); </script> <title></title> </head> <body> <form id="form1" runat="server"> <div> <br /> </div> <asp:TextBox ID="TextBox1" runat="server" ValidationGroup="1"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="Button" ValidationGroup="1" /> <br /> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="RequiredFieldValidator" ValidationGroup="1">*</asp:RequiredFieldValidator> <br /> <asp:TextBox ID="TextBox2" runat="server" ValidationGroup="2"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TextBox2" ErrorMessage="RequiredFieldValidator" ValidationGroup="2"></asp:RequiredFieldValidator> <asp:Button ID="Button2" runat="server" Text="Button" ValidationGroup="2" /> </form> </body> </html>.Hari Vemuri
Please remember to mark the replies as answers if they help and unmark them if they provide no help.