利用js編寫一個簡單的html表單驗證,驗證通過時提交資料(附原始碼)

數以沉迷發表於2019-01-12
<!DOCTYPE html>
<html lang=”en”>
<head>
    <meta charset=”UTF-8″>
    <meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
    <meta http-equiv=”X-UA-Compatible” content=”ie=edge”>
    <title>Document</title>
    <script type=”text/javascript”>
        function checknm() {
            var check = false;
            var username = document.getElementsByName(“username”)[0];
            if (1<=username.value.length && username.value.length<=10){
                console.log(document.getElementById(“checktext1″).innerHTML = ” √”)
                check = true;
            }else{
              
                document.getElementById(“checktext1″).innerHTML = ” × 最多10位且不為空”;
                check = false;
            }
            return check;
        }
       
        function checkide(){
            var check = false;
            var userid = document.getElementsByName(“userid”)[0];
            if (1<=userid.value.length && userid.value.length<=10){
                document.getElementById(“checktext2″).innerHTML = ” √”;
                check = true;
               
            }else{
                document.getElementById(“checktext2″).innerHTML = ” × 最多10位且不為空”;
                check = false;
            }
            return check;
        }

        function checkpwd(){
            var check = false;
            var userpwd = document.getElementsByName(“userpwd”)[0];
            if (6<userpwd.value.length && userpwd.value.length<=15){
                document.getElementById(“checktext3″).innerHTML = ” √”;
                check = true;
               
            }else{
                document.getElementById(“checktext3″).innerHTML = ” × 最少6位,最多15位”;
                check = false;
            }
            return check;
        }

        function checkpwd1(){
            var check = false;
            var userpwd1 = document.getElementsByName(“userpwd1”)[0];
            var userpwd = document.getElementsByName(“userpwd”)[0];
            if (userpwd1.value == userpwd.value){
                document.getElementById(“checktext4″).innerHTML = ” √密碼一致”;
                check = true;
           
               
            }else{
                document.getElementById(“checktext4″).innerHTML = ” × 兩次密碼不一致”;
                check = false;
            }
            return check;
        }

        function checkeml(){
            var check = false;
            var useremail = document.getElementsByName(“useremail”)[0];
            if (!/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/.test(useremail.value)){
                document.getElementById(“checktext5″).innerHTML = ” × 郵箱格式不規範”;
                check = false;
               
            }else{          
                document.getElementById(“checktext5″).innerHTML = ” √”;
                check = true;
            }
            return check;
        }

        function check(){
            var check = checknm() && checkide() && checkpwd() && checkpwd1() && checkeml();
            return check;
  
        }

    </script>
</head>
<body>
    <!– <a href=”./login.html”></a> –>
    <!– <button onclick=”yz()” >驗證賬號</button> –>
    <form method=”get” action=”./success.html” onsubmit = “return check()”>
        <table align=”center”>
            <tr>
                <td><label>*</label><strong>使用者名稱:</strong></td>
                <td><input type=”text” name=”username” value=”” placeholder=”使用者名稱(5位以內)” onchange=”checknm()”></td>
                <td><span id=”checktext1″></span></td>
            </tr>
            <tr>
                <td><label>*</label><strong>使用者ID:</strong></td>
                <td><input type=”text” name=”userid” value=”” placeholder=”數字ID(11位以內)” onchange=”checkide()”></td>
                <td><span id=”checktext2″></span></td>
            </tr>

            <tr>
                <td><label>*</label><strong>登入密碼:</strong></td>
                <td><input type=”password” name=”userpwd” value=”” placeholder=”請輸入密碼(6~15)” onchange=”checkpwd()”></td>
                <td><span id=”checktext3″></span></td>
            </tr>

            <tr>
                <td><label>*</label><strong>確認密碼:</strong></td>
                <td><input type=”password” name=”userpwd1″ value=”” placeholder=”請再次確認密碼” onchange=”checkpwd1()”></td>
                <td><span id=”checktext4″></span></td>
            </tr>

            <tr>
                <td><label>*</label><strong>電子郵箱:</strong></td>
                <td><input type=”text” name=”useremail” value=”” placeholder=”郵箱地址,如:123@qq.com” onchange=”checkeml()”></td>
                <td><span id=”checktext5″></span></td>
            </tr>

            <tr>
                <td></td>
                <td><input type=”submit” value=”提交”></td>
                <td><a href=””>重填</a></td>
            </td>
            </tr>
        </table>
    </form>
</body>
</html>

相關文章