2日期输入验证PHP和条件(2 date input validation php with con

2019-08-08 06:12发布

我想要验证在笨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)" />

如何在形式“结束日期和时间开机”验证?

Answer 1:

试试这个。 它是用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;
  }
}

该“要求”验证使得场强制性要装满东西。 回调函数,在这种情况下,比较了日期,并进一步处理形式,如果开始日期小于从日期或标志,否则出错。



Answer 2:

同时,如果你在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;
}


Answer 3:

这是工作的代码

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


文章来源: 2 date input validation php with condition