0

I am new to Asp.Net, I want my login page to be validate on Button Click using Java script but on .cs file. Here is my code:

<script type="text/javascript">
    function CheckisEmpty()
    {
        var username = document.getElementById('txtUserName').innerHTML;
        var password = document.getElementById('txtPwd').innerHTML;

        if (username != '' || password != '')
        {

            return true;
        }
        else
        {
            alert("Feild Cannot Be Left Blank");
            return false;
        }

    }
</script>

<asp:Button ID="btnLogin" Text="Login" runat="server" Width="120px" OnClick="btnLogin_Click" OnClientClick="return CheckisEmpty();" CssClass="button"></asp:Button>

btnLogin.Attributes.Add("OnClick", "CheckisEmpty()");

Where am I wrong?

1 Answer 1

2

on your aspx page add this attribute on your button onClientClick="CheckisEmpty()"

No need to do on .cs page.

This way, first the js function will be executed, If the validation is successful, return true from the js function , else return false and give some notification to the user. When false is returned, The process will not continue to your cs function and thus page will not be post-back.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.