Umm E Habiba Siddiqui
Umm E Habiba Siddiqui

Reputation: 594

Javascript not working in master page

I have a login page using master page and I applied validation via javascript but its not working.

<script type="text/javascript">

    function checkField(form)
    {
        var id = document.getElementById('<% =txtLoginId.ClientID %>').value;
        var pwd = document.getElementById('<% =txtPassword.ClientID %>').value;
        alert("testing" + id);
        if ((id == '' && pwd != '' )|| (id == null && pwd != null)) {
            lblUserId.visible = true;
            form.txtLoginId.focus();
            return false;
        }
        if ((pwd == '' && id != '') || (pwd == null && id != null)) {
            lblPwd.visible = true;;
            form.txtPassword.focus();
            return false;
        }
        if ((id == '' && pwd == '') || (id == null && pwd == null)) {
            lblBoth.visible = true;
            form.txtLoginId.focus();
            return false;
        }
    }
</script>

if I give some value in username it shows me in that alert but if I don't give any values then the if condition should work but it isn't working, alert just shows "testing". here is my html code.

<form id="form" method="post" action="Login.aspx">
      <div style="left:37%;position:absolute;top:55%;background-color:red">
        <table style="left:0%; position:absolute;top:0%; width: 265px">
            <asp:Label ID="lblUserId" runat="server" ForeColor="Red" Visible="false"  Text="* Username is Required!"></asp:Label>
            <asp:Label ID="lblPwd" runat="server" ForeColor="Red" Visible="false"  Text="* Passowrd is Required!"></asp:Label>
            <asp:Label ID="lblBoth" runat="server" ForeColor="Red" Visible="false"  Text="* Username and Password are Required!"></asp:Label>
         </table>
      </div>
    <div style="left:37%;position:absolute;top:60%;background-color:red">
        <table style="left:0%; position:absolute;top:0%; width: 265px;border:solid;background-color:darkorange">
            <tr align="center">
                <td align="center">
                    <asp:Label ID="lblHead" runat="server" Font-Bold="true"  Text="Mobile Customer Order Tracking"></asp:Label>
                </td>
            </tr>
         </table>
      </div>
      <div style="left:37%;position:absolute;top:65%">
         <table style="border:solid;">
            <tr>
              <td>
                 <asp:Label ID="lblLoginId" runat="server" Text="Login ID"></asp:Label>
              </td>
              <td>
                 <asp:TextBox ID="txtLoginId" ClientIDMode="Static" runat="server" />
              </td>
            </tr>
            <tr>
               <td>
                  <asp:Label ID="lblPassword" runat="server" Text="Password"></asp:Label>
               </td>
               <td>
                  <asp:TextBox ID="txtPassword" TextMode="Password" ClientIDMode="Static" runat="server"></asp:TextBox>
               </td>
            </tr>
            <tr>
               <td>
                  <asp:Label ID ="lblRemember" runat="server" Text="Remember My ID"></asp:Label>
                  <asp:CheckBox ID ="chkRemember" runat="server" />
               </td>
               <td align="right">
                   <asp:Button ID="btnLogin" runat="server" text="Login" OnClientClick="return checkField(this);"/>
               </td>
             </tr>
         </table>
       </div>
   </form>

Upvotes: 0

Views: 753

Answers (2)

black bubbles
black bubbles

Reputation: 20

you should use required field validator, its time saving, efficient, client side, and doesn't require much effort.

Upvotes: 0

Chris L
Chris L

Reputation: 2292

Have you checked for javascript exceptions?

You are referencing asp elements by the server side ID, this won't be found by the front end:

    if ((id == '' && pwd != '' )|| (id == null && pwd != null)) {
        lblUserId.visible = true; //here
        form.txtLoginId.focus();
        return false;
    }
    if ((pwd == '' && id != '') || (pwd == null && id != null)) {
        lblPwd.visible = true; //here
        form.txtPassword.focus(); 
        return false;
    }
    if ((id == '' && pwd == '') || (id == null && pwd == null)) {
        lblBoth.visible = true; //here
        form.txtLoginId.focus();
        return false;
    }

Solution:

This can be resolved either setting ClientIDMode = "static" for these user controls, or using <%= lblUserId.ClientID %> within the javascript as you have done elsewhere.

Upvotes: 1

Related Questions