2013-01-25 52 views
0

我想驗證codeigniter中的2日期輸入,條件,如果結束日期大於開始日期,將出現警告[JavaScript警告或某事]或數據不能輸入2日期輸入驗證php的條件

我的形式是這樣,

<h1><?php echo $title; ?></h1> 
<form action="<?= base_url(); ?>index.php/admin/kalender/buat" method="post" enctype="multipart/form-data" name="form" id="form"> 
<?php 
echo "<p><label for='IDKategori'>Tingkatan Pimpinan :</label><br/>"; 
echo form_dropdown('IDKategori', $kategori) . "</p>"; 

echo "<label for='ptitle'>Kegiatan/Lokasi :</label><br/>"; 
$data = array('class' => 'validate[required] text-input', 'name' => 'judul', 'id' => 'ptitle', 'size' => 80); 
echo form_input($data); 

echo "<p><label for='long'>Uraian Kegiatan/Keterangan/Catatan :</label><br/>"; 
$data = array('class' => 'validate[required] text-input', 'name' => 'konten', 'rows' => '13', 'cols' => '60', 'style' => 'width: 60%'); 
echo form_textarea($data) . "</p>"; 

echo "<p><label for='ptitle'>Waktu Mulai :</label><br/>"; 
$data = array('class' => 'validate[required] text-input', 'name' => 'TanggalMulai', 'id' => 'basic_example_1'); 
echo form_input($data) . "</p>"; 

echo "<p><label for='ptitle'>Waktu Akhir :</label><br/>"; 
$data = array('class' => 'validate[required] text-input', 'name' => 'TanggalAkhir', 'id' => 'basic_example_2'); 
echo form_input($data) . "</p>"; 

echo form_submit('submit', 'Tambah Even'); 
?> 
&nbsp;&nbsp;<input type="button" value="Kembali" onClick="javascript: history.go(-1)" /> 

如何在形式 「Waktu Akhir & Waktu Mulai」 驗證?

+0

你想使用JavaScript或jQuery或CI驗證? –

+0

任何東西,我從來沒有把它全部 – Suzt

+2

你應該使用serverside(CI)和clientside(jquery)valdiation,以獲得對用戶輸入和安全應用程序的快速響應 –

回答

4

試試這個。它是通過使用CI驗證庫。 它使用回調類型的驗證。

把它放在if(isset($_POST['submit_button_name'])) {}欄目。 首先,負載驗證陣列,

$validation = array(
    array('field' => 'startDate', 'label' => 'StartDate', 'rules' => 'required|callback_compareDate'), 
    array('field' => 'endDate', 'label' => 'endDate', 'rules' => 'required|callback_compareDate'), 
); 

然後負載CI驗證庫如,

$this->form_validation->set_rules($validation); 
$this->form_validation->set_message('required', '%s is required.'); 

這是回叫功能。

function compareDate() { 
    $startDate = strtotime($_POST['startDate']); 
    $endDate = strtotime($_POST['endDate']); 

    if ($endDate >= $startDate) 
    return True; 
    else { 
    $this->form_validation->set_message('compareDate', '%s should be greater than Contract Start Date.'); 
    return False; 
    } 
} 

「必需的」驗證使得字段必須填寫某些內容。 在這種情況下,回調函數會比較日期,並在開始日期小於日期時進一步處理表單,否則標記錯誤。

+0

功能的位置? – Suzt

+0

等待,我會把所需的代碼,將引導你。 –

+0

我仍然困惑腳本位於 – Suzt

0

同時,如果你想在jquery中使用這個。

var startDate = new Date($('#startDate').val()); 
var endDate = new Date($('#endDate').val()); 

if (startDate > endDate){ 
     alert("Start Date should be less than End Date"); 
     return false; 
} 
+0

它不工作... – Suzt