javascript error handling

Posted by pankaj on Stack Overflow See other posts from Stack Overflow or by pankaj
Published on 2010-03-24T12:09:56Z Indexed on 2010/03/24 12:23 UTC
Read the original article Hit count: 161

I have a javascript function for checking errors which I am calling on OnClicentClick event of a button. Once it catch a error I want to stop execution of the click event. But in my case it always it always executes the onclick event.

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:

<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.

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about JavaScript