OnClientClick prevent page submission if validation errors
I've written a JavaScript function which validates form input and highlights the input fields and displays any error messages, which I have tested and all works fine.
I have called this function from the OnClientClick attribute of an asp:Button tag. However, if the JavaScript function determines there to be validation errors, I want to stop the form from submitting. How do I do this?
I am a VB.NET newbie, so any help would be appreciated. Thanks.
A开发者_如何学编程SPX
<asp:Button ID="btnSubmit" Text="Save Changes" runat="server" OnClientClick="validateEMI();" />
JavaScript
function validateEMI() {
// a big bunch of validation checks...
if (errorOnForm == true) {
document.getElementById("message").innerHTML = errorMsg;
return;
}
}
if (errorOnForm == true) {
document.getElementById("message").innerHTML = errorMsg;
return false;
}
return false;
EDIT: and validateEMI
should be hooked with onsubmit
event
use stopPropagation
function validateEMI(event) {
event.stopPropagation()
// a big bunch of validation checks...
if (errorOnForm == true) {
document.getElementById("message").innerHTML = errorMsg;
return;
}
}
精彩评论