使用Java脚本进行表单验证

时间:2018-08-28 11:32:33

标签: javascript php html

尝试验证表单,但我一直遇到问题。这是代码 HTML:

 <form id="regForm" class="form-group" method="POST" action="signup.php">
                                <div class="col-md-12">
                                        <h2>Job Pocket</h2>
                                </div>
                                <div class="col-md-12">
                                    <input placeholder="email" class="form-control"type="text" name="email" id="email">
                                </div>
                                <div class="col-md-12">
                                    <input placeholder="password"  class="form-control" type="password" name="password" id="password">
                                </div>
                                <div class="col-md-12">
                                    <input placeholder="confirm password"  class="form-control" type="password" name="confirmpass" id="confirmpass">
                                </div>
                                <div class="container">
                                    <div class="row">
                                        <div class="col-md-6">
                                            <input placeholder="first name"  class="form-control" type="text" name="first_name" id="first_name">
                                        </div>
                                        <div class="col-md-6">
                                            <input placeholder="last name"  class="form-control" type="text" name="last_name" id="last_name">
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-12">
                                    <input type="submit"  onclick="return validation()"class="btn btn-primary"name="submitsignup" id="submitsignup" value="submit">
                                </div>
                                <hr>
                            </form>
                        </div>
                    </div>              
                </div>
            </div>
        </div>
        </main>
        <p id="mg"></p>
    </div>

JavaScript:

<script type="text/javascript">
    function validation(){
      if(document.getElementById("email").value=="" || document.getElementById("password").value=="" || document.getElementById("last_name").value=="" || document.getElementById("first_name").value==""){

            document.getElementById("mg").innerHTML="Fill all fields";
            return false; 
        }
        var emails = document.getElementById("email").value;

        else if(emails.indexOf('@') <= 0){
            document.getElementById('mg').innerHTML =" ** @ Invalid Position";
            return false;
        }

        else if((emails.charAt(emails.length-4)!='.') && (emails.charAt(emails.length-3)!='.')){
            document.getElementById('mg').innerHTML =" ** . Invalid Position";
            return false;
        }

        else {
        document.getElementById("regForm").submit();
        }
}
</script>

表格不断提交。它适用于第一个if语句,但是此后它将忽略另外两个if并提交自己。 即使我注释掉此声明,它仍然会提交到signup.php

document.getElementById("regForm").submit();

此刻,我不知道为什么要提交,所以我还要添加php代码。

if(isset($_POST['submitsignup'])){

$date = array();

if($_POST['email']=='' || $_POST['password']=='' || $_POST['first_name']=='' || $_POST['last_name']==''){
    $template->error="Please fill all fields";
           }}

我在signup.php文件中添加了这段代码,以进行额外检查,但我看到它的strightup提交给signup.php。

2 个答案:

答案 0 :(得分:1)

编辑:已更新问题的更新答案

您的问题可能与以下代码行有关:

var emails = document.getElementById("email").value;

elseif之前,这可能会中断if elseif流程。

尝试使用此代码代替

function validation(){
    var emails = document.getElementById("email").value;
  if(emails=="" || document.getElementById("password").value=="" || document.getElementById("last_name").value=="" || document.getElementById("first_name").value==""){
        document.getElementById("mg").innerHTML="Fill all fields";
        return false; 
    }
    else if(emails.indexOf('@') <= 0){
        document.getElementById('mg').innerHTML =" ** @ Invalid Position";
        return false;
    }
    else if((emails.charAt(emails.length-4)!='.') && (emails.charAt(emails.length-3)!='.')){
        document.getElementById('mg').innerHTML =" ** . Invalid Position";
        return false;
    }
    else {
        document.getElementById("regForm").submit();
    }
}

答案 1 :(得分:-2)

尝试:

<input type="button"

相反:

type="submit"

编辑:否则您的.submit()函数无效。 -2?告诉我如果表单已提交类型,为什么要使用.submit()?