【HTML+CSS+JS】简单的登录注册验证

简单的小说明

本文是基于https://blog.csdn.net/NpcCat/article/details/106434653?spm=1001.2014.3001.5501的基础上的优化
有私信希望我做登录的用户名密码验证和登录后的页面跳转,这里用JS简单实现一下
还有很多额外的可以做的优化如输入信息格式的验证、查询用户名是否重复等都可以在取到用户输入的信息后通过一些简单的方法实现,百度上的大佬们也很多方法,这里就不进一步细化了。

效果

在这里插入图片描述

源码

HTML

登录注册.html

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>login</title>
		<link rel="stylesheet" type="text/css" href="pageChange.css" />
		<script src="pageChange.js"></script>
		<script src="Click.js"></script>
	</head>
	<body>
		<div class="control">
			<div class="item">
				<div class="active">登录</div><div>注册</div>
			</div>
			<div class="content">
				<div style="display: block;">
					<p>账号</p>
					<input type="text" placeholder="username" id="login-username"/>
					<p>密码</p>
					<input type="password" placeholder="password" id="login-password"/>
					<br/>
					<input type="submit" value="登录" onclick="login()"/>
				</div>
				<div>
					<p>用户名</p>
					<input type="text" placeholder="username" id="register-username"/>
					<p>密码</p>
					<input type="password" placeholder="password" id="register-password"/>
					<p>邮箱</p>
					<input type="text" placeholder="email" id="register-email"/>
					<br/>
					<input type="submit" value="登录" onclick="register()"/>
				</div>
			</div>
		</div>
	</body>
</html>


loginSuccess.html

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<h1>login Success</h1>
	</body>
</html>

CSS

*{
	margin: 0;
	padding: 0;
}
body{
	background: #f3f3f3;
}
.control{
	width: 340px;
	background: white;
	position: absolute;
	top: 50%;
	left: 50%;
	transform: translate(-50%,-50%);
	border-radius: 5px;
}
.item{
	width: 340px;
	height: 60px;
	background: #eeeeee;
}
.item div{
	width: 170px;
	height: 60px;
	display: inline-block;
	color: black;
	font-size: 18px;
	text-align: center;
	line-height: 60px;
	cursor: pointer;
}
.content{
	width: 100%;
}
.content div{
	margin: 20px 30px;
	display: none;
	text-align: left;
}
p{
	color: #4a4a4a;
	margin-top: 30px;
	margin-bottom: 6px;
	font-size: 15px;
}
.content input[type="text"], .content input[type="password"]{
	width: 100%;
	height: 40px;
	border-radius: 3px;
	border: 1px solid #adadad;
	padding: 0 10px;
	box-sizing: border-box;
}
.content input[type="submit"]{
	margin-top: 40px;
	width: 100%;
	height: 40px;
	border-radius: 5px;
	color: white;
	border: 1px solid #adadad;
	background: #00dd60;
	cursor: pointer;
	letter-spacing: 4px;
	margin-bottom: 40px;
}
.active{
	background: white;
}
.item div:hover{
	background: #f6f6f6;
}


JS

pageChange.js

window.onload = function(){
	var item = document.getElementsByClassName("item");
	var it = item[0].getElementsByTagName("div");
	
	var content = document.getElementsByClassName("content");
	var con = content[0].getElementsByTagName("div");
	
	for(let i=0;i<it.length;i++){
		it[i].onclick = function(){
			for(let j=0;j<it.length;j++){
				it[j].className = '';
				con[j].style.display = "none";
			}
			this.className = "active";
			it[i].index=i;
			con[i].style.display = "block";
		}
	}
}

Click.js

var user = new Map([["123","123"]])

function login(){
	var username = document.getElementById("login-username").value;
	var password = document.getElementById("login-password").value;
	
	if(user.get(username)==password){
		window.location.href="loginSuccess.html";
	}else{
		alert("用户名或密码错误");
	}
}

function register(){
	var username = document.getElementById("register-username").value;
	var password = document.getElementById("register-password").value;
	
	user.set(username,password);
	alert("注册成功,请登录");
	document.getElementById("register-username").value="";
	document.getElementById("register-password").value="";
	document.getElementById("register-email").value="";
}
  • 13
    点赞
  • 148
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是一个简单HTML+CSS+JS 注册页面,包括表单验证和成功跳转到登录页面的代码: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>注册页面</title> <style> form { display: flex; flex-direction: column; align-items: center; } input { margin: 10px; } .error { color: red; font-size: small; margin: 5px; } </style> </head> <body> <h1>注册</h1> <form onsubmit="return validateForm()"> <label for="username">用户名:</label> <input type="text" id="username" name="username" required> <label for="password">密码:</label> <input type="password" id="password" name="password" required> <label for="confirm_password">确认密码:</label> <input type="password" id="confirm_password" name="confirm_password" required> <button type="submit">注册</button> </form> <div id="error_messages"></div> <script> function validateForm() { let username = document.getElementById("username").value; let password = document.getElementById("password").value; let confirm_password = document.getElementById("confirm_password").value; // 检查用户名是否为空 if (username === "") { displayErrorMessage("用户名不能为空!"); return false; } // 检查密码是否为空 if (password === "") { displayErrorMessage("密码不能为空!"); return false; } // 检查密码是否与确认密码相同 if (password !== confirm_password) { displayErrorMessage("密码与确认密码不一致!"); return false; } // 注册成功,跳转到登录页面 alert("注册成功!"); window.location.href = "login.html"; return false; } function displayErrorMessage(message) { let error_messages_div = document.getElementById("error_messages"); let error_message = document.createElement("div"); error_message.className = "error"; error_message.innerHTML = message; error_messages_div.appendChild(error_message); } </script> </body> </html> ``` 这段代码包括一个表单和一些简单CSS 样式来布局表单。在 `<form>` 标签上添加了一个 `onsubmit` 属性,当用户点击注册按钮时,它会调用 `validateForm()` 函数来验证表单数据。如果表单验证通过,它将弹出一个成功消息,并使用 `window.location.href` 跳转到登录页面。 在 `validateForm()` 函数中,我们首先获取表单中的用户名、密码和确认密码。然后,我们检查用户名和密码是否为空,并检查密码和确认密码是否相同。如果检查失败,则使用 `displayErrorMessage()` 函数显示错误消息,并返回 `false` 防止表单提交。如果表单验证通过,则使用 `window.location.href` 跳转到登录页面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值