gpt4 book ai didi

javascript - 使用 JavaScript 验证注册

转载 作者:行者123 更新时间:2023-11-28 03:20:50 25 4
gpt4 key购买 nike

我是 JavaScript 新手,目前正在使用 JavaScript 验证注册表单。我好像没掌握其中的诀窍。这是表单的 codepen 链接:

Codepen

请指出我做错了什么。

let button = document.querySelector("#submit");
let message = document.querySelector("#id");
let form = document.forms["login"]
let field1 = document.forms["login"]["firstName"];
let field2 = document.forms["login"]["lasttName"];
let field3 = document.forms["login"]["emailAddress"];
let field4 = document.forms["login"]["password"];



button.addEventListener("click", () => {
function submit() {
if (field1.value == "") {
message.textContext = "Fill in your first name";
return false;
}
if (field2.value == "") {
message.textContext = "Fill in your last name";
return false;
}
if (field3.value == "") {
message.textContext = "Fill in your email address";
return false;
}
if (field4.value == "") {
message.textContext = "Type in your desired password";
return false;
}
}
})
body,
html {
margin: 0;
width: 100%;
font-family: "lato", sans-serif;
background: #efefef;
height: 100%;
}

.container {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
vertical-align: middle;
line-height: 2;
width: 100%;
height: 100%;
}

.login {
width: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}

.input {
width: 70%;
}

.input-text {
width: 97%;
height: 2.5em;
padding: 0 10px 0 10px;
border-radius: 25px;
border: 1px solid black;
background: transparent;
}

.submit {
margin-top: 50px;
margin-bottom: 50px;
text-align: center;
background-color: #2196F3;
padding-top: 5px;
padding-bottom: 5px;
border-radius: 25px;
width: 70%;
color: white;
text-decoration: none;
}

a:hover {
background-color: #56b4ff;
color: white;
}

.inline {
display: flex;
width: 70%;
justify-content: space-between;
}

#box2 {
margin-left: 2em;
width: 70%;
}
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="register.css">
</head>

<body>
<div class="container">
<h1>Register Here</h1>
<p id="id"></p>
<form class="login" name="login" onsubmit="return submit()">
<div class="inline">
<div class="input" id="box1">
<p>First Name</p>
<input class="input-text" type="text" name="firstName" placeholder="First Name">
</div>
<div class="input" id="box2">
<p>Last Name</p>
<input class="input-text" type="text" name="lasttName" placeholder="Last Name">
</div>
</div>
<div class="input" id="box3">
<p>E-mail address</p>
<input class="input-text" type="text" name="emailAddress" placeholder="E-mail address">
</div>
<div class="input" id="box4">
<p>Password</p>
<input class="input-text" type="password" name="password" placeholder="Password">
</div>
<a href="#" class="submit" id="submit">Register</a>
</form>
</div>
<script type="text/javascript" src="register.js"></script>
</body>

</html>

最佳答案

几个问题

  • 使用表单的提交事件
  • 没有内部命名函数
  • 使用 PreventDefault 将事件传递给函数
  • 切勿将任何内容称为“提交”
  • 如果元素有 ID,我更喜欢 getElementById
  • 表单名称已过时
  • 不要使用“id”作为id - 这会令人困惑

我稍微简化了代码

let message = document.getElementById("message");
let form = document.getElementById("login");
let field1 = form.firstName;
let field2 = form.lasttName;
let field3 = form.emailAddress;
let field4 = form.password;
let messages = [];


form.addEventListener("submit", (e) => {
if (field1.value == "") {
messages.push("Fill in your first name");
}
if (field2.value == "") {
messages.push("Fill in your last name");
}
if (field3.value == "") {
messages.push("Fill in your email address");
}
if (field4.value == "") {
messages.push("Type in your desired password");
}
if (messages.length > 0) {
e.preventDefault();
message.innerText = messages.join("\n");
}
})
body,
html {
margin: 0;
width: 100%;
font-family: "lato", sans-serif;
background: #efefef;
height: 100%;
}

.container {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
vertical-align: middle;
line-height: 2;
width: 100%;
height: 100%;
}

.login {
width: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}

.input {
width: 70%;
}

.input-text {
width: 97%;
height: 2.5em;
padding: 0 10px 0 10px;
border-radius: 25px;
border: 1px solid black;
background: transparent;
}

.submit {
margin-top: 50px;
margin-bottom: 50px;
text-align: center;
background-color: #2196F3;
padding-top: 5px;
padding-bottom: 5px;
border-radius: 25px;
width: 70%;
color: white;
text-decoration: none;
}

a:hover {
background-color: #56b4ff;
color: white;
}

.inline {
display: flex;
width: 70%;
justify-content: space-between;
}

#box2 {
margin-left: 2em;
width: 70%;
}
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="register.css">
</head>

<body>
<div class="container">
<h1>Register Here</h1>
<p id="message"></p>
<form class="login" id="login">
<div class="inline">
<div class="input" id="box1">
<p>First Name</p>
<input class="input-text" type="text" name="firstName" placeholder="First Name">
</div>
<div class="input" id="box2">
<p>Last Name</p>
<input class="input-text" type="text" name="lasttName" placeholder="Last Name">
</div>
</div>
<div class="input" id="box3">
<p>E-mail address</p>
<input class="input-text" type="text" name="emailAddress" placeholder="E-mail address">
</div>
<div class="input" id="box4">
<p>Password</p>
<input class="input-text" type="password" name="password" placeholder="Password">
</div>
<button type="submit" class="submit">Register</a>
</form>
</div>
<script type="text/javascript" src="register.js"></script>
</body>

</html>

关于javascript - 使用 JavaScript 验证注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59170774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com