前端html+css+js注册+登录案例
时间: 2023-07-25 18:31:21 浏览: 79
好的,以下是一个简单的前端HTML + CSS + JS注册和登录案例:
HTML代码:
```
<!DOCTYPE html>
<html>
<head>
<title>注册和登录</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>欢迎注册和登录</h1>
<div class="container">
<div class="form-container">
<h2>注册</h2>
<form id="register-form" onsubmit="return validateRegisterForm()">
<label for="username">用户名:</label>
<input type="text" name="username" id="username">
<label for="password">密码:</label>
<input type="password" name="password" id="password">
<label for="email">电子邮件:</label>
<input type="email" name="email" id="email">
<button type="submit">注册</button>
</form>
</div>
<div class="form-container">
<h2>登录</h2>
<form id="login-form" onsubmit="return validateLoginForm()">
<label for="username">用户名:</label>
<input type="text" name="username" id="login-username">
<label for="password">密码:</label>
<input type="password" name="password" id="login-password">
<button type="submit">登录</button>
</form>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
```
CSS代码:
```
body {
font-family: Arial, Helvetica, sans-serif;
background-color: #f4f4f4;
}
h1 {
text-align: center;
margin-top: 50px;
}
.container {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
margin-top: 50px;
}
.form-container {
background-color: #fff;
padding: 20px;
margin: 20px;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0,0,0,0.3);
}
label {
display: block;
margin-bottom: 5px;
}
input[type="text"], input[type="password"], input[type="email"] {
width: 100%;
padding: 10px;
margin-bottom: 10px;
border: none;
border-radius: 5px;
background-color: #f4f4f4;
}
button[type="submit"] {
background-color: #4CAF50;
color: #fff;
padding: 10px;
border: none;
border-radius: 5px;
cursor: pointer;
}
button[type="submit"]:hover {
background-color: #3e8e41;
}
```
JavaScript代码:
```
function validateRegisterForm() {
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
var email = document.getElementById("email").value;
if (username == "") {
alert("请输入用户名");
return false;
}
if (password == "") {
alert("请输入密码");
return false;
}
if (email == "") {
alert("请输入电子邮件");
return false;
}
alert("注册成功!");
return true;
}
function validateLoginForm() {
var username = document.getElementById("login-username").value;
var password = document.getElementById("login-password").value;
if (username == "") {
alert("请输入用户名");
return false;
}
if (password == "") {
alert("请输入密码");
return false;
}
alert("登录成功!");
return true;
}
```
这个案例包含一个注册表单和一个登录表单。当用户提交表单时,JavaScript代码会验证输入是否有效,并根据结果显示成功或失败消息。您可以将此代码作为起点,进行更多的开发和改进,以实现更完整的注册和登录功能。