2014-10-20 59 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