2015-05-07 111 views
0

我有一個包含字段必須填寫這樣的模式引導:驗證數據

<div class="modal hide fade" id="myModal"> 
     <div class="modal-header"> 
      <button type="button" class="close" data-dismiss="modal">×</button> 
      <h2 id="mainTitle">No.request : <strong></strong></h2> 
     </div> 

     <div class="modal-body"> 

      <table class="table table-striped table-condensed"> 
       <tbody> 
        <tr> 
         <td valign="top"> 
          <label class="control-label">Notes</label> 
         </td> 

         <td> 
          <div class="control-group "> 
           <div class="controls"> 
            <textarea class="cleditor span10" id="modalCatatan" rows="3" name="notes"></textarea> 
           </div> 
          </div> 
         </td> 
        </tr> 


        <tr> 

         <td colspan="2"><h3>Estimated over</h3></td> 
        </tr> 

        <tr> 
         <td valign="top"> 
          <label class="control-label">Date </label> 
         </td> 

         <td> 
          <div class="controls"> 
           <input type="text" class="input-small datepicker" id="tanggal" value="<?php echo date("d-m-Y"); ?>"> 
          </div> 
         </td> 
        </tr> 

        <tr> 
         <td valign="top"> 
          <label class="control-label">Hour</label> 
         </td> 

         <td> 
          <div class="controls"> 
           <select id="hour" class="input-small"> 
            <?php 
            for ($i = 8; $i <= 21; $i++) { 
             if ($i < 10) { 
              echo "<option value='" . $i . "'> 0" . $i . "</option>"; 
             } else { 
              echo "<option value='" . $i . "'>" . $i . "</option>"; 
             } 
            } 
            ?> 
           </select> 

           : 

           <select id="minute" class="input-small"> 
            <?php 
            for ($i = 0; $i <= 59; $i++) { 
             if ($i < 10) { 
              echo "<option value='" . $i . "'> 0" . $i . "</option>"; 
             } else { 
              echo "<option value='" . $i . "'>" . $i . "</option>"; 
             } 
            } 
            ?> 
           </select> 

          </div> 
         </td> 
        </tr> 
       </tbody> 
      </table> 
     </div> 

     <div class="modal-footer"> 
      <a href="#" id="btn-footer" class="btn btn-danger" req_id="<?php echo $data['id_request']; ?>">Yes</a> 
      <a href="#" id="btn-batal" class="btn" data-dismiss="modal">No</a> 
     </div> 
    </div> 

要在此模式發送了一些數據,我用ajax在jquery的這樣

$('#btn-footer').click(function(e) { 
       e.preventDefault(); 


       var id = $("#mainTitle strong").text().split("/").pop();/*ambil id*/ 
       var notes = $('#modalCatatan').val(); /*ambil comment*/ 

       var date = $('#tanggal').val(); /*ambil tanggal*/ 
       var hour = $('#hour').val(); /*ambil jam*/ 
       var minute= $('#hour').val(); 


       hour = hour + ":" + minute 

       $.ajax({ 
        url: '<?php echo base_url() . 'control_closing/kasihCatatan/' ?>', 
        type: 'POST', 
        data: {id: id, 
         notes: notes, 
         date: date, 
         hour: hour 
        }, 
        dataType: 'json', 
        success: function(obj) { 
         alert('Update Success'); 
         location.reload(); 
        } 
       }); 
      }); 

我的問題是,如何驗證上面的ajax中的數據?假設textarea是空的,它會顯示「文本區域不允許清空」的消息。感謝4的幫助。

回答

0

你嘗試過類似

if(notes.length == 0) 
{ 
    // Logic to set an error message 
    return false; 
} 

然後清除錯誤信息的點擊,或使用時刻設定和x秒後消失的錯誤消息出來