0

我想验证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');
?>
&nbsp;&nbsp;<input type="button" value="Kembali" onClick="javascript: history.go(-1)" />

如何在“Waktu Akhir & Waktu Mulai”表格中验证?

4

3 回答 3

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;
  }
}

“必需”验证使字段必须填写某些内容。在这种情况下,回调函数比较日期,如果开始日期小于起始日期,则进一步处理表单,否则标记错误。

于 2013-01-25T08:10:52.573 回答
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;
}
于 2013-01-25T08:20:33.133 回答
-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;
于 2018-05-09T09:46:42.010 回答