2014-10-20 64 views
0

我有一个使用母版页的登录页面,我通过JavaScript应用验证,但它不工作。JavaScript不工作在母版页

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

如果我的用户名给予一定的价值它显示我在警报,但如果我不给任何值,那么如果条件应该工作,但它不工作,警报只是显示“测试”。 这里是我的html代码。

<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> 
+0

删除'||'部分条件。当你使用松散相等时,'null'也用'=='''检查。 – Teemu 2014-10-20 14:13:22

+0

它不是以这种方式工作.. – 2014-10-20 14:16:14

+0

您将一个按钮传递给该函数,而不是一个表单,因此在checkField()内'form.txtLoginId'是'undefined'。这打破了代码,'false'永远不会被返回。您尚未打开控制台以查看错误消息? – Teemu 2014-10-20 14:20:14

回答

0

您应该使用必需的字段验证程序,其节省时间,高效的客户端,并且不需要太多的努力。

1

您是否检查过javascript异常?

您通过服务器端ID引用ASP元素,这将不会被前端发现:

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; 
    } 

解决方案:

这可以解析为这些设置ClientIDMode = "static"用户控件或在javascript内使用<%= lblUserId.ClientID %>,正如您在别处所做的一样。

+0

“TypeError:document.getElementById(...)is null” 此错误出来了。 – 2014-10-20 14:46:56