Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JavaScript to validate <asp:CustomValidator>

Status
Not open for further replies.

BunBo900

MIS
Dec 11, 2009
50
0
0
US
Hello:

I am trying to modify existing pages. I just added in a search capaibility to the page. This page rather complicate and lengthy to me. This page is using <ajaxToolkit:modal>
to process information. Everything is working fine except when
I have the JavaScript function ensureChecked() added to validate the checkbox making sure the user clicks on the checkbox before proceed to submit this causes the search capaiblity not to work. Is there work around this with out the need of JavaScript function ensureChecked() being called from <asp:CustomValidator> but still achieve what I want? Let me know if this confuses you....Thanks.


<script type="text/javascript">

function ensureChecked(source, args) {
var cb = document.getElementById('<%=cbxAgreement.ClientID%>');

if (cb.checked == true) {
args.IsValid = true;

} else {
args.IsValid = false;
}
}

</script>


<asp:panel ID="pnlEnrollmentAgreement" runat="server" Width="400px" Height="300px"
Style="display: none">
<table>
<tr>
<td align="left" valign="top">
<asp:Label ID="lbAgreementHeader" runat="server" Width="100%" BackColor="#E2E3C2"
ForeColor="Black" CssClass="linkStyle" Font-Size="14px" Text="<b>Step 3</b> - Enrollment Agreement"></asp:Label>
</td>
</tr>
<tr>
<td align="left">
<asp:CheckBox ID="cbxAgreement" runat="server" Text="I have received pre-enrollment counseling and will notify the appropriate training." /><br />
<br />
<asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction="ensureChecked"
ErrorMessage="* Please read then check the agreement to continue"></asp:CustomValidator>
<br />
<br />
<br />
</td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:Button ID="btnSubmit" runat="server" Text="Enroll in this Course!" OnClick="btnSubmit_OnClick" />
</td>
</tr>
</table>
</asp:panel>
 

I think I get what you're asking...

If your search function employs a "Search Button" where the user enters text in a textbox and clicks a separate button (other than the Submit button), set the CausesValidation="false" in the Search button propterties. This bypasses the CustomValidator OnClientClick when the Search button is clicked but leaves it to operate normally when the user clicks the Submit button.






Mark

"You guys pair up in groups of three, then line up in a circle."
- Bill Peterson, a Florida State football coach
 
Forget the Custom Validator


Code:
<script src="[URL unfurl="true"]http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js"[/URL] type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#<%= btnSubmit.ClientID %>').click(function () {
                if ($('#<%= cbxAgreement.ClientID %>').is(":not(:checked)")) {
                    alert("* Please read then check the agreement to continue");
                    return false;
                }

            });
        });
    </script>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top