onsubmit没有调用javascript函数

时间:2018-03-17 17:29:48

标签: javascript validation

我查看过最近关于onsubmit事件的博客没有触发该方法。这些建议对这个问题没有帮助。我已经尝试过这个方法并在另一个没有工作的html页面中形成。所以我无法找出主要问题在哪里?我的代码:

    <script>
    function validateLogin()
    {
        var nameCheck=document.Log.username.value;
        var passwordCheck=document.Log.password.value;
        var status=false;

        if(nameCheck.length<4)
        {
            document.Log.getElementById("nameLoc").innerHTML=
            'Put your Email Address for this Community\'s Sake';
            status=false;

        }
        else
        {
            document.Log.getElementById("nameLoc").innerHTML=
            'Naming Convention Allright';
            status=true;
        }

        if(passwordCheck.length<8)
        {
            document.Log.getElementById("passwordLoc").innerHTML=
            'Password Does not Meet with Standard';
            status=false;
        }
        else
        {
            document.Log.getElementById("passwordLoc").innerHTML=
            'Passowrd Convention Allright';
            status=true;
        }
        return status;
    }
    </script>

    <form name="Log" method="post" action="login.php" onsubmit="return 
    validateLogin()">
      <div class="imgcontainer">
        <img src="login.png" alt="Avatar" class="avatar">
      </div>

      <div class="container">
        <label for="username"><b>Username</b></label>
        <input type="text" placeholder="Enter Username" name="username" 
        required><span id="nameLoc"></span>
        <br>
        <label for="password"><b>Password</b></label><span id="passwordLoc">
        </span>
        <input type="password" placeholder="Enter Password" name="password" 
         required><span id="passwordLoc"></span>
            <br>  
            <button type="submit">Login</button>
            <label>
              <input type="checkbox" checked="checked" name="remember"> 
               Remember      me
            </label>
          </div>
        </form>
            </div>

4 个答案:

答案 0 :(得分:1)

只需在按钮中添加值,如下所示: -

session_start();
$db = mysqli_connect("localhost", "root", "", "users");
if(isset ($_POST['Register_btm'])) {
    $name = mysqli_real_escape_string($db, $_POST['name']);
    $email = mysqli_real_escape_string($db, $_POST["email"]);
    $username = mysqli_real_escape_string($db, $_POST["username"]);
    $password = mysqli_real_escape_string($db, $_POST["password"]);
    $password2 = mysqli_real_escape_string($db, $_POST["password2"]);

if($password == $password2)
    $password = MD5($password);
    $sql = "INSERT INTO users(id, name, email, username, pssword) VALUES ('null','$name','$email','$username','$pssword')";
    mysqli_query($dm, $sql);
    $_SESSION[message] = "You have successfully registered";
    $_SESSION[username] = $username;
}
else {
    @$_SESSION[message] = " Passwords do not match";
}
?>

答案 1 :(得分:1)

您正试图在getElementById上使用form - 但它是document的函数。

更改为:

if (nameCheck.length < 4) {
                document.getElementById("nameLoc").innerHTML =
                    'Put your Email Address for this Community\'s Sake';
                status = false;

            }
            else {
                document.getElementById("nameLoc").innerHTML =
                    'Naming Convention Allright';
                status = true;
            }

            if (passwordCheck.length < 8) {
                document.getElementById("passwordLoc").innerHTML =
                    'Password Does not Meet with Standard';
                status = false;
            }
            else {
                document.getElementById("passwordLoc").innerHTML =
                    'Passowrd Convention Allright';
                status = true;
            }

答案 2 :(得分:0)

您可以在脚本标记中向表单添加事件侦听器,它将起作用:

document.Log.addEventListener("submit", validateLogin);

PS:为此,您需要将脚本标记移动到表单下方,因为在代码运行之前加载表单之前,不会对代码进行定义。

答案 3 :(得分:0)

你怎么知道你的功能是否被调用?在那里发出警报以确保!

如果满足任一条件,您的验证功能将允许继续提交。您应将其更改为初始化状态为true,然后在两个否定结果中将其设置为false,但不要将其设置为true以表示肯定。

如果它仍然不起作用,您可以将您的呼叫置于提交按钮的onclick事件中。