提交前的表单验证

时间:2015-02-02 06:57:12

标签: php jquery forms validation codeigniter-2

我有以下需要提供给数据库的表单,但我希望在将其保存到数据库之前进行验证:

<form name="add_walkin_patient_form" class="add_walkin_patient_form" id="add_walkin_patient_form"  autocomplete="off" >

                <div class="form-line">

                    <div class="control-group">
                        <label class="control-label">
                            Patient Name
                        </label>
                        <div class="controls">
                            <input type="text" name="patientname" id="patientname" required="" value=""/>
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label">
                            Patient Phone Number
                        </label>
                        <div class="controls">
                            <input type="text" name="patient_phone" id="patient_phone" required="" value=""/>
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label">
                            Department
                        </label>
                        <div class="controls">
                            <select name="department" required="" class="department" id="department">
                                <option value="">Please select : </option>
                                <option value="Pharmacy">Pharmacy</option>
                                <option value="Laboratory">Laboratory</option>
                                <option value="Nurse">Nurse</option>

                            </select>
                        </div>
                    </div>
                </div>

                <button name="add_walkin_patient_button" type="submit" id="add_walkin_patient_button" class="btn add_walkin_patient_button btn-info pull-right">
                    Add Walk In Patient
                </button>
            </form>

使用以下脚本通过jquery脚本完成提交:

<script type="text/javascript">

    $(document).ready(function () {
        //delegated submit handlers for the forms inside the table
        $('#add_walkin_patient_button').on('click', function (e) {
            e.preventDefault();

            //read the form data ans submit it to someurl
            $.post('<?php echo base_url() ?>index.php/reception/add_walkin', $('#add_walkin_patient_form').serialize(), function () {
                //success do something

                // $.notify("New Patient Added Succesfully", "success",{ position:"left" });
                $(".add_walkin_patient_form").notify(
                        "New Walkin Patient Added Successfully",
                        "success",
                        {position: "center"}
                );
                setInterval(function () {
                    var url = "<?php echo base_url() ?>index.php/reception/";
                    $(location).attr('href', url);
                }, 3000);


            }).fail(function () {
                //error do something
                $(".add_walkin_patient_form").notify(
                        "There was an error please try again later or  contact the system support desk  for assistance",
                        "error",
                        {position: "center"}
                );

            })
        })

    });

</script>

如何在将输入提交到脚本之前进行表单验证以检查输入是否为空?

6 个答案:

答案 0 :(得分:1)

您正在使用,

<button name="add_walkin_patient_button" type="submit" id="add_walkin_patient_button" class="btn add_walkin_patient_button btn-info pull-right">
       Add Walk In Patient
</button>

此处,submit按钮用于提交表单,永远不会触发点击事件。因为,首先会触发提交,从而导致click事件跳过。

$('#add_walkin_patient_button').on('click', function (e) {

如果您使用normal按钮而不是submit按钮

,这将有效
<input type="button">Submit</button>

现在问题。它有两个解决方案,

如果您使用点击事件,那么您应该在正确的验证案例中手动触发提交,

<input type="button" id="add_walkin_patient_button">Submit</button>
//JS : 
$("#add_walkin_patient_button").click(function() {
   if(valid){
     $("#form-id").submit();
    }

另一种选择是使用submit事件;这是在您单击提交按钮后触发的事件。在这里,您需要允许表单提交或根据您的验证标准暂停它,

$("#form-id").submit(function(){
   if(invalid){
     //Suppress form submit
      return false;
   }else{
      return true;
   }
});

P.S

我建议您按jQuery Validate

的建议使用@sherin-mathew

答案 1 :(得分:0)

使用bool返回类型创建一个javascript验证方法(例如,validateForm())。将[onsubmit =&#34; return validateForm()&#34;]属性添加到表单中,您就完成了。

答案 2 :(得分:0)

您需要阻止默认操作。

$('#add_walkin_patient_form').on('submit', function(e) {
e.preventDefault();

//Validate form and submit
});

答案 3 :(得分:0)

我正在使用javascript进行验证。 以下是表单代码:

<form action="upload.php" method="post" onSubmit="return validateForm()">
    <input type="text" id="username">
    <input type="text" password="password">
    <input type="submit" value='Login' name='login'>
</form>

要执行验证,请编写javascript函数:

<script>
  function checkform(){
    var uname= document.getElementById("username").value.trim().toUpperCase();

    if(uname=== '' || uname=== null) {
      alert("Username is blank");
      document.getElementById("username").backgroundColor = "#ff6666";
      return false;
    }else document.getElementById("username").backgroundColor = "white";

    var pass= document.getElementById("password").value.trim().toUpperCase();

    if(pass=== '' || pass=== null) {
       alert("Password is blank");
       document.getElementById("password").backgroundColor = "#ff6666";
       return false;
     }else document.getElementById("password").backgroundColor = "white";

    return true;
  }
</script>

答案 4 :(得分:0)

<html>
    <head>
        <title></title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
    
    <!-- jQuery library -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    
    <!-- Popper JS -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
    
    <!-- Latest compiled JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
    </head>
    <body>

        <style>
            #first{
                display: none;
            }
        </style>
        
        <div class="container"><br>
        
            <div class="col-lg-6 m-auto d-block">
                
                <form action="" method="" onsubmit="return validation()" class="bg-light">
                    
                    <div class="form-group">
               
                        <label for="">Title</label>
                        <span class="text-danger">*</span>
                      <!-- <input class="form-control" type="text" > -->
                      
                      <select name="title" id="title"  class="form-control"  >
                          <option value=""class="form-control" >Select</option>
                          <option value="Mr" class="form-control">Mr</option>
                          <option value="Mrs" class="form-control">Mrs</option>
                      </select>
                     
                      <span id="tit" class="text-danger font-weight-bold"> </span>
                    </div>
                    
                    <div class="form-group">
                     
                      <div class="row">
                            <div class="col">
                                <label for="firstName">FirstName</label>
                                <span class="text-danger">*</span>
                              <input type="text" class="form-control" placeholder="First name" id="firstName" >
                              <span id="first" class="text-danger font-weight-bold"> </span>
                            </div>
                            
                            <div class="col">
                                <label for="lastName">LastName</label>
                                <span class="text-danger">*</span>
                              <input type="text" class="form-control" placeholder="Last name" id="lastName">
                              <span id="last" class="text-danger font-weight-bold"> </span>
                            </div>
                          </div>
        
                    </div>
                
                    <div class="form-group">
                        <label for="email">Your Email</label>
                        <span class="text-danger">*</span>
                      <input type="text" class="form-control" placeholder="Email" id="email">
                      <span id="fillemail" class="text-danger font-weight-bold"> </span>
                    </div>
                    <div class="form-group">
                            
                                <label for="contact">Your Contact</label>
                                <span class="text-danger">*</span>
                              <input type="text" class="form-control" placeholder="Contact Number" id="contact">
                              <span id="con" class="text-danger font-weight-bold"> </span>
                            </div>
                            
                            <div class="form-group">
                                        <label for="password">Your Password</label>
                                        <span class="text-danger">*</span>
                                      <input type="text" class="form-control" placeholder="Password" id="password">
                                      <span id="pass" class="text-danger font-weight-bold"> </span>
                                    </div>
                                    
                                    <div class="form-group">
                                                <label for="conPassword">Confirm Password</label>
                                                <span class="text-danger">*</span>
                                              <input type="text" class="form-control" placeholder="Password" id="conPassword">
                                              <span id="conPass" class="text-danger font-weight-bold"> </span>
                                        </div>

                                        <div class="checkbox">
                                            <label><input type="checkbox"> I accept <a href="form.html">Terms and Conditions</a></label>
                                          </div>
                                          <div class="checkbox">
                                            <label><input type="checkbox"> I agree to recieve Email <a href="form2.html">Terms and Conditions</a></label>
                                          </div>
                                          <div class="checkbox">
                                            <label><input type="checkbox"> I agree to recieve SMS <a href="form2.html">Terms and Conditions</a></label>
                                          </div>
                                          <input type="submit" name="submit" value="SignUp" id="signUp" class="btn btn-success"     autocomplete="off">
    
                </form><br><br>
    
    
            </div>
        </div>

        <script type="text/javascript">
        

            function validation(){
    
                var title = document.getElementById('title').value;
                var firstName = document.getElementById('firstName').value;
                var email=document.getElementById('email').value;
                var contact=document.getElementById('contact').value;
                var password=document.getElementById('password').value;
                var conPassword=document.getElementById('conPassword').value;
              var signBut=document.getElementById('signUp');
             
    
    console.log(firstName);

    
                if(title == ""){
                    document.getElementById("tit").innerHTML ="  Please select the title feild first field";
                    return false;
                }
                
//                 if(firstName == "" & firstName.length<=3){
//                     document.getElementById("first").innerHTML =" Please Enter First Name";
//                     return false;
//                     document.getElementById("signUp").addEventListener("click", function(event){
//   event.preventDefault()
// });
//                 }
signBut.addEventListener('click',function(e){
  if(firstName=="" & firstName<3)
  {
    document.getElementById("first").innerHTML="Please Enter proper Name";
    e.preventDefault();
  }
},false);
             if(lastName == ""){
                    document.getElementById("last").innerHTML =" Please Enter Last Name";
                    return false;
                }
               else if(email ==""){
                    document.getElementById("fillemail").innerHTML="Please Enter Email";
                    
                }
               else if(contact ==""){
                    document.getElementById("con").innerHTML="Please Enter Your Contact";
                    
                }
               else if(password ==""){
                    document.getElementById("pass").innerHTML="Please Enter Your Password";
                    
                }
               else if(conPassword ==""){
                    document.getElementById("conPass").innerHTML="Please Confirm Password";
                    
                }
                
                
            }
        </script>
    
    
    </body>
</html>

答案 5 :(得分:0)

我认为您应该使用类型按钮 然后是jquery的eventClick函数