在日常生活中,凡是需要表单验证的都会用到正则验证.下面拿一个简单的带有模态框的正则验证的小demo看一下
    <style>
        /* 遮罩层 */
        .mask{
            position: fixed;
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
            background-color: rgba(0,0,0,0.5);
            display: none;
            /* display: flex; */
            /* flex-direction: column; */
            justify-content: space-around;
            align-items: center;
            flex: 1;
        }
        .con {
            width: 450px;
            height: 404px;
            /* border: 1px solid; */
            background-color: #fff;
        }
        .title {
            height:40px;
            width: 450px;
            color: #db6419;
            border-bottom: 3px solid orange;
           
        }
        .title p{
            text-align: center;
            line-height: 40px;
            font-weight: 700;
            font-size: 18px;
        }
        .form input {
            width: 405px;
            height: 30px;
            text-indent: 1em;
            border-radius: 10px;
            border: #8d898a 1px solid;
        }
        .form input,
        p {
            margin-top: 0;
            margin-left: 25px;
            margin-top: 15px;
        }
        .con button {
            width: 114px;
            height: 40px;
            outline: none;
            border: none;
            justify-content: space-around;
        }
        .con p {
            font-size: 12px;
            color: #8d898a;
        }
        /* 按钮 */
        .btn{
            display: flex;
            justify-content: space-around;
            cursor: pointer;
        }
        .btn .close{
            background-color: #8b898a;
            border-radius: 15px;
        }
        .btn .login{
            background-color: orange;
            color: #fff;
            border-radius: 15px;
        }
    </style>
</head>
<body>
    <!-- 注册按钮 -->
    <button id="submitBtn">注册</button>
    <!-- 模态框 -->
    <div class="mask">
        <div class="con">
            <!-- 标题 -->
            <div class="title">
                <p>用户注册</p>
            </div>
            <!-- 表单-->
            <div class="form">
                <input class="username" type="text" placeholder="请输入用户名">
                <p>中文、字母、数字、下划线长度2-12位</p>
                <input class="email" type="email" name="" id="" placeholder="请输入邮箱">
                <p>有效的邮箱</p>
                <input class="password" type="password" name="" id="" placeholder="请输入密码">
                <p>字母、数字、下划线长度6-12位</p>
            </div>
            <!-- 按钮 -->
            <div class="btn">
                <button class="close">关闭</button>
                <button class="login">注册</button>
            </div>
        </div>
    </div>
    <!-- 最外层大盒子 -->
    <script>
        // 获取元素
        let submitBtn = document.querySelector('#submitBtn')
        let mask = document.querySelector('.mask')
        // 关闭按钮
        let close = document.querySelector('.btn .close')
        // 注册按钮
        let login = document.querySelector('.btn .login')
        // 用户名
        let username = document.querySelector('.form .username')
        let email = document.querySelector('.form .email')
        let password = document.querySelector('.form .password')
        let ps = document.querySelectorAll('.form p')
        // 正则
        let useReg =  /^[\u4E00-\u9FA5A-Za-z0-9]{2,20}$/
        let emaReg =  /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
        let psdReg = /^[a-zA-Z]\w{5,17}$/
        // 记录是否符合正则
        let useFlag = false
        let emaFlag = false
        let psdFlag = false
        // 给按钮注册点击事件
        submitBtn.addEventListener('click',function(){
            mask.style.display = "flex"
        })
        // 关闭按钮点击的时候 关闭模态框
        close.addEventListener('click',function(){
            mask.style.display="none"
        })
       
        // 遍历所有的正则验证的提示文字
        ps.forEach(function(p,index){
            // 用户名的input框注册失焦事件
            username.addEventListener('blur',function(){
                if(useReg.test(this.value)){
                    console.log(this.value);
                    ps[0].style.color="#8b898a"
                    useFlag = true
                }else{
                    ps[0].style.color="red"
                    console.log(ps[index]);
                    useFlag = false
                }
        })
        // 邮箱input框的失焦事件
        email.addEventListener('blur',function(){
                if(emaReg.test(this.value)){
                    console.log(this.value);
                    ps[1].style.color="#8b898a"
                    emaFlag = true
                }else{
                    ps[1].style.color="red"
                    emaFlag = false
                }
        })
        })
       
        // 密码的input输入框的失焦事件
        password.addEventListener('blur',function(){
            if(psdReg.test(this.value)){
                ps[2].style.color="#8b898a"
                psdFlag = true
            }else{
                ps[2].style.color="red"
                psdFlag = false
            }
        })
        //登陆
        login.addEventListener('click',function(){
            if(useFlag&&emaFlag&&psdFlag){
                location.href='https:www.baidu.com'
            }else{
                alert('请输入正确的格式')
            }
        })
    </script>
</body>
</html>

正则验证&模态框的更多相关文章

  1. 循环遍历正则验证input框内容合法性

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. js常用的一些正则验证文本框

    只允许输入数字和-onKeyUp="value=value.replace(/[^-\d]/g,'')" onafterpaste="value=value.replac ...

  3. Asp.net MVC利用Ajax.BeginForm实现bootstrap模态框弹出,并进行前段验证

    1.新建Controller public ActionResult Index() { return View(); } public ActionResult Person(int? id) { ...

  4. 模态框的理解 ,jQ: loading,进度条, 省级联动 表单验证 插件

    模态框: 打开一个弹框 不关闭它就不能做框外的操作 必须关闭或弹出另外的弹框 加载延迟loading + 进度条只要有请求 就处理一下监控ajax 全局事件jquery: $('#box').ajax ...

  5. bootstrap下使用模态框,在模态框内输入框中回车时,模态框自动关闭的问题及解决方法

    使用bootstrap下模态框,构建表单提交页面,但是输入框中直接回车,本来是想执行一下验证,但是却导致模态框自动关闭了. 遇到这样的问题,只需要先禁止回车触发表单提交            $(do ...

  6. bootstrap模态框远程加载网页的正确处理方式

    bootstrap模态框远程加载网页的方法 在bootsrap模态框文档里给出了这个方法: 使用链接模式 <a data-toggle="modal" href=" ...

  7. Bootstrap3 模态框 select2搜索框无法输入

    <div class="modal fade" role="dialog" aria-hidden="true" data-backd ...

  8. 整理:手机端弹出提示框,使用的bootstrap中的模态框(modal,弹出层),比kendo弹出效果好

    效果图: 我的代码示例: <!--提示模态框--> <div class="modal fade" id="myModal" tabindex ...

  9. 身份证真实性校验js、mini ui身份证长度正则验证

    身份证号码真实性校验 <input type="text" value="请输入身份证号" id="cards" ><bu ...

随机推荐

  1. ERROR: Command errored out with exit status 1:安装pip3 install --user pyecharts==0.5.11失败问题总结

    一.前言:最近在学习pyecharts学习到Grid时候发现代码无法运行了,经过在网上查找资料说是pyecharts版本不适配了,之前的版本是 pip install pyecharts==0.1.9 ...

  2. 小迪安全 Web安全 基础入门 - 第九天 信息打点 - CDN绕过篇&漏洞回链&接口探针&全网扫描&反向邮件

    一. CDN知识-工作原理及阻碍 (一)工作原理 1.概述:CDN的全称是Content Delivery Network,即内容分发网络.CDN是构建在现有网络基础之上的智能虚拟网络,依靠部署在各地 ...

  3. CF336A Vasily the Bear and Triangle 题解

    Content 一个矩形的顶点为 \((0,0)\),其对顶点为 \((x,y)\),现过 \((x,y)\) 作直线,分别交 \(x\) 轴和 \(y\) 轴于 \(A,B\) 两点,使得 \(\t ...

  4. LuoguB2075 幂的末尾 题解

    Content 求 \(a^b\) 的末三位. 数据范围:\(1\leqslant a\leqslant 100\),\(1\leqslant b\leqslant 10^4\). Solution ...

  5. 初识requests

    Make a Request 一开始要导入 Requests 模块: >>> import requests 然后,尝试获取某个网页.本例子中,我们来获取 Github 的公共时间线 ...

  6. pymysql-sqlalchemy-orm

    sqlalchemy示例代码 多对多外键 场景:一个作者映射多个书籍,一个书籍有多个作者 作者表: id name email sex 1 Alex alex@.. M 2 Rail rail@.. ...

  7. Nginxre quest_time 和upstream_response_time

    nginx优化之request_time 和upstream_response_time差别 https://www.cnblogs.com/dongruiha/p/7007801.html http ...

  8. centos使用docker安装redis ,并设置外网访问

    拉取镜像 docker pull redis:4.0 在主机/data/redis/conf目录下新建redis.conf文件vim /data/redis/conf/redis.conf # Red ...

  9. Xshell连接Ubuntu服务器连接不上 显示拒绝了密码

    确保下方两个都安装了 sudo apt-get install openssh-server sudo apt-get install ssh 修改 vim /etc/ssh/sshd_config ...

  10. 【LeetCode】1041. Robot Bounded In Circle 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 找规律 日期 题目地址:https://leetco ...