I have this js function :
function neki() {
if ( $("input[id*='txtIDCriteria']").val() == ''
&& $("input[id*='chkID']").is(':checked')
) {
alert('Value is required.');
}
}
And this asp code:
<table border="0" cellpadding="1" cellspacing="0"
style="width: 100%" class="DataTable" id="check_all"
>
<tr>
<td>
<asp:CheckBox CssClass="formdata100" ID="chkID" runat="server">
</asp:CheckBox>
</td>
<td>
<asp:Label CssClass="formdata100" ID="lblID" runat="server">
Maintenance ID
</asp:Label>
</td>
<td>
<asp:TextBox CssClass="formdata100" ID="txtIDCriteria"
runat="server" MaxLength="6"
onkeyup="return validate_int(event,this);"
>
</asp:TextBox>
</td>
</tr>
<tr>
<td colspan="5">
<asp:Button Text="search" ID="search" OnClick="search_Click"
runat="server" style="float:right" onClientClick="neki()"
/>
</td>
</tr>
I want this OnClick="search_Click"
to stop when this function neki()
is executed ie when it is true.
How to solve it through JavaScript?
Yes, you can do this. The way this works?
If the onclient click function returns true, then the server side code/event runs.
If the onclient click function returns false, then the server side code/event does NOT run. So you can do this:
<asp:Button Text="search" ID="search" OnClick="search_Click"
runat="server" style="float:right" onClientClick="return neki();"
And now your JS code can be this:
function neki() {
if ( $("input[id*='txtIDCriteria']").val() == ''
&& $("input*[id*='chkID']").is(':checked'))
{
alert('Value is required.');
return true;
}
return false;
}
While this will work for above, if you use a jQuery.UI dialog, the code does not wait, but there is a simple work around for even jQuery.UI dialog that respond to say a yes/no answer, and you can still conditional run the server side button code.
the code should be like this:
function neki() {
if ( $("input[id*='txtIDCriteria']").val() == ''
&& $("input*[id*='chkID']").is(':checked'))
{
alert('Value is required.');
return false; <--- return false to NOT run button click
}
return true; <--- return true - this will allow button click to run
}