第二个IF函数未在SetInterval函数

时间:2018-03-21 13:59:02

标签: javascript jquery html login

所以,我正在编写一个简单的注册页面,而现在,我正在开发一个系统,可以识别并显示您在密码或用户名中使用非法字符的情况。

它工作得很好,但它只是在用户名中执行关于非法字符的部分,而不是密码。这很奇怪,因为如果我注释掉非法字符用户名if功能,密码if功能就可以了!

这是我的代码:

$(document).ready(function() {
  var errorThere2 = false;
  var errorThere = false;
  setInterval(function() {
    if ((/^[abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ]+$/.test($("#usernameBox").val())) || $("#usernameBox").val() == "") {
      currentText = "";
      errorThere2 = false;
      $("#usernameBox").css("border", "2px solid #707070");
      $("#submitButton").attr("disabled", false);
      $("#wrongStuff").html(currentText);
    } else {
      if (errorThere2 == false) {
        var currentText = $("#wrongStuff").text();
        var addedText = "Usernames are limitied to a-z, A-Z, and 0-9! ";
        $("#wrongStuff").html(currentText + addedText);
        errorThere2 = true;
      }
      $("#submitButton").attr("disabled", true);
      $("#usernameBox").css("border", "2px solid #E90B0B");
    }
    // Test for password box
    if (/^[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^{}-&*()`~'"<>,.?_=+]+$/.test($("#passwordBox").val()) || $("#passwordBox").val() == "") {
      currentText = "";
      errorThere = false;
      $("#passwordBox").css("border", "2px solid #707070");
      $("#submitButton").attr("disabled", false);
      $("#wrongStuff").html(currentText);
    } else {
      if (errorThere == false) {
        var currentText = $("#wrongStuff").text();
        var addedText = "Passwords cannot include the characters |, \\, or []! ";
        $("#wrongStuff").html(currentText + addedText);
        errorThere = true;
      }
      $("#submitButton").attr("disabled", true);
      $("#passwordBox").css("border", "2px solid #E90B0B");
    }
  }, 100);
  var hoveredOver = false;
  $(".enterButton").mouseenter(function() {
    if (hoveredOver === false) {
      $(this).css("background", "#F056AD");
      hoveredOver = true;
    }
  });
  $(".enterButton").mouseleave(function() {
    if (hoveredOver === true) {
      $(this).css("background", "#56E0F0");
      hoveredOver = false;
    }
  });
});
<!DOCTYPE>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Sign Up</title>
  <link href="https://fonts.googleapis.com/css?family=Muli" rel="stylesheet">
  <link rel="icon" href="logo.png">
  <link type="text/css" rel="stylesheet" href="index.css" />
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>

<body>
  <div id="container">
    <br>
    <div id="mainTitle">Sign Up</div><br><br><br><br><br>
    <center>
      <form method="POST" action="signup">
        <p id="wrongStuff" style="color: red;"></p><br>
        <input class="enterText" id="nameBox" required name="nameBox" type="text" placeholder="Full name"><br><br>
        <input class="enterText" id="usernameBox" required name="usernameBox" type="text" placeholder="Username"><br><br>
        <input class="enterText" id="emailBox" required name="emailBox" spellcheck="false" type="email" placeholder="Email Address"><br><br>
        <input class="enterText" id="passwordBox" required name="passwordBox" type="password" placeholder="Password"><br><br>
        <input class="enterText" id="passwordBox2" required name="passwordBox2" type="password" placeholder="Repeated password"><br><br><br>
        <input class="enterButton" id="submitButton" type="submit" value="Submit">
      </form>
    </center>
  </div>
</body>

</html>

您知道,用户名的允许字符是az,AZ和0-9,密码是az,AZ,0-9和大多数其他符号(不包括一些像\和|这会导致错误与我的程序的其他部分)

0 个答案:

没有答案
相关问题