javascript error handeling

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:13 UTC
Read the original article Hit count: 269

Filed under:

I have a javascript function for checking errors which i am callin on OnClicentClick event of a button. Once it catch a error i want to stop execution of 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 javascript function.

© Stack Overflow or respective owner

Related posts about ASP.NET