利用JS实现简单校验

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>JS简单校验</title>
        <script>
            function checkForm(){
                var username = document.getElementById("username").value;
                var password = document.getElementById("password").value;
                var email = document.getElementById("email").value;
                if(username.length <= 6){
                    alert("用户名太短,请重新输入!");
                    return false;
                }
                if(password.length <= 8){
                    alert("密码太短,请重新输入!");
                    return false;
                }
                if(!/^[\w.-]+@[\w.-]+\.\w+$/.test(email)){
                    alert("邮箱格式错误,请重新输入!");
                    return false;
                }
                return true;
            }
        </script>
    </head>
    <body>
        <div>
            <form action="../../CSS/CSS美化网页/网站首页美化版.html" method="get" onsubmit="return checkForm()">
                <table>
                    <tr>
                        <td>用户名</td>
                        <td>
                            <input type="text" id="username" />
                        </td>
                    </tr>
                    <tr>
                        <td>密码</td>
                        <td>
                            <input type="password" id="password" />
                        </td>
                    </tr>
                    <tr>
                        <td>邮箱</td>
                        <td>
                            <input type="text" id="email" />
                        </td>
                    </tr>
                    <tr>
                        <td></td>
                        <td>
                            <input type="submit" value="提交" />
                        </td>
                    </tr>
                </table>
            </form>
        </div>
    </body>
</html>

猜你喜欢

转载自www.cnblogs.com/yxfyg/p/12642921.html