我想在codeigniter中验证2个日期输入,条件是,如果结束日期大于开始日期,则会出现警告[javascript警告或某事]或无法输入数据
我的表格是这样的,
<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');
?>
<input type="button" value="Kembali" onClick="javascript: history.go(-1)" />
如何以“Waktu Akhir&amp; Waktu Mulai”的形式进行验证?
答案 0 :(得分:6)
试试这个。它是通过使用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;
}
}
“必需”验证使字段必须填充某些内容。 在这种情况下,回调函数会比较日期,如果开始日期小于日期,则进一步处理表单。否则,标记错误。
答案 1 :(得分: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;
}
答案 2 :(得分:-1)
这是正常工作的代码
$params['toDate'] = $this->input->post('toDate', TRUE);
$params['fromDate'] = $this->input->post('fromDate', TRUE);$this->load->model('your_model');
$this->load->library('form_validation');
$this->form_validation->set_data($params);
$startDate = strtotime($params['fromDate']);
$endDate = strtotime($params['toDate']);
if ($endDate >= $startDate):
$this->form_validation->set_rules('fromDate', 'From Date', 'required|trim');
$this->form_validation->set_rules('branchCode', 'Branch Code', 'required|trim');
else:
$json = array(
"success" => false,
"msg" => "Start date must be greater than end date"
);
echo json_encode($json);
die();
endif;