views:

982

answers:

4

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  A: 

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.

CMS
+1  A: 

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

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

geowa4
A: 

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" />
Phaedrus
A: 

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

ob
Though i corrected that error still not working...