简体   繁体   English

使用java脚本进行表单验证

[英]Form validation with java script

Trying to validate a form but I am facing constant problems.试图验证表单,但我一直面临着问题。 Here is the code 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: 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>

The form keeps submitting itself.表单不断提交自己。 It works for the first if statement but after that it ignores the two else if and submits itself.它适用于第一个 if 语句,但之后它会忽略两个 else if 并提交自己。 Even if I comment out this statement, it still submits to signup.php即使我注释掉这个声明,它仍然提交给 signup.php

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

At the moment I have no idea why it is submitting so I am adding the php code aswell.目前我不知道它为什么要提交,所以我还要添加 php 代码。

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

$date = array();

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

I added this bit of code in the signup.php file for an extra check but I have seen that it strightup submits to signup.php.我在 signup.php 文件中添加了这段代码以进行额外检查,但我已经看到它直接提交到 signup.php。

EDIT: Updated answer to updated question编辑:更新问题的更新答案

Your problem might be related to the fact that you have this line of code:您的问题可能与您拥有以下代码行的事实有关:

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

before the elseif , which might break the if elseif flow.elseif之前,这可能会破坏if elseif流程。

Try using this code instead:尝试改用此代码:

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();
    }
}

Try with:尝试:

<input type="button"

instead:反而:

type="submit"

Edit: otherwise your .submit() function is useless.编辑:否则你的 .submit() 函数是无用的。 -2 ? -2 ? Tell me why using .submit() if the form as already submit type ?如果表单已经提交类型,请告诉我为什么使用 .submit() ?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM