簡體   English   中英

我的頁面中有表單驗證 function 但它不起作用

[英]I have Form validation function in my page but it is not working

我有 3 個字段第一個input for image title select for year和圖像input type file 但是我想在按下update按鈕時檢查圖像標題是否插入。 我正在使用表單驗證,但它不起作用。

我的代碼:

<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>assets/admin/plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.min.css">
  <div class="content-wrapper">
    <section class="content-header">
      <h1><u>Heading </u></h1>
      <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-home"></i> Home Content</a></li>
        <li class="active"> Topic </li>
      </ol>
    </section>
     <section class="content">    
      <div class="row">
       <div class="col-md-12 col-sm-12 col-xs-12 col-lg-12">
        <h3 class="page-header"></h3>
      </div> 
     <div class="col-lg-offset-1 col-md-12 col-sm-12 col-xs-12 col-lg-10">

     <div class="col-md-12">
      <div class="box box-primary">
        <div class="box box-default">
                      <div class="box-header with-border">
                        <h3 class="box-title">Add Gallary Images</h3>
                        <div class="box-tools pull-right">
                          <button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
                          <button type="button" class="btn btn-box-tool" data-widget="remove"><i class="fa fa-remove"></i></button>
                        </div>
                      </div>
          <!-- /.box-header -->
          <div class="box-body">
            <form id="add_gallary_form" method="post">

                     <div class="form-group">
                       <label>Image Title</label>
                       <input type="text" class="form-control" name="img_title" id="img_title" placeholder="Enter your Image Title" dir="" autocomplete="off" required>
                     </div>
                     <div class="form-group">
                       <label>Year</label>
                        <select class="form-control" name="year" id="year" required>
                          <?php
                          foreach(range(1950, (int)date("Y")) as $year) {
                              echo "\t<option value='".$year."'>".$year."</option>\n\r";
                          }
                          ?>
                          </select>
                     </div>

                     <div class="form-group">
                       <label>Image</label>
                         <input type="file" id="upload_image" name="upload_image" class="col-md-8 form-control"  multiple />
                         <div class="form-group">
                          <div class="col-md-12" id="uploaded_images" ></div>
                      </div>
                     </div>

                    <div class="box-footer">
                          <button type="submit" id="btn-insert" class="btn btn-success col-lg-8 col-md-offset-2">Insert</button>
                    </div>
            </form>
          </div>
        </div>
      </div>
    </div>
  </div>
  </div>
  </div>
  </section>
  <script src="<?php echo base_url();?>assets/admin/assets/jquery/jquery-3.2.1.min.js"></script>
  <script src="<?php echo base_url();?>assets/admin/bootstrap/js/bootstrap.min.js"></script>
  <script src="<?php echo base_url();?>assets/admin/plugins/fastclick/fastclick.js"></script>
  <script src="<?php echo base_url();?>assets/admin/dist/js/app.min.js"></script>
  <script src="<?php echo base_url();?>assets/admin/plugins/slimScroll/jquery.slimscroll.min.js"></script>
  <script src="<?php echo base_url();?>assets/admin/dist/js/demo.js"></script>
  <script type="text/javascript" src="<?php echo base_url(); ?>assets/admin/plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.all.min.js"></script>
  <script src="https://gitcdn.github.io/bootstrap-toggle/2.2.2/js/bootstrap-toggle.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.0/jquery.validate.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.0/additional-methods.min.js"></script>

   <script>
 $("#add_gallary_form").validate({
  rules:{
    "img_title":{
  required: true,
  },

  },
messages:{
  "img_title":{
  required: "Please Enter Image Title",
  },
},
});
   </script>
  <script type="text/javascript">
      $("#btn-insert").click(function(e){
                e.preventDefault();
                var img_title = $('#img_title').val();
                var year = $('#year').val();
                if ( $('#upload_image').val() =='') {
                    alert("Please Select Image File");
                }else{
                      var files = $('#upload_image')[0].files;
                      var error = '';
                      var formData=new FormData();
                      formData.append('img_title',img_title);
                      formData.append('year',year);

                      for (var count = 0; count <files.length; count++) {
                           var name = files[count].name;
                           var extension = name.split('.').pop().toLowerCase();
                        if (jQuery.inArray(extension,['gif','png','jpg','jpeg'])== -1) {
                            error += "Invalid " + count +" Image File " 
                        }else{
                          formData.append("files[]",files[count]);
                        }
                      }
                      if (error=='') {
                            $.ajax({
                                   url: "<?php echo base_url();?>admin_controllers/WebsiteContentController/insertImage",
                                    type: "POST",
                                    data: formData,
                                    contentType:false,
                                    cache:false,    
                                    processData:false,
                                    beforeSend: function(){
    $('#uploaded_images').html("<div style='margin: auto; text-align: center;'><img src='<?php echo base_url();?>/assets/images/uploads/loader.gif'><label class='text-success' align='center'>Uploading...</label></div>");
                                    },
                                    success: function(data)
                                    {
                                      $('#uploaded_images').html(data);
                                      alert('Image successfully added')
                                     setTimeout(function(){location.reload();},1000);          
                                    }
                                  });
                        }else{
                          alert(error);
                        }
                } 
           });
  </script>

我的數據是直接插入的,沒有檢查表單驗證。 我不知道我的代碼哪里錯了。 如何驗證我的圖片標題。

var img_title = $('#img_title').val();正下方添加以下代碼

if(img_title=="")
{
  alert("Enter Image title");
  return false;
}

您可以在 btn-send 按鈕單擊事件中進行驗證。

使用此代碼將檢查img_title變量而不是 null 和空也檢查未定義。

我希望它對你有用

 if(img_title)
    {
      alert("Enter Image title");
      return false;
    }

單擊提交按鈕時嘗試此操作

<script type="text/javascript">
        $(function () {
            $("#btn-insert").click(function () {
            var img = $('#img_title').val();
             if(img == ""){
                alert("Enter the image name");
                return false;
             }
            return true;});
        });
    </script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM