2日期输入验证PHP的条件 [英] 2 date input validation php with condition

查看:43
本文介绍了2日期输入验证PHP的条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想验证在Codeigniter中输入的2个日期,条件是,如果结束日期大于开始日期,则会显示警告[javascript警告或其他内容]或无法输入数据

i want to validate 2 date input in codeigniter, with the conditions, if the end date is greater than the start date, will appear warning [javascript warning or something] or data can't be input

我的表单,

<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?

how to validate in form "Waktu Akhir & Waktu Mulai" ?

推荐答案

尝试一下。它是通过使用CI验证库。
它使用回调类型的验证。

Try this. It is by using CI validation library. It uses callback type of validation.

将此内容放入 if(isset($ _ POST ['submit_button_name'])){} 部分。
首先,加载验证数组,

Put this in if(isset($_POST['submit_button_name'])) {} section. First, load validation array,

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

然后将CI验证库加载为,

Then load CI validation library as,

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

必填验证使字段必填充满了东西。
在这种情况下,回调函数比较日期,如果开始日期小于起始日期,则进一步处理表单,否则标记错误。

The "required" validation makes the fields mandatory to be filled with something. The callback function, in this case, compares the dates, and further processes the form if start date is less than from date OR flags error otherwise.

这篇关于2日期输入验证PHP的条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