首页 > 编程语言 >javascript 文本校验框

javascript 文本校验框

时间:2023-08-06 12:00:41浏览次数:34  
标签:now document const form color javascript 校验 item 文本


 

javascript 文本校验框_ci

javascript 文本校验框_ci_02编辑

实现效果:

javascript 文本校验框_css_03

javascript 文本校验框_css_04编辑

javascript 文本校验框_css_05

javascript 文本校验框_ci_06编辑

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
  font-family: Arial, sans-serif;
  text-align: center;
  background-color: #f3f3f3;
}

h1 {
  margin-top: 50px;
  margin-bottom: 20px;
  font-size: 2em;
  color: #444;
}

#clock {
  margin-bottom: 20px;
  font-size: 1.2em;
  color: #666;
}

.form-item {
  margin-top: 20px;
  text-align: left;
  line-height: 2;
}

.form-item label {
  display: inline-block;
  width: 100px;
  font-weight: bold;
  color: #333;
}

.form-item input[type=text],
.form-item select {
  padding: 5px;
  border-radius: 3px;
  border: 1px solid #ccc;
  outline: none;
}

.form-item input[type=text]:focus,
.form-item select:focus {
  border-color: #5298d2;
}

.form-item button {
  margin-left: 10px;
  padding: 5px 10px;
  border-radius: 3px;
  border: none;
  outline: none;
  background-color: #5298d2;
  color: white;
  cursor: pointer;
}

.form-item button:hover {
  background-color: #3786c7;
}

.form-item input[type=radio],
.form-item input[type=checkbox] {
  margin-right: 10px;
}

.form-item input[type=radio]:checked+label,
.form-item input[type=checkbox]:checked+label {
  font-weight: bold;
  color: #5298d2;
}

.form-item button#submitForm {
  margin-top: 30px;
  width: 120px;
  height: 40px;
  background-color: #5298d2;
  color: white;
  font-size: 1.2em;
  border-radius: 5px;
  border: none;
}

.form-item button#submitForm:hover {
  background-color: #3786c7;
}

    </style>
</head>
<body>
    <!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>个人信息注册</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <h1>个人信息注册</h1>
    <div id="clock"></div>
    <form>
        <div class="form-item">
            <label for="name">姓名:</label>
            <input type="text" id="name" required>
        </div>
        <div class="form-item">
            <label for="password">密码:</label>
            <input type="text" id="password" readonly>
            <button type="button" id="generatePassword">生成密码</button>
        </div>
        <div class="form-item">
            <label>性别:</label>
            <input type="radio" name="gender" value="male" checked>男
            <input type="radio" name="gender" value="female">女
        </div>
        <div class="form-item">
            <label for="age">年龄:</label>
            <input type="text" id="age" pattern="\d+" required>
        </div>
        <div class="form-item">
            <label for="city">所在城市:</label>
            <select id="city">
                <option value="Beijing">北京</option>
                <option value="Shanghai">上海</option>
                <option value="Guangzhou">广州</option>
                <option value="Shenzhen">深圳</option>
            </select>
        </div>
        <div class="form-item">
            <label>爱好:</label>
            <input type="checkbox" name="hobby" value="reading">阅读
            <input type="checkbox" name="hobby" value="music">音乐
            <input type="checkbox" name="hobby" value="sports">运动
            <input type="checkbox" name="hobby" value="travel">旅游
            <input type="checkbox" name="hobby" value="food">美食
        </div>
        <div class="form-item">
            <button type="button" id="submitForm">提交</button>
            <button type="reset">重置</button>
        </div>
    </form>

    <script src="script.js"></script>
</body>
</html>
   
</body>
<script>
    // 获取HTML元素
const clockEle = document.getElementById("clock");
const nameInput = document.getElementById("name");
const passwordInput = document.getElementById("password");
const generatePasswordBtn = document.getElementById("generatePassword");
const ageInput = document.getElementById("age");
const submitFormBtn = document.getElementById("submitForm");

// 获取当前时间并显示在页面上
function showTime() {
    const now = new Date();
    const year = now.getFullYear();
    const month = now.getMonth() + 1;
    const date = now.getDate();
    const day = ["日", "一", "二", "三", "四", "五", "六"][now.getDay()];
    const hour = now.getHours();
    const minute = now.getMinutes();
    const second = now.getSeconds();
    clockEle.textContent = `${year}年${month}月${date}日 星期${day} ${hour}:${minute}:${second}`;
}
showTime(); // 首次加载页面时显示时间
setInterval(showTime, 1000); // 每秒钟更新时间

