在日常生活中,凡是需要表单验证的都会用到正则验证.下面拿一个简单的带有模态框的正则验证的小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. Jenkins配置管理工具

    安装完成后,配置管理工具 安装并配置git yum -y install git 编辑git环境变量为/usr/bin/git 安装并配置maven wget https://mirrors.tuna ...

  2. 如何用PHP/MySQL为 iOS App 写一个简单的web服务器(译) PART1

    原文:http://www.raywenderlich.com/2941/how-to-write-a-simple-phpmysql-web-service-for-an-ios-app 作为一个i ...

  3. python selenium 多账户自动登入163邮箱

    pycharm一些快捷键: ' ctrl ' +' / ' :注释 ' Tab ' :同时缩进 ' shift ' +' Tab ' :左移 一次缩进 本文webinfo.txt路径:C:\Pytho ...

  4. CF508A Pasha and Pixels 题解

    Content 有一个 \(n\times m\) 的矩阵,一开始全部格子被染成白色. 接下来有 \(k\) 个操作,每一个操作表示把一个格子染成黑色.问第一次出现 \(2\times 2\) 的全部 ...

  5. CF710C Magic Odd Square 题解

    Content 构造出一个 \(n\times n\) 的矩阵,使得这个矩阵由 \(1\sim n^2\) 这些数字组成,并且这个矩阵的每行,每列,以及对角线的和都为奇数. 数据范围:\(1\leqs ...

  6. Shell字体颜色

    输出特效格式控制: \033[0m 关闭所有属性 \033[1m 设置高亮度 \03[4m 下划线 \033[5m 闪烁 \033[7m 反显 \033[8m 消隐 \033[30m -- \033[ ...

  7. JuiceFS 数据加密原理

    JuiceFS 作为分布文件系统,每天与海量的数据打着交道,因此数据的安全性尤为关键,今天就来介绍一下 JuiceFS 在数据加密方面所做的努力. 传输中数据加密 JuiceFS 在网络上传输时会对数 ...

  8. SpringBoot启动报错:ould not be registered. A bean with that name has already been defined in file and overriding is disabled.

    SpringBoot启动报错 ***************************APPLICATION FAILED TO START*************************** Des ...

  9. 【LeetCode】264. Ugly Number II 解题报告(Java & Python)

    标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ https://leetcode.com/prob ...

  10. 【LeetCode】753. Cracking the Safe 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/cracking ...