I'm using asp.net validation controls which work fine. I've since introduced an HTML checkbox which should be ticked before the form can be submitted. My function for this reads as follows:
function terms(form) {
var terms = document.getElementById("chkTerms").checked;
var errorsArray = new Array();
if (terms == false) {
//alert("Terms not checked (works)");
errorsArray.push("You must agree to the terms and conditions.");
if (errorsArray.length) {
document.getElementById("termsOutput").innerHTML = errorsArray.join("\n") + "<br />";
return false;
}
开发者_StackOverflow中文版}
else {
document.getElementById("termsOutput").innerHTML = "";
}}
It is called as follows:
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="return terms(form)" />
The problem I have is seems I can either use the asp.net validation OR the custom checkbox validation I wrote. After the form posts however, the asp.net validation fires! Anyone know what I'm doing wrong?
The ASP.NET button control uses the WebForm_DoPostBackWithOptions method that subsequently triggers the client-side validation. By Adding return terms(form) to your button client click handler, this statement returns before the postback statement runs (which calls Page_ClientValidate) and so you don't see the validation. Doing an (if !terms(form)) return false) instead will cancel only when false.
HTH.
精彩评论