JavaScript(三十四)正则表达式+表单验证demo

正则表达式知识点

效果图展示

采用的是正则表达式进行判断的一次性表单验证,点击注册以后才会判断格式是否都正确!

全部代码

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href="./css/iconfont.css"/>
    <style>
        #formuser {
            display: block;
            margin: 0 auto;
            width: 400px;
        }

        .form_user > li {
            list-style: none;
            height: 50px;
            width: 400px;
            border: 1px solid silver;
            border-radius: 5px;
            position: relative;
            margin: 40px 0;
        }

        .form_user > li.litxt > input {
            margin-left: 1px;
            width: 378px;
            height: 48px;
            border-style: none;
            outline: none;
            font-size: 20px;
            color: black;
            padding-left: 20px;

        }

        .tel {
            position: relative;
            border-style: none !important;
        }

        .usertelno {
            width: 150px;
            height: 50px;
            border: 1px solid silver;
            border-radius: 5px;
            line-height: 50px;
            position: relative;
            float: left;
        }

        .usertelno > input {
            position: absolute;
            top: 0;
            left: 1px;
            width: 120px;
            height: 48px;
            padding-left: 20px;
            border-style: none;
            font-size: 20px;
            outline: none;
        }

        #icon {
            position: absolute;
            right: 10px;
            top: 0;
            z-index: 2;
            color: gray;
        }

        .ph {
            float: right;
            width: 210px;
            height: 50px;
            border-radius: 5px;
            border: 1px solid silver;
            font-size: 20px;
            padding-left: 20px;
            outline: none;
        }

        .btn > input {
            width: 400px;
            height: 50px;
            outline: none;
            border-style: none;
            background-color: transparent;
            color: white;
            font-size: 22px;
        }

        .btn {
            background-color: #2e7fff;
        }

        .btn:hover {
            background-color: #40a6ff;
        }

        .telerror {
            clear: left;
            display: block;
            font-size: 14px;
            color: gray;
            position: relative;
            top: 5px;
        }

        .emailshow {
            display: block;
            font-size: 14px;
            color: gray !important;
            position: absolute;
            top: 53px;
        }

        .error {
            font-size: 14px;
            color: red;
            position: absolute;
            top: 53px;
            left: 0;
            background-image: url("./img/error@2x.png");
            background-repeat: no-repeat;
            background-size: 20px;
            padding-left: 25px;
        }

        .success {
            display: none;
            position: absolute;
            top: 15px;
            right: 10px;
        }
    </style>
</head>
<body>
<form id="formuser" action="" method="get">
    <ul class="form_user">
        <li class="litxt">
            <input type="text" class="userData" name="email" placeholder="邮箱" value=""/>
            <span class="emailshow">请输入你常用的邮箱(非qq/foxmail邮箱)</span>
            <img class="success" src="./img/tick.png" alt=""/>
        </li>
        <li class="litxt">
            <input type="text" class="userData" name="nickname" placeholder="昵称" value=""/>
            <img class="success" src="./img/tick.png" alt=""/>
        </li>
        <li class="litxt">
            <input type="password" class="userData" name="password" placeholder="密码" value=""/>
            <img class="success" src="./img/tick.png" alt=""/>
        </li>
        <li class="tel">
            <div class="usertelno">
                <input id="telnum" type="text" value="+86"/>
                <i id="icon" class=" iconfont">&#xf01a6;</i>
            </div>
            <input class="ph" placeholder="手机号码" type="text"/>
            <span class="telerror">可通过该手机号找回密码</span>
        </li>
        <li class="btn"><input type="submit" value="立即注册"/></li>
    </ul>
</form>

<script>
    var emailshow = document.getElementsByClassName("emailshow")[0];
    var formuser = document.getElementById("formuser");
    var userData = document.getElementsByClassName("userData");
    var litxt = document.getElementsByClassName("litxt");
    var success = document.getElementsByClassName("success");

    var errorinfo = [
        {
            name: "邮箱",
            error: [
                {info: "邮箱不能为空!"},
                {info: "邮箱格式不正确!"}
            ]
        },
        {
            name: "昵称",
            error: [
                {info: "昵称不能为空!"}
            ]
        },
        {
            name: "密码",
            error: [
                {info: "密码不能为空!"},
                {info: "密码必须包含数字和字母"},
                {info: "密码必须在6-8位!"}
            ]
        }
    ];
    formuser.onsubmit = function () {
        emailshow.style.display = "none";
        for (var i = 0; i < userData.length; i++) {
            if (userData[i].value.match(/./) == null) {
                addEvent("error", i, 0);
            }
            else {
                //不为0  判断格式
                if (i == 0) {
                    var emailReg = /^[A-Za-z\d]+([-_.][a-zA-Z\d]+)*@([a-zA-Z\d]+[.])+[a-zA-Z\d]{2,4}$/;
                    if (userData[i].value.match(emailReg)) {
                        success[i].style.display = "block";
                        addEvent("success", i, null);
                    }
                    else {
                        addEvent("error", i, 1);
                    }
                }
                if (i == 1) {
                    success[i].style.display = "block";
                    addEvent("success", i, null);
                }
                if (i == 2) {
                    if (userData[i].value.length >= 6 && userData[i].value.length <= 8) {

                        var pwd=/^([a-zA-Z_]+(\d)+[A-Za-z0-9]*)|((\d)+[a-zA-Z_]+([A-Za-z0-9]*))$/;
                        //以字母或下划线开头,必须要有数字或 以数字开头 必须有字母或下划线
//                        var pwd = /^(([a-zA-Z_])+(\d)+([a-zA-Z0-9]*))+|((\d)+([a-zA-Z_])+([a-zA-Z0-9]*))$/;
                        if (userData[i].value.match(pwd)) {
                            success[i].style.display = "block";
                            addEvent("success", i, null);
                        }
                        else {
                            addEvent("error", i, 1);
                        }
                    }
                    else {
                        addEvent("error", i, 2);
                    }

                }
            }
        }
        var len = document.getElementsByClassName("error").length;
        if (len) {
            return false;
        }
        return true;
    }


    function addEvent(res, index, num) {
        if (litxt[index].lastElementChild.className == "error") {
            litxt[index].lastElementChild.remove();
        }
        if (res == "error") {
            success[index].style.display = "none";
            var span = document.createElement("span");
            span.className = "error";
            span.innerHTML = errorinfo[index].error[num].info;
            litxt[index].appendChild(span);
        }
    }
</script>
</body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值