提示框重新创建自身,直到输入正确的字段


Prompt box to re-create itself until correct field has been entered

我有一些Javascript警报代码。我希望我的客户端能够输入一个代码来访问我的网站的一个区域。

此刻,我有一些JS代码,如果用户输入错误的凭据一个警告框被创建,用户选择ok,它去网站无论如何。这并不理想。

我怎样才能有一个循环,重新创建提示框,直到提供正确的凭据(变量x)。

   window.onload = function launch() {
            var x = "name of credential";
            var person = prompt("The website is under development'nPlease enter your development code:");
            if (person == x) {
                alert("Success!");
                <?php header("Location : index.php");?>
            } 
            else {
                alert("You have entered the wrong credentials please try again!");
                var person = prompt("The Care Socierty website is under development'nPlease enter your development code:");
            }
克里希纳的建议

    window.onload = function launch() {
            var x = "credential";
            var person = prompt("The website is under development'nPlease enter your development code:");
            if (person == x) {
                alert("Success!");
                window.location;
            } 
            else {
                alert("You have entered the wrong credentials please try again!");
                return false;
            }
        }

创建循环以迭代代码

试试这个,它可能对你有帮助

window.onload = function () {
  var x = "name of credential";
  var wrong = true;
  while (wrong) {
    var person = prompt("The website is under development'nPlease enter your development code:");
    if (person == x) {
      alert("Success!");
      wrong = false;
      <?php header("Location : index.php");?>
    }
  }
}

试试:

if (person == x) {
  alert("Success!");
  window.location = 'index.php';
} // ...

如果您需要持续提示,您需要这样做。

试试:http://jsbin.com/duroguji/2/edit

$(function () {
  setTimeout(askPermission,1000);
});
function askPermission()
{
  var success = false;
  var x = "HELLO";
  while(!success)
    {
  var person = prompt("The website is under development'nPlease enter your development     code:", "");
  if (person == x) {
    success = true;
    alert("Success!");
  } 
  else {
    alert("You have entered the wrong credentials please try again!");
    setTimeout(askPermission,1000);
  }
  }
}