[英]javascript error handling
I have a javascript function for checking errors which I am calling on OnClicentClick event of a button. 我有一个JavaScript函数来检查我在按钮的OnClicentClick事件上调用的错误。 Once it catch a error I want to stop execution of the click event. 一旦发现错误,我想停止执行click事件。 But in my case it always it always executes the onclick event. 但就我而言,它始终执行onclick事件。
Following is my function: 以下是我的功能:
function DisplayError() {
if (document.getElementById('<%=txtPassword.ClientID %>').value.length < 6 || document.getElementById('<%=txtPassword.ClientID %>').value.length > 12) {
document.getElementById('<%=lblError.ClientID %>').innerText = "Password length must be between 6 to 12 characters";
return false;
}
var str = <%=PhoneNumber()%>;
if(str.length <10)
{
alert('<%=phoneNum%>'.length);
document.getElementById('<%=lblError.ClientID %>').innerText = "Phone Number not in correct format";
return false;
}
}
button html code: 按钮html代码:
<asp:Button runat="server" Text="Submit" ID="btnSubmit" ValidationGroup="submit" onclick="btnSubmit_Click" OnClientClick="DisplayError()"/>
It should not execute the button click event once it satisfies any of the IF condition in the javascript function. 一旦满足javascript函数中的任何IF条件,就不应执行按钮单击事件。
使用OnClientClick="return DisplayError();"
Use OnClientClick="if (!DisplayError()) return false;" 使用OnClientClick =“ if(!DisplayError())返回false;”
That way, if no error occurred, it will still postback, but only when an error occurs, it will return false, and cancel the postback. 这样,如果没有发生错误,它仍然会回发,但是只有当发生错误时,它才会返回false,并取消回发。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.