js 密码为空显示错误

 <script>
        var user = document.getElementById("user");
        var pwd = document.getElementById("pwd");
        var user_erro = document.getElementById("user_erro");
        var pwd_erro = document.getElementById("pwd_erro");
        //focus 获得焦点
        user.addEventListener("blur", function () {
            check_length(user); //传递参数
        }, false);
        pwd.addEventListener("blur", function () {
            check_length(pwd);
        }, false);

        function check_length(obj) {
            var that = obj;
            if (that.value.length < 1) {
                that.style.borderColor = 'rgb(255, 76, 82)'; //为空
                show_dom(that.id,"block");
            } else {
                that.style.borderColor = 'rgb(17, 194, 109)'; //不为空
                show_dom(that.id,"none");
            }
        }
        function show_dom(obj,disp) {
            switch (obj) {
                case "user":
                    user_erro.style.display = disp;
                    break;
                case "pwd":
                    pwd_erro.style.display = disp;
                    break;
            }
        }

    </script>

posted @ 2019-11-20 18:07  enych  阅读(568)  评论(0编辑  收藏  举报