JavaScript+CSS+html【练习题】JS正则表达式验证表单

题目

程序猿小明需要为公司员工的个人信息录入编写前端代码,效果如下图。
需求:要保证信息录入格式正确无遗漏(输入有误要给到对应提示)。
使用 HTML+CSS 布局出如上图所示页面效果;
嵌入 JS 代码,添加表单提交和表单项失去焦点等事件处理;
在事件处理中按照表单项后面提示的信息完成对应表单验证操作。
注意:单选和下拉框无须添加失去焦点事件(因为只有输入框需要)。
在这里插入图片描述

JS正则表达式

使用正则的3种方式

  • 向String对象的方法(search,match,replace,split)传入参数:/正则表达式/。
  • 先new一个正则表达式对象RegExp,再将其传入String对象的方法(search,match,replace,split)。
  • 先new一个正则表达式对象RegExp,使用RegExp自身的方法。
    //正则的使用
    var str = "wert45678yuiytrew";
    //使用正则匹配子串str中的数字
    console.log(str.match(/[0-9]+/));

    //使用RegExp创建一个正则对象
    var pat = new RegExp("[0-9]+");
    console.log(str.match(pat));
    console.log(pat.exec(str));
    //以上三个返回结果一致:["45678", index: 4, input: "wert45678yuiytrew", groups: undefined]

修饰符

在这里插入图片描述

方括号

方括号用于查找某个范围内的字符:
在这里插入图片描述

元字符

元字符(Metacharacter)是拥有特殊含义的字符:
\w :匹配单词字符,等价于 [a-zA-Z0-9_] 共63个字符(字母数字下划线)。
在这里插入图片描述

量词

在这里插入图片描述

RegExp 对象属性

在这里插入图片描述

RegExp 对象方法

在这里插入图片描述

支持正则表达式的 String 对象的方法

在这里插入图片描述

代码实现

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>个人信息登记表</title>
    <style>
        #board{
    
            width: 700px;
            height: 500px;
            border: 1px solid blueviolet;
            background-color:lightblue;

            overflow: hidden;
            position: relative;
        }

        form{
    
            position: absolute;
            left:50px;
            bottom: 30px;
        }

        h1{
    
            position: absolute;
            left:50px;
            top:0px;
        }

        #require
  • 3
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,以下是一个简单的注册页面示例,使用 HTMLCSSJavaScript,并使用正则表达式验证输入的邮箱和密码格式。 HTML: ```html <!DOCTYPE html> <html> <head> <title>注册页面</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> /* 居中显示 */ html, body { height: 100%; } body { display: flex; justify-content: center; align-items: center; } /* 表单样式 */ form { display: flex; flex-direction: column; align-items: center; padding: 20px; border: 1px solid #ccc; border-radius: 5px; } input[type="text"], input[type="password"] { margin: 10px; padding: 10px; border: 1px solid #ccc; border-radius: 5px; width: 300px; } input[type="submit"] { margin: 20px; padding: 10px; background-color: #4CAF50; color: white; border: none; border-radius: 5px; cursor: pointer; } input[type="submit"]:hover { background-color: #3e8e41; } /* 错误提示信息 */ .error { color: red; font-size: 14px; margin-top: -10px; } </style> </head> <body> <form id="register-form"> <label for="email">邮箱:</label> <input type="text" id="email" name="email" required> <span class="error" id="email-error"></span> <label for="password">密码:</label> <input type="password" id="password" name="password" required> <span class="error" id="password-error"></span> <input type="submit" value="注册"> </form> <script src="index.js"></script> </body> </html> ``` CSS: 上面的 HTML 中包含了一些 CSS 样式,用于设置页面布局和表单样式。 JavaScript: ```javascript // 获取表单元素和错误提示元素 const form = document.getElementById('register-form'); const email = document.getElementById('email'); const emailError = document.getElementById('email-error'); const password = document.getElementById('password'); const passwordError = document.getElementById('password-error'); // 邮箱验证正则表达式 const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; // 密码验证正则表达式 const passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$/; // 表单提交事件 form.addEventListener('submit', function(event) { // 阻止默认提交行为 event.preventDefault(); // 验证邮箱格式 if (!emailRegex.test(email.value)) { emailError.textContent = '请输入有效的邮箱地址'; return; } // 清除错误提示 emailError.textContent = ''; // 验证密码格式 if (!passwordRegex.test(password.value)) { passwordError.textContent = '密码要求至少8个字符,包括至少一个小写字母、一个大写字母和一个数字'; return; } // 清除错误提示 passwordError.textContent = ''; // 表单验证通过,可以提交了 alert('注册成功!'); }); ``` 以上代码实现了一个简单的注册页面,当用户填写邮箱和密码并点击注册按钮时,会使用正则表达式验证邮箱和密码格式。如果格式不正确,则会在对应的输入框下方提示错误信息,阻止表单提交。如果格式正确,则弹出一个注册成功的提示框。 注意:以上代码只是一个示例,实际项目中可能需要更复杂的表单验证逻辑和更完善的错误提示信息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值