无法使用JavaScript验证表单

时间:2018-12-16 14:30:27

标签: javascript forms validation

我正在尝试使用JavaScript验证表单中存在的各个字段。但是,这些字段尚未得到验证。空字段被接受并且电子邮件模式不匹配。

    <script type="text/javascript">
        function validateform(){
            var username=document.myform.username.value;
            var password=document.myform.password.value;
            var course=document.myform.course.value;
            var x=document.myform.email.value;
            var atposition=x.indexOf("@");
            var dotposition=x.lastIndexOf(".");

            if (username==null || username==""){
                alert("Name can't be blank");
                return false;
            } if(password.length<6){
                alert("Password must be at least 6 characters long.");
                return false;
            }
            if (course==null || course==""){
                alert("Course can't be blank");
                return false;
        }
            if (atposition<1 || dotposition<atposition+2 || dotposition+2>=x.length){
                alert("Please enter a valid e-mail address \n atpostion:"+atposition+"\n dotposition:"+dotposition);
                return false;
            }
    </script>
    <br>
    <title>Insert Operation</title>
</head>
<body>

<div id="box">
<form name="myform" method="POST" action="Insert.php" onsubmit="return validateform()">
    <div id="textbox">
    Username: <input type="text" name="username"><br><br>
    Password: <input type="password" name="password"><br><br>
    Email: <input type="text" name="email"><br><br>
    Course:<input type="course" name="course"><br><br>
   <input class="btn" type="Submit" value="Insert" name="done">
    </div>
</form>

0 个答案:

没有答案