qq_27930357
2015-09-06 09:30
采纳率: 100%
浏览 2.0k
已采纳

js里面我想同时用onfocus和onbulr事件可是没有反应,是我写错了吗??

 <!DOCTYPE html>
<html>
<head>
<meta charset="gbk">
<title>用户注册</title>
<script type="text/javascript"src="用户注册.js"></script>
</head>
<body>
    <form action="" onsubmit="return check();">
        <table>
            <tr>
                <td>用户名:</td>
                <td><input type="text" name="userName" onfocus="showHow();"
                    onblur="showWrong();" /><span id="userMsg"></span></td>
            </tr>
            <tr>
                <td>密码:</td>
                <td><input type="password" name="password" onfocus="showHow2();"
                    onblur="showWrong2();" /><span id="passMsg"></span></td>
            </tr>
            <tr>
                <td></td>
                <td>
                    <input type="submit" value="确定"/>
                </td>
            </tr>
        </table>
    </form>
</body>
</html>
function showHow() {
    var msg = document.getElementById("userMsg");
    msg.innerHTML = "首位为字母的4-16位数字、字母、下划线";
}
function showWrong() {
    var msg = document.getElementById("userMsg");
    var name = document.getElementById("form").userName;
    if (name.value == "") {
        msg.innerHTML = "用户名不能为空";
        return false;
    }
    if (name.value.length < 4) {
        msg.innerHTML = "用户名长度须大于4位";
        return false;
    }
    if (name.value.length > 16) {
        msg.innerHTML = "用户名长度须在16位以内";
        return false;
    }
    var reg = /^[a-zA-z]{1}\w{3,15}$/;
    var result = reg.test(name.value);
    if (result == false) {
        msg.innerHTML = "输入用户名格式错误";
        return false;
    }
    return true;
}
function showHow2() {
    var msg = document.getElementById("passMsg");
    msg.innerHTML = "4-10个字母和下划线";
}
function showWrong2() {
    var msg = document.getElementById("passMsg");
    var word = document.getElementById("form").password;
    var reg = /^[a-zA-Z_]{4,10}$/;
    if (word.value == "") {
        msg.innerHTML = "密码不能为空";
        return false;
    }
    if (word.value.length < 4) {
        msg.innerHTML = "密码长度须大于4位";
        return false;
    }
    if (word.value.length > 10) {
        msg.innerHTML = "密码长度须在10位以内";
        return false;
    }
    var result = reg.test(word.value);
    if (result == false) {
        msg.innerHTML = "输入密码格式错误";
        return false;
    }
    return true;
}
function check() {
    return showWrong() && showWrong2();
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • 已采纳
    
            function showWrong() {
                var msg = document.getElementById("userMsg");
                var name = document.getElementById("form").userName;//表单id忘记加了
    
        <form action="" onsubmit="return check();" id="form">
    
    点赞 打赏 评论

相关推荐 更多相似问题