2013-02-25 113 views
0

我需要你的幫助。我有一個非常簡單的問題,我無法弄清楚。它很難解釋,但我對一條線有評論,我希望你能理解它。這是我的代碼。對於長篇帖子感到抱歉,但我似乎無法擺脫這個漏洞。document.getElementById(「ID」)。value not working

function CheckLength() 
{ 
    var stringpassword = document.getElementById('Pass').value; 
    alert(document.getElementById('Pass').value); --> this part is only printing a blank. 
    /*  
    if(document.getElementById("Pass").value.length > 8) 
    { 
    return true; 
    } 
    else 
    { 
    alert(document.getElementById("Pass").value.length); 
    return false; 
    } 
    */ 
} 

function Check(a) 
{ 
    switch(a) 
    { 
    case 1 : 
     var first = document.getElementById("Uname").value.toUpperCase().indexOf(document.getElementById("Fname").value.toUpperCase()); 
     var last = document.getElementById("Uname").value.toUpperCase().indexOf(document.getElementById("Lname").value.toUpperCase()); 
     if(first !=-1 || last != -1) 
     { 
    alert("huy mali ang username. bawal yan pre"); 
    document.getElementById("Uname").value = ""; 
     } 
     break; 

    case 2 : 
     var gate = 0; 
     var first = document.getElementById("Pass").value.toUpperCase().indexOf(document.getElementById("Fname").value.toUpperCase()); 
     var last = document.getElementById("Pass").value.toUpperCase().indexOf(document.getElementById("Lname").value.toUpperCase()); 

     if(first !=-1 || last != -1) 
     { 
     alert("mali dahil sa name"); 
     gate++; 
     } 
     if(!CorrectPassword()) 
     {  
     gate++; 
     } 
     if(!CheckLength()) 
     {  
     gate++; 
     } 

     if(gate != 0) 
     { 
    alert("Please follow password policy"); 
     document.getElementById("Pass").value=""; 
     } 
     break; 

    case 3: 
     if(document.getElementById("Pass").value != document.getElementById("Rpass").value) 
     { 
    document.getElementById("Rpass").value = ""; 
     } 
     break; 
    } 
} 

在身體某處:

<input type="Password" placeholder="Password" value="" id="Pass" onblur="Check(2)"/> 

但是當我嘗試它。在密碼字段上輸入密碼後,點擊其他一些。警報框只會返回一個空白警報框。即使簡單的賦值語句不起作用?原諒我的壞英語

回答

0

我得到它的工作......我不得不取出檢查checkPassword(),因爲你沒有提供該功能。

在#Pass元素上,刪除onblur屬性。我把它分配在我提供的js的底部。

的jsfiddle:http://jsfiddle.net/jeffshaver/vcRDp/1/

function CheckLength() { 
    var stringpassword = document.getElementById('Pass').value; 
    alert(document.getElementById('Pass').value); 
/* 
    if(document.getElementById("Pass").value.length > 8) 
    { 
    return true; 
    } 
    else 
    { 
    alert(document.getElementById("Pass").value.length); 
    return false; 
    } 
*/ 
} 

function Check(a) { 
    switch(a) { 
     case 1 : 
      var first = document.getElementById("Uname").value.toUpperCase().indexOf(document.getElementById("Fname").value.toUpperCase()); 
      var last = document.getElementById("Uname").value.toUpperCase().indexOf(document.getElementById("Lname").value.toUpperCase()); 
      if(first !=-1 || last != -1) { 
       alert("huy mali ang username. bawal yan pre"); 
       document.getElementById("Uname").value = ""; 
      } 
      break; 

     case 2 : 
      var gate = 0; 
      if(first !=-1 || last != -1) { 
       alert("mali dahil sa name"); 
       gate++; 
      } 
      if(!CheckLength()) { 
       gate++; 
      } 

      if(gate != 0) { 
       alert("Please follow password policy"); 
       document.getElementById("Pass").value=""; 
      } 
      break; 

     case 3: 
      if(document.getElementById("Pass").value != document.getElementById("Rpass").value) { 
       document.getElementById("Rpass").value = ""; 
      } 
      break; 
    } 

} 

documentgetElementById('Pass').addEventListener('blur', function() { Check(2) }, false); 
相關問題