0

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

my form like this,

<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)" />

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

3
  • you want to use javascript OR jquery or CI validation ? Commented Jan 25, 2013 at 8:08
  • anything, I've never taken it all Commented Jan 25, 2013 at 8:09
  • 2
    You should use both, serverside (CI) and clientside (jquery) valdiation, to get a fast response to users inputs and a secure application Commented Jan 25, 2013 at 8:34

3 Answers 3

6

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

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'),
);

Then load CI validation library as,

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

This is the called back function.

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.

Sign up to request clarification or add additional context in comments.

5 Comments

where the function is placed?
I'm still confused the script located
see, except method definition, (the last code part), put everything else above it in if(isset($_POST['submit'])){}; Then put the method definition in the last.
how the installation on my script above
0

Meanwhile, if you want in Jquery you can use this.

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

Comments

-1

This is working code

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.