// 点击“生成密码”按钮,生成随机密码并填写到密码框中
generatePasswordBtn.addEventListener("click", function() {
    const password = Math.random().toString(36).slice(-8);
    passwordInput.value = password;
});

// 点击“提交”按钮,显示拼接提示信息
submitFormBtn.addEventListener("click", function() {
    const name = nameInput.value.trim();
    const password = passwordInput.value.trim();
    const gender = document.querySelector("input[name=gender]:checked").value;
    const age = parseInt(ageInput.value.trim());
    const city = document.getElementById("city").value;
    const hobbyArr = Array.from(document.querySelectorAll("input[name=hobby]:checked")).map((ele) => ele.value);
    const hobby = hobbyArr.length > 0 ? hobbyArr.join(",") : "无";
    if (!name) {
        alert("请输入姓名!");
        return;
    }
    if (!age || isNaN(age)) {
        alert("年龄必须是数字!");
        return;
    }
    const msg = `${name}您好,您的密码是${password},您所在的城市是${city},年龄是${age},您的爱好是${hobby}`;
    alert(msg);
});

</script>
</html>

javascript 文本校验框_ci_07

标签:now,document,const,form,color,javascript,校验,item,文本
From: https://blog.51cto.com/chenfenglove/6982904

相关文章

  • 【JavaScript01】简介与声明变量
    前言JavaScript是Web的编程语言。所有现代的HTML页面都使用JavaScript。JavaScript简介JavaScript的学习分三个部分:1、核心(ECMAScript):JavaScript的核心,描述了语言的基本语法和数据类型。ECMAScript是一套标准,定义了一种语言的标准与具体实现无关。2、文档......
  • JavaScript基础(1)
    正文:编程语言和标记语言JavaScript介绍JS基础注释输入输出语句变量  编程语言和标记语言编程语言:编程语言有很强的逻辑和行为能力,在编程语言里,有很多ifelse、for、while等具有逻辑性和行为能力的指令,这是主动的标记语言:标记语言(HTML)不同于向计算机发出指令,常用于......
  • 业务功能探索之条件分支全覆盖,includes能否一劳永逸【玩转JavaScript】
    灵感闪现某天,正在认真的敲代码,突然同事问了我一个问题,虽然不难,但是解决方案还是挺有趣的。所以写写这篇文章,详细记录一下实现过程。这个功能来自业务功能中的条件分支全覆盖,原本的做法一个includes轻松搞定,而这次,我心生了一点点疑问:includes真的能一劳永逸吗?JS实验开始includes不......
  • ppt怎么添加文本框
    1、点击插入打开打开PPT后点击上方工具栏插入打开。2、点击文本框在插入工具栏下点击文本框打开。3、插入文本框在幻灯片上拖动鼠标即可添加文本框。......
  • Web前端大作业、基于HTML+CSS+JavaScript响应式个人相册博客网站
    ......
  • 使用PaddleOCR在Ubuntu上实现一键截屏OCR提取文本
    转自:https://aistudio.baidu.com/aistudio/projectdetail/5665249一、项目简介1.1简要说明最近在折腾Ubuntu,有一个截屏然后OCR提取文本的应用需求。在Windws上这样的工具很好找,但是在Linux没有现成的软件可用,得自己解决。网上流行的方案是使用tesseract,试了一下,效果并不......
  • OCR深度实践系列(四):文本识别
    https://zhuanlan.zhihu.com/p/334340972(一)图像预处理(二)数据生成(三)文本检测最近在攻关法律领域的类案检索系统,这几天正好忙完了,得空继续写《OCR深度实践系列》这一专题的文章。前面三章依次介绍了图像预处理、数据生成以及文本检测三个模块,本章将介绍第四个关键模块:文本识......
  • JavaScript基础05
    函数参数的校验/***@判断参数是否为数字类型*@参数,一个参数*@返回值,如果参数是数字类型返回true,否则返回false*/functionisNumber(num){​ //1.第一版,把条件拆开判断​ //判断num是否是什么数据类型 if(typeofnum!=='number'){  console.log('num不是......
  • 记一次JavaScript异或算法加密 , 异或加密
     公司业务代码constBase64=require('base-64')functionxorEncrypt(str,key){letresultconstlist=[]for(leti=0;i<str.length;i++){constcharCode=str.charCodeAt(i)^key.charCodeAt(i%key.length)list.push(String.......
  • Windows校验文件MD5和SHA值的方法
    1、需求背景下载或传输文件后,需要计算文件的MD5、SHA256等校验值,以确保下载或传输后的文件和源文件一致2、校验方法如上图所示,可以使用Windows自带的certutil命令来计算一个文件的校验值命令格式为:certutil-hashfile文件名校验值类型certutil命令支持的校验值类型......