javascript按钮在html中不起作用

javascript button not working in html

本文关键字:不起作用 html 按钮 javascript      更新时间:2023-09-26

我目前是web开发的新手。我忽略了这个代码,直到头疼。有人明白为什么点击登录按钮什么都不做吗?

    function validate()
{
  if( validateEmail(document.myForm.login.value)!==true )
  {
    alert( "Please provide your valide email address!" );
    document.myForm.login.focus() ;
    return false;
  }
  if(form.password.value.length < 6) {
    alert("Error: Password must contain at least six characters!");
    form.password.focus();
    return false;
  }
  if(form.password.value == form.login.value) {
    alert("Error: Password must be different from Username!");
    form.password.focus();
    return false;
  }
  re = /[0-9]/;
  if(!re.test(form.password.value)) {
    alert("Error: password must contain at least one number (0-9)!");
    form.password.focus();
    return false;
  }
  re = /[a-z]/;
  if(!re.test(form.password.value)) {
    alert("Error: password must contain at least one lowercase letter (a-z)!");
    form.password.focus();
    return false;
  }
  re = /[A-Z]/;
  if(!re.test(form.password.value)) {
    alert("Error: password must contain at least one uppercase letter (A-Z)!");
    form.password.focus();
    return false;
  }
  else {
    alert("Error: Please check that you've entered and confirmed your password!");
    form.password.focus();
    return false;
  }
  return true; 
}
function validateEmail(email) {   
  var re = /^(([^<>()[']''.,;:'s@'"]+('.[^<>()[']''.,;:'s@'"]+)*)|('".+'"))@(('[[0-9]{1,3}'.[0-9]{1,3}'.[0-9]{1,3}'.[0-9]{1,3}'])|(([a-zA-Z'-0-9]+'.)+[a-zA-Z]{2,}))$/;
  return re.test(email);
}

var attempt = 3; // Variable to count number of attempts.
// Below function Executes on click of login button.
function doLogin(){
  if(validate()){
    var username = document.getElementById("login").value;
    var password = document.getElementById("password").value;
    if ( username == "admin@website.co.za" && password == "Password1"){
      alert ("Login successfully");
      window.location = "session.php"; // Redirecting to other page.
      return false;
    }
    else{
      attempt--;// Decrementing by one.
      alert("You have left "+attempt+" attempt;");
      // Disabling fields after 3 attempts.
      if( attempt === 0){
        document.getElementById("login").disabled = true;
        document.getElementById("password").disabled = true;
        document.getElementById("submit").disabled = true;
        return false;
      }
    }
  }
}

^^javascript

    <!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <link rel="stylesheet" type="text/css" href="styles'login_Style.css">
    <title>Log In</title>
  </head>
  <body>
    <div class="container">
      <div class="login">
        <h1>Login</h1>
        <form name="myForm" method="post" action="">
          <p>
            <input type="text" name="login" value="" placeholder="Email Address">
          </p>
          <p>
            <input type="password" name="password" value="" placeholder="Password">
          </p>
          <p class="submit">
            <button type="button" name="commit" value="Login" onclick="doLogin()">Login</button>
          </p>
          <p class="submit">
            <button type="button" name="back" value="back" onclick="document.location.href='home-page.html';">Back To Home Page</button>
          </p>
        </form>
      </div>
    </div>
<script></script>
</body>
</html>

^^html按钮。请注意,脚本位于正文关闭的正上方。

尝试复制并粘贴它,您应该更接近于解决问题。我在代码中包含了一些注释,以帮助您理解我所做的操作。

    <!DOCTYPE html>
<html>
<head>
    <!--You need to reference these scripts to actually use any validation.-->
    <script src="lib/jquery-validation/dist/jquery.validate.js"></script>
    <script src="~/lib/jquery/dist/jquery.js"></script>
    <!--End Script reference-->
    <meta charset="utf-8" />
    <link rel="stylesheet" type="text/css" href="styles'login_Style.css">
    <title>Log In</title>
</head>
<body>
    <script>
        function validate() {
            if (validateEmail(document.myForm.login.value) !== true) {
                alert("Please provide your valid email address!");
                document.myForm.login.focus();
                return false;
            }
            if (myForm.password.value.length < 6) {
                alert("Error: Password must contain at least six characters!");
                form.password.focus();
                return false;
            }
            if (myForm.password.value == myForm.login.value) {
                alert("Error: Password must be different from Username!");
                form.password.focus();
                return false;
            }
            re = /[0-9]/;
            if (!re.test(myForm.password.value)) {
                alert("Error: password must contain at least one number (0-9)!");
                form.password.focus();
                return false;
            }
            re = /[a-z]/;
            if (!re.test(myForm.password.value)) {
                alert("Error: password must contain at least one lowercase letter (a-z)!");
                form.password.focus();
                return false;
            }
            re = /[A-Z]/;
            if (!re.test(myForm.password.value)) {
                alert("Error: password must contain at least one uppercase letter (A-Z)!");
                form.password.focus();
                return false;
            }
            //Your else function was returning false therefore when you did actually validate you would always get false and never actually validate
            else {
                return true;
            }
        }
        function validateEmail(email) {
            var re = /^(([^<>()[']''.,;:'s@'"]+('.[^<>()[']''.,;:'s@'"]+)*)|('".+'"))@(('[[0-9]{1,3}'.[0-9]{1,3}'.[0-9]{1,3}'.[0-9]{1,3}'])|(([a-zA-Z'-0-9]+'.)+[a-zA-Z]{2,}))$/;
            return re.test(email);
        }

        var attempt = 3; // Variable to count number of attempts.
        // Below function Executes on click of login button.
        function doLogin() {
            if (validate()) {
                var username = document.getElementById("login").value;
                var password = document.getElementById("password").value;
                if (username == "admin@website.co.za" && password == "Password1") {
                    alert("Login successfully");
                    window.location = "session.php"; // Redirecting to other page.
                    return false;
                }
                else {
                    attempt--;// Decrementing by one.
                    alert("You have left " + attempt + " attempt;");
                    // Disabling fields after 3 attempts.
                    if (attempt === 0) {
                        document.getElementById("login").disabled = true;
                        document.getElementById("password").disabled = true;
                        document.getElementById("submit").disabled = true;
                        return false;
                    }
                }
            }
        }
    </script>
    <div class="container">
        <div class="login">
            <h1>Login</h1>
            <!--onsubmit it will call the function doLogin() and the action attribute will redirect to session.php-->
            <form name="myForm" method="post" onsubmit="doLogin()" action="session.php">
                <p>
                    <!--Use Id to tag the elements that are getElementById-->
                    <input type="text" name="login" id="login" placeholder="Email Address">
                </p>
                <p>
                    <!--Use Id to tag the elements that are getElementById-->
                    <input type="password" name="password" id="password" placeholder="Password">
                </p>
                <p class="submit">
                    <!--Use Id to tag the elements that are getElementById-->
                    <!--Also the button type should be submit and id also submit-->
                    <button type="submit" name="commit" id="submit">Login</button>
                </p>
                <p class="submit">
                    <!--This is how I would do the back to home page button. It still should appear as a button-->
                    <a type="button" href="http://yoursite.com/home-page.html">Back To Home Page</a>
                </p>
            </form>
        </div>
    </div>

</body>
</html>

由于验证是一个应该像这样使用的函数,

 function doLogin(){
      if(validate()){

目前您正在将其用作变量。

我已经编辑了小提琴里面的代码。看看这个。Fiddle

尝试以下更新的HTML代码

<p class="submit"><button type="button" name="commit" value="Login" onclick="javascript:doLogin()">Login</button></p>

并确保doLogin函数中的validate变量具有有效值,如true/false