首页 > 其他分享 >用html写用户注册与登录

用html写用户注册与登录

时间:2023-09-27 23:15:39浏览次数:36  
标签:function 登录 用户注册 textContent getElementById html var document margin

<!DOCTYPE html>
<html>
<head>
<title>Registration System</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
}

h1 {
text-align: center;
}

form {
max-width: 400px;
margin: 0 auto;
}

label {
display: block;
margin-top: 10px;
}

input[type="text"],
input[type="password"] {
width: 100%;
padding: 5px;
margin-top: 5px;
}

.captcha {
display: flex;
align-items: center;
margin-top: 10px;
}

.captcha span {
margin-right: 10px;
}

.feedback {
color: red;
margin-top: 10px;
}

.buttons {
text-align: center;
margin-top: 10px;
}

.buttons button {
padding: 5px 10px;
margin-right: 5px;
}
</style>
</head>
<body>
<h1>Registration System</h1>
<form id="registrationForm">
<label for="username">Username:</label>
<input type="text" id="username" required>
<label for="password">Password:</label>
<input type="password" id="password" required>
<div class="captcha">
<label for="captcha">Captcha:</label>
<input type="text" id="captcha" required>
<span id="captchaText"></span>
<button type="button" onclick="refreshCaptcha()">Refresh Captcha</button>
</div>
<div class="buttons">
<button type="submit" onclick="register()">Register</button>
</div>
<div id="feedback" class="feedback"></div>
</form>

<div id="loginPage" style="display: none;">
<h1>Login</h1>
<form id="loginForm">
<label for="loginUsername">Username:</label>
<input type="text" id="loginUsername" required>
<label for="loginPassword">Password:</label>
<input type="password" id="loginPassword" required>
<label for="loginCaptcha">Captcha:</label>
<input type="text" id="loginCaptcha" required>
<span id="loginCaptchaText"></span>
<button type="button" onclick="refreshLoginCaptcha()">Refresh Captcha</button>
<div class="buttons">
<button type="submit" onclick="login()">Login</button>
</div>
<div id="loginFeedback" class="feedback"></div>
</form>
</div>

<div id="welcomePage" style="display: none;">
<h1>Welcome</h1>
<div id="welcomeMessage"></div>
<div id="currentTime"></div>
</div>

<script>
function refreshCaptcha() {
var captchaText = document.getElementById("captchaText");
var randomString = generateRandomString(8);
captchaText.textContent = randomString;
}

function refreshLoginCaptcha() {
var loginCaptchaText = document.getElementById("loginCaptchaText");
var randomString = generateRandomString(8);
loginCaptchaText.textContent = randomString;
}

function generateRandomString(length) {
var characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var randomString = "";
for (var i = 0; i < length; i++) {
var randomIndex = Math.floor(Math.random() * characters.length);
randomString += characters.charAt(randomIndex);
}
return randomString;
}

function register() {
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
var captcha = document.getElementById("captcha").value;
var captchaText = document.getElementById("captchaText").textContent;

if (captcha !== captchaText) {
document.getElementById("feedback").textContent = "Invalid captcha. Please try again.";
refreshCaptcha();
return;
}

if (!isValidUsername(username) || !isValidPassword(password)) {
document.getElementById("feedback").textContent = "Invalid username or password. Please try again.";
refreshCaptcha();
return;
}

alert("Registration successful!");
document.getElementById("registrationForm").reset();
showLoginPage();
}

function login() {
var loginUsername = document.getElementById("loginUsername").value;
var loginPassword = document.getElementById("loginPassword").value;
var loginCaptcha = document.getElementById("loginCaptcha").value;
var loginCaptchaText = document.getElementById("loginCaptchaText").textContent;

if (loginCaptcha !== loginCaptchaText) {
document.getElementById("loginFeedback").textContent = "Invalid captcha. Please try again.";
refreshLoginCaptcha();
return;
}

if (!isValidUsername(loginUsername) || !isValidPassword(loginPassword)) {
document.getElementById("loginFeedback").textContent = "Invalid username or password. Please try again.";
refreshLoginCaptcha();
return;
}

alert("Login successful!");
document.getElementById("loginForm").reset();
showWelcomePage(loginUsername);
}

function showLoginPage() {
document.getElementById("registrationForm").style.display = "none";
document.getElementById("loginPage").style.display = "block";
refreshLoginCaptcha();
}

function showWelcomePage(username) {
document.getElementById("loginPage").style.display = "none";
document.getElementById("welcomePage").style.display = "block";
document.getElementById("welcomeMessage").textContent = "Hello, " + username + "!";
updateTime();
}

function updateTime() {
var currentTime = document.getElementById("currentTime");
var date = new Date();
currentTime.textContent = "Current time: " + date.toLocaleTimeString();
setTimeout(updateTime, 1000);
}

function isValidUsername(username) {
var regex = /^[a-zA-Z0-9]{8,20}$/;
return regex.test(username);
}

function isValidPassword(password) {
var regex = /^[a-zA-Z0-9]{8,16}$/;
return regex.test(password);
}
</script>
</body>
</html>


标签:function,登录,用户注册,textContent,getElementById,html,var,document,margin
From: https://www.cnblogs.com/newzeon/p/17734601.html

相关文章

  • python解决ModuleNotFoundError No module named 'HTMLTestRunner'问题修改
    1、报错截图2、解决方法:(1)py2:从http://tungwaiyip.info/software/HTMLTestRunner.html下载HTMLTestRunner.py并将文件放到python2安装目录的Lib下,然后再次运行文件,可成功运行! (2) py3:a: 从http://tungwaiyip.info/software/HTMLTestRunner.html下载HTMLTestRunner.py......
  • HTML中的常见标签
    HTML中的常见标签<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content=&q......
  • itext7.pdfhtml For C#
    最近发现itext7(前身为iTextSharp)下有个https://github.com/itext/i7n-pdfhtml的项目可以支持html转PDF下面是官方电子书的翻译内容,原文地址:Chapter1:HelloHTMLtoPDF---第1章:你好HTML到PDF(itextpdf.com)第1章:你好HTML到PDF在本章中,我们将以许多不同的......
  • 批量将 html文件转成PDF文件
    再不会用openai就等被淘汰吧要批量将HTML文件转换为PDF文件,你可以使用一些可以自动化这个过程的工具或编程语言库。以下是一种可能的方法,使用Python编程语言和一个名为pdfkit的库,以及一个名为wkhtmltopdf的命令行工具,它可以将HTML转换为PDF。请按照以下步骤操作:安装必要的工......
  • Python桌面可视化+自动登录学校教务系统(含源码!!!)
    前言:通过Python爬虫与tkinter模块实现桌面快捷自动化登录教务系统效果展示:整体思路:创建主界面,在界面中手动输入用户名和密码,点击登录后自动打开浏览器,截取整个页面,裁剪出登录页面中的图片验证码并保存到本地,对验证码图片进行处理,识别出验证码。将用户名,密码,验证码一同自动输入到对......
  • ssh添加白名单限制登录
    集团分公司内网环境下的zabbix主机需要远程管理,那么就需要将ssh22端口从公司出口路由器中映射出来,如果不加以登录限制,那么被黑的几率就很高了,现将具体方法记录如下:编辑/etc/hosts.allow文件,将允许远程登录的主机ip添加到该文件中:sshd:50.30.150.158:allowsshd:50.194.2.1......
  • Linux2.1.13网络源代码学习(https://qiankunli.github.io/2022/07/04/linux_2_1_13_ne
    简介简介源码目录网络分层数据结构套接字套接字与vfssk_buff结构网络协议栈实现——数据struct和协议structlinux1.2.13接收数据收到数据包的几种情况Socket读取发送数据面向过程/对象/ioc以下来自linux1.2.13源码,算是参见Linux1.0的学习笔记。源码目......
  • 我们公司用了 3 年多的多账号统一登录方案,万能通用,稳的一批!
    作者:VanFan来源:juejin.cn/post/6844904053411938311现在几乎大部分的App都支持使用多个第三方账号进行登录,如:微信、QQ、微博等,我们把此称为多账号统一登陆。而这些账号的表设计,流程设计至关重要,不然后续扩展性贼差。本文不提供任何代码实操,但是梳理一下博主根据我司账号模块......
  • 使用CSS、HTML、JavaScript实现一个简单的身份验证页
      这是我在博客园的第一篇博客,也是我人生中的第一篇博客。希望它能够记录我的成长,帮助更多的人。  最近在写我们社团的社团网站,有一个页面不太希望普通访客能访问到,所以想做一个“统一身份验证验证”,但是又苦于社团网站搭建是纯静态站,没法做数据库,只能妥协,将账号密码字符串......
  • HTML <a> 标签的 target 属性
    https://www.w3school.com.cn/tags/att_a_target.asp<a>标签的target属性规定在何处打开链接文档。打开新窗口:<ahref="pref.html"target="view_window">Preface</a>在框架中打开窗口:<framesetcols="100,*"><framesrc="toc.h......