javascript 尝试在我的网站上执行表单验证

lrl1mhuk  于 2023-03-28  发布在  Java
关注(0)|答案(1)|浏览(125)

在youtube上看了一个基本的表单验证视频,似乎我的javascript文件中没有一条消息实际上是通过测试被推到网站上的......我希望登录表单上方显示错误消息,但我的代码中肯定有一些错误。

const username = document.getElementById('Username')
const password = document.getElementById('Password')
const form = document.getElementById('form')
const errorElement = document.getElementById('error')

form.addEventListener ('Login', (e) => {
    let messages =[]
    if (username.value === '' || username.value == null){
        messages.push('username is required')
    }
    if (password.value.length <= 6){
        messages.push('Password must be longer than 6 characters')
    }

    if (password.value.length >= 20){
        messages.push('Password is too long')
    }

    if (password.value == 'password'){
        messages.push('Password cant be password')
    }

    if (messages.length > 0){
        e.preventDefault()
        errorElement.innerText = messages.join(', ')

    }
})
body {
    font-family: "Lato", sans-serif;
}

.main-head {
    height: 150px;
    background: #FFF;
}

.sidenav {
    height: 100%;
    background-color: #680C07;
    overflow-x: hidden;
    padding-top: 20px;
    width: 100%;
}

.main {
    padding: 0px 10px;
}

@media screen and (max-height: 450px) {
    .sidenav {
        padding-top: 15px;
    }
}

@media screen and (max-width: 450px) {
    .login-form {
        margin-top: 10%;
    }

    .register-form {
        margin-top: 10%;
    }
}

@media screen and (min-width: 768px) {
    .sidenav {
        position: fixed;
        z-index: 1;
        top: 0;
        left: 0;
    }

    .login-form {
        margin-top: 80%;
    }

    .register-form {
        margin-top: 20%;
    }
}

.login-main-text {
    margin-top: 20%;
    padding: 60px;
    color: #fff;
}

.login-main-text h2 {
    font-weight: 300;
}

.btn-black {
    background-color: #000 !important;
    color: #fff;
}

.login-form {
  margin-top: 20%;
  padding: 20px;
}

.form-group {
  margin-bottom: 20px;
}

.btn {
  margin-top: 20px;
}

.alert {
    color: #31708F;
    background-color: #D9EDF7;
    border-color: #BCE8F1;
}
<!DOCTYPE HTML>
<html lang="en">
<head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<link href="sitestyle.css" rel="stylesheet" type="text/css">
<script defer src="C:\Users\John\Downloads\capstone project\sitescript.js"></script>
    <title>Excell Visualizer</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="sidenav">
    <div class="login-main-text">
        <h2>Chartcel <br> Login Page </h2>
        <p>Login or register from here to upload your desired spreadsheet.</p>
    </div>
    <div class="main">
        <div class="col-md-6 col-sm-12">
            <div class="login-form">
                <form id="form">
                    <div id="error"></div>
                    <div class="form-group">
                        <label for="username"> Username </label>
                        <input type="text" class="form-control" id="Username">
                    </div>
                    <div class="form-group">
                        <label>Password</label>
                        <input type="password" class="form-control" id="Password">
                    </div>
                    <button type="submit" onclick="check(this.form)" class="btn btn-black">Login</button>
                    <button type="submit" onclick="check(this.form)" class="btn btn-secondary"> Register</button>
                </form>
            </div>
        </div>
    </div>
 </div>
</body>
</html>
oknwwptz

oknwwptz1#

尝试将JS代码中的行更改为:

form.addEventListener('submit', (e) => {
   // .... your logic
})

在HTML DOM事件中没有'login'事件。所以你应该使用'submit'。

相关问题