在日常生活中,凡是需要表单验证的都会用到正则验证.下面拿一个简单的带有模态框的正则验证的小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. Redis - 2 - 聊聊Redis的RDB和AOF持久化 - 更新完毕

    1.RDB 1.1).RDB是什么? RDB,全称Redis Database RDB是Redis进行持久化的一种方式,当然:Redis默认的持久化方式也是RDB 1.2).Redis配置RDB 1. ...

  2. how2heap libc2.31学习

    今天是四月十九,想在五月份之前把how2heap中的高版本(2.31)的例子过一遍.所以这个系列目前还是在更新中.如果比较简单就几句话带过了,遇到难一点的会写的详细一点. fastbin_dup 源代 ...

  3. [BUUCTF]REVERSE——reverse1

    reverse1 附件 首先检查一下附件程序,64位,没有壳 64位ida载入,shift+f12检索程序里的字符串 我们得到了this is flag的提示,而且还看见了一个类似flag的字符串{h ...

  4. 《Power Query数据清洗实战》捉虫……

    先道歉,<Power Query数据清洗实战>里,有虫-- 谢谢大家帮忙捉虫了. 谢谢法叔,他捉了四只--(汗) 112页第倒第二行,[追加查询],应是[合并查询]. 151.154.15 ...

  5. Sentinel-Go 源码系列(三)滑动时间窗口算法的工程实现

    要说现在工程师最重要的能力,我觉得工程能力要排第一. 就算现在大厂面试经常要手撕算法,也是更偏向考查代码工程实现的能力,之前在群里看到这样的图片,就觉得很离谱. 算法与工程实现 在 Sentinel- ...

  6. CF544A Set of Strings 题解

    Content 有一个长为 \(n\) 的字符串 \(q\),试问能否将其划分为 \(k\) 个子串,使得每个子串的首字母都不相等,可以的话输出 \(\texttt{YES}\) 并输出任意一个方案, ...

  7. AT4811 [ABC160D] Line++ 题解

    Content 给定一个 \(n\) 个点.\(n\) 条边的无向图.对于所有的 \(1\leqslant i<n\),在点 \(i,i+1\) 之间连一条无向边.另外在给定两个点 \(x,y\ ...

  8. mail如何在linux中发送邮件,使用163邮箱发信。

    如何在linux中发送邮件,使用163邮箱发信.   linux中,可以使用mail命令往外发送邮件,在使用前,只需要指定如下简单配置即可,这里演示用  163.com    邮箱发送至 qq.com ...

  9. Elasticsearch 和 solr 的区别

    背景:它们都是基于Luence搜索服务器基础之上开发的一款优秀高性能的企业级搜索服务器.也都是基于分词技术构建的倒排索引的方式进行查询 开发语言:java 诞生时间: solr  :2004年 es ...

  10. centos7 升级php版本到7.2

    #自带的只有5.4版本 yum provides php [root@localhost etc]# yum provides php Loaded plugins: fastestmirror, l ...