2014-06-17 81 views
-2

這是驗證,我正在努力,但似乎無法得到正確的。當我輸入正確的引腳「12345」時,它工作並且函數Done()顯示警告「完成」。但是,我們在引腳上輸入了3次失敗的嘗試,但它顯示錯誤,但函數完成仍然會在錯誤發生後激活,程序應該停止。任何解決方案Javascript:爲什麼我的輸入驗證不起作用?

<head> 
    <script> 
     valid = false; 

     function Start(valid) { 


      check(valid); 


      if (valid = true) { 
       Done(); 
      } 
      if (valid = false) { 
       alert('End'); 
       return; 
      } 

     } 

     function check(valid) { 

      var PinNum; 
      var Attempts = 0; 


      while (Attempts < 3) { 
       Attempts = (Attempts + 1); 
       PinNum = prompt('Enter the pin number'); 

       if (PinNum == '12345') { 
        alert('Welcome!'); 
        valid = true; 
        return valid; 
        return; 
       } else { 
        alert('Wrong pin number, this is attemp ' + Attempts + ' of 3'); 
       } 
      } 
      alert('Too many failed attempts, giving up'); 
      return; 
     } 


     function Done() { 
      alert("well done") 
     } 
    </script> 
</head> 

<body> 
    <button onclick="Start()">Start</button> 
</body> 

</html> 
+1

採取的調試器和調試代碼 – zerkms

+2

你剛過'valid'作爲參數使得本地的校驗功能,所以改變'檢查函數內部valid'不會影響函數外的'valid',這是兩個不同的變量。 – adeneo

回答

1

在你if else語句中,分配給true代替valid比較它們的。您可以簡化語句:

var valid = false; 

function Start() { 
    check(); 

    if (valid) { 
     Done(); 
    } else { 
     alert('End'); 
    } 
} 

function check() { 

    var PinNum; 
    var Attempts = 0; 

    while (Attempts < 3) { 
     Attempts = (Attempts + 1); 
     PinNum = prompt('Enter the pin number'); 

     if (PinNum == '12345') { 
      alert('Welcome!'); 
      valid = true; 
      return; 
     } else { 
      alert('Wrong pin number, this is attemp ' + Attempts + ' of 3'); 
     } 
    } 
    alert('Too many failed attempts, giving up'); 
    return; 
} 


function Done() { 
    alert("well done") 
} 

demo

+0

但我需要確保有效是真實的。用你的方法,我的函數Done()不起作用。 – user3746770

+0

@ user3746770,當'valid === true'時,'if(valid)'等同於'if(true)'。如果這沒有解決它,還有一些其他問題 – Fabricator

+0

@ user3746770,我已經清理了你的代碼。它現在有效。 – Fabricator

0

幾個項目:

  1. 變量validStart功能比你的函數的外部定義的不同變量。每次單擊該按鈕時,undefined都會傳遞給該函數。您應該閱讀JavaScript範圍 - http://www.w3schools.com/js/js_function_closures.asp

  2. valid = true是一項任務。一個等號表示「分配給」,而一個雙重或三重等號===意味着要進行比較。你應該閱讀比較運算符 - http://www.w3schools.com/js/js_comparisons.asp

  3. 全局變量也被認爲是不好的做法,所以最好避免所有的變量valid。這裏是一個可能的解決方案 - http://www.jasonbuckboyer.com/playground/stack-overflow/javascript-why-does-my-input-validation-not-work.html


<!DOCTYPE html> 
<html> 
<head> 
<script> 
function start() { 
    if (is_valid()) { 
     Done(); 
    } else { 
     alert('End'); 
     return; 
    } 
} 
function is_valid() { 
    var PinNum, 
     Attempts = 0; 
    while (Attempts < 3) { 
     Attempts = (Attempts + 1); 
     PinNum = prompt('Enter the pin number'); 
     if (PinNum == '12345') { 
      alert('Welcome!'); 
      return true; 
     } else { 
      alert('Wrong pin number, this is attempt ' + Attempts + ' of 3'); 
     } 
    } 
    alert('Too many failed attempts, giving up'); 
    return false; 
} 
function Done() { 
    alert("well done") 
} 
</script> 
</head> 
<body> 
    <button onclick="start()">Start</button> 
</body> 
</html>