如何在Codeigneter中验证应该小于18岁的出生日期

时间:2014-09-01 08:01:13

标签: php codeigniter

我使用了很多东西,但我无法确认出生日期应该少于18岁

<?php echo form_error('DOB'); ?>
<div class="form-group">
<label class="col-sm-3 control-label" for="form-field-3" >
  Date Of Birth
</label>
<div class="col-sm-8">
<input type="text"  class="form-control" id="datepicker" name="DOB" placeholder="Date Of Birth" value="<?php echo set_value('DOB'); ?>"  >

1 个答案:

答案 0 :(得分:1)

或者,您可以使用回调。例如:

public function your_form_page()
{
    $this->load->library('form_validation');
    // this part is when you set your validation rules
    $this->form_validation->set_rules('DOM', 'Date of Birth', 'trim|required||callback_validate_age');
    $this->form_validation->set_message('validate_age','Member is not valid!');
}

// you need to add this on the controller, this will be the custom validation
public function validate_age($age) {
    $dob = new DateTime($age);
    $now = new DateTime();
    return ($now->diff($dob)->y > 18) ? false : true;
}