2014-10-20 1 views
0

마스터 페이지를 사용하는 로그인 페이지가 있는데 자바 스크립트를 통해 유효성 검사를 적용했지만 작동하지 않습니다.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

+0

그것도 그런 식으로 작동하지 않습니다 .. –

+0

버튼을 함수가 아닌 폼으로 전달하기 때문에'form.txtLoginId'는'checkField()'에서'undefined'입니다. 그러면 코드가 깨지고'false'가 반환되지 않습니다. 오류 메시지를보기 위해 콘솔을 열지 않았습니까? – Teemu

답변

0

당신은 필요한 필드 유효성 검사기, 시간 절약, 효율적인 클라이언트 측을 사용해야하며 많은 노력을 필요로하지 않습니다.

1

자바 스크립트 예외를 확인 했습니까?

당신은 서버 측 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" 설정 중 하나를 해결할 수 있습니다

사용자 컨트롤을 사용하거나 다른 곳에서했던 것처럼 자바 스크립트에서 <%= lblUserId.ClientID %>을 사용하십시오.

+0

"TypeError : document.getElementById (...) is null" 이 오류가 발생했습니다. –

관련 문제