关于emailid和密码验证的简单javascript代码

时间:2017-03-15 18:31:30

标签: javascript

我已经制作了emailid和密码验证的示例代码,但无法同时验证emailid和密码。任何人都可以帮我解释一下吗?

    <html>
        <head>
            <script language="javascript">
                function emailid(){
                    var a=f.emailid;
                    if(a==" "){
                        alert("emailid must not be empty");
                    }
                    else if(a.indexof("@")!=-1){
                        alert("not valid id");
                    }
                    else if(a.indexof(".")!=-1){
                        alert("not a valid id");
                    }
                    else
                    alert("valid id");
                }
                function pswd(){
                    var b=f.password;
                    if(b==" "){
                        alert("password must not be empty");
                    }
                    else if(b.length<6){
                        alert("not valid password");
                    }
                    else
                    alert("vaild password");
                }
            </script>
            <a href="submit.html"></a>
        </head>
        <body>
            <form>
                emailid<input type="text" name="emailid"><input type="button" value="validate" onclick="emailid()"><br>
                password<input type="password" name="password"><input type="button" value="validate" onclick="pswd()"><br>
                <input type="button" value="submit" onclick="submit.html">
            </form>
        </body>
    </html>

还有其他部分submit.html

<html>
    <body>
        successfully submitted
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

        

        <script type="text/javascript">
     <!--

        function validateEmail() {  
        var f = document.getElementById("myForm").elements;             
           var a=f.emailid.value;
                if(a==""){
                    alert("emailid must not be empty");
                }
                else if(a.indexOf("@") ==-1 || a.indexOf(".") ==-1){
                    alert("not valid id");
                }
                else
                alert("valid id");
        }

        function pswd(){
            var f = document.getElementById("myForm").elements;
                var b=f.password.value;
                if(b==""){
                    alert("password must not be empty");
                }
                else if(b.length<6){
                    alert("not valid password");
                }
                else
                alert("vaild password");
            };
     //-->
  </script>
    </head>
    <body>
    <a href="submit.html"></a>
        <form id='myForm'>
            emailid<input type="text" name="emailid"><input type="button" value="validate" onclick="validateEmail()"><br>
            password<input type="password" name="password"><input type="button" value="validate" onclick="pswd()"><br>
            <input type="button" value="submit" onclick="submit.html">
        </form>
    </body>
</html>