JS 基础篇(策略模式-表单验证案例)

<!DOCTYPE html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title></title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="">
    <style>
        ul {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        .fn-textcenter {
            text-align: center;
        }

        .main {
            max-width: 1000px;
            margin: 0 auto;
            zoom: 1;
        }

        .main .top {
            text-align: center;
        }

        .list ul li {
            padding: 10px;
        }

        .list ul li label {
            width: 40%;
            display: inline-block;
            padding: 0 10px;
            text-align: right;
            vertical-align: middle;
        }

        .list ul li input {
            border: 1px solid #e2e2e2;
            padding: 10px;
            width: 200px;
        }

        .list ul li input.valid {
            border: 1px solid green;
        }

        .list ul li input.error {
            border: 1px solid red;
        }

        .list ul li .btn-blue {
            background: #06a5e1;
            color: #fff;
            text-align: center;
            display: inline-block;
            padding: 5px 30px;
            cursor: pointer;
            border-radius: 5px;
            -webkit-border-radius: 5px;
            -moz-border-radius: 5px;
            -o-border-radius: 5px;
        }

        .list ul li .btn-blue:disabled {
            background: #999;
            cursor: not-allowed;
        }

        .list ul li .btn-blue:not([disabled]):hover {
            opacity: 0.8;
            filter: alpha(opacity=80);
            /* 针对 IE8 以及更早的版本 */
        }

        .list input:focus {
            border-color: #66AFE9;
            box-shadow: 0 1px 1px rgba(0, 0, 0, 0.075) inset, 0 0 8px rgba(102, 175, 233, 0.6);
            outline: 0 none;
        }

        .list .tip-error {
            background-color: #F2DEDE;
            border-color: #EED3D7;
            color: #B94A48;
            vertical-align: middle;
            padding: 5px 30px;
            display: none;
        }

        .error-msg {
            color: red;
            font-size: 12px;
        }
    </style>
</head>

<body>
    <div class="main">
        <div class="top">
            <div class="">
                <h2>注册</h2>
            </div>
        </div>

        <!-- 用户名不能为空。
     密码长度不能少于 6 位。 
     手机号码必须符合格式 -->
        <form id="registerForm">
            <div class="list">
                <ul>
                    <li>
                        <label>用户名</label>
                        <input type="text" name="userName" class="name" placeholder="输入您的用户名">
                    </li>
                    <li>
                        <label>密码</label>
                        <input type="password" name="password" class="pwd" placeholder="输入您的密码">
                    </li>
                    <li>
                        <label>手机</label>
                        <input type="text" name="phoneNumber" class="mobile" placeholder="输入您的手机">
                    </li>
                    <li class="fn-textcenter"><button type="button" id="btnSubmit"
                            class="btn-blue btn-submit">提交</button></li>
                </ul>
            </div>
    </div>
    </form>

    <script>
        var checkResult = {
            errorTip: function (dom, errMsg) {
                var errDom;
                //先判断li后面有没有error-msg这dom 
                var errmsgT = dom.parentElement.querySelector('.error-msg');
                if (!errmsgT) {
                    errDom = document.createElement('span');
                    errDom.className = 'error-msg';
                    errDom.innerText = errMsg;
                    dom.parentElement.appendChild(errDom);
                }
            },
            ok: function (dom) {
                //移除所有的提示,并把input里红色部分去掉,加上正确的提示  
                dom.classList.remove('error');
                dom.classList.add('valid');
                var errmsgT = dom.parentElement.querySelector('.error-msg');
                if (errmsgT) {
                    dom.parentElement.querySelector('.error-msg').remove();
                }
            },
            no: function (dom, errMsg) {
                dom.classList.remove('valid');
                dom.classList.add('error');
                this.errorTip(dom, errMsg);
            }
        }

        // 规则集
        // 用策略模式重构表单校验
        //  strategies['minLength']('ss',6,'error')
        var strategies = {
            minLength: function (errMsg, length) {
                if (this.value.length < length) {
                    checkResult.no(this, errMsg);
                    return errMsg;
                } else {
                    checkResult.ok(this);
                }
            },
            isNumber: function (errMsg) {
                if (!/\d+/.test(this.value)) {
                    checkResult.no(this, errMsg);
                    return errMsg;
                } else {
                    checkResult.ok(this);
                }
            },
            required: function (errMsg) {
                if (this.value === '') {
                    checkResult.no(this, errMsg);
                    return errMsg;
                } else {
                    checkResult.ok(this);
                }
            },
            isMobile: function (errMsg) {
                if (!/(^1[3|5|8][0-9]{9}$)/.test(this.value)) {
                    checkResult.no(this, errMsg);
                    return errMsg;
                } else {
                    checkResult.ok(this);
                }
            }
        };

        // 校验器
        function Validator() {
            this.items = [];
        };

        Validator.prototype = {
            constructor: Validator,
            // 添加校验规则
            add: function (dom, rule) {
                for (let i = 0, len = rule.length; i < len; i++) {
                    var strategy = rule[i].strategy;
                    var errorMsg = rule[i].errorMsg;

                    //特殊处理 
                    if (strategy.indexOf('minLength') !== -1) {
                        var temp = strategy.split(':');
                        var minLen = temp[1]
                        strategy = temp[0];
                    }
                    console.log(strategy);
                    this.items.push(strategies[strategy].bind(dom, errorMsg, minLen));
                }
            },

            // 开始校验
            start: function () {
                for (var i = 0; i < this.items.length; ++i) {
                    var ret = this.items[i]();
                    if (ret) {
                        console.log('oooo');
                        // break;        
                    }
                }
            }
        }

        var validate = new Validator();
        var registerForm = document.getElementById('registerForm');
        validate.add(registerForm.userName, [{
            strategy: 'required',
            errorMsg: '用户名不能为空'
        }, {
            strategy: 'isNumber',
            errorMsg: '只能为数字'
        }]);
        validate.add(registerForm.password, [{
            strategy: 'required',
            errorMsg: '密码不能为空'
        }]);
        validate.add(registerForm.phoneNumber, [{
            strategy: 'isMobile',
            errorMsg: '请输入正确的手机号'
        }]);

        document.getElementById('btnSubmit').onclick = function () {
            var ret = validate.start();
            console.log(ret);
        }
        registerForm.querySelectorAll('input').forEach(function (option, index) {
            option.addEventListener('blur', function (ev) {
                var ret = validate.start();
            })
        })
    </script>
</body>

</html>
©️2020 CSDN 皮肤主题: 代码科技 设计师:Amelia_0503 返回首页