0

Please could somebody point me out what is the error here? Using javascript i am validating the user input. When i click the login button without checking the javascript function, it goes to the welcome page. Here is my code:

<script type="text/javascript">  
function validLogin()
{
        if(document.getElementById("txtUserName").value == "")
        {
          alert("Please enter your UserName");
          return false;
        }

        if(document.getElementById("txtPassword").value == "")
        {
          alert("Please enter your Password");
          return false;
        }

}
</script>


 protected void Page_Load(object sender, EventArgs e)
 {
     BtnLogin.Attributes.Add("onClick","return ValidLogin();");     
 }
1
  • How does the HTML code look that this produces? Server side code is not very relevant for a client side issue... Commented Jan 31, 2009 at 9:20

4 Answers 4

1

I see that you're using ASP .NET (the Page_Load event on your posted code).

I think that will be easier to handle validation through ASP .NET Validation Controls, i.e. RequiredFieldValidator.

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

Comments

1

Check your case on return ValidLogin(); it doesn't match.

P.S.: I hope you aren't performing all user validation client-side.

Comments

0

It would probably be easier to user ASP .NET validation controls, here's a sample:

User Name:
<asp:TextBox ID="UserName" runat="server" />
<asp:RequiredFieldValidator 
    ID="UserNameValidator" 
    ControlToValidate="UserName"
    ErrorMessage="User Name Required" 
    runat="server" />
Password:
<asp:TextBox ID="Password" runat="server" />
<asp:RequiredFieldValidator 
    ID="PasswordValidator" 
    ControlToValidate="Password"
    ErrorMessage="Password Required"
    runat="server" />

Comments

0

first make sure it's not a difference in case... your javascript function is validLogin and in Page_Load you have ValidLogin

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.