如何验证出生日期应该是小于18岁Codeigneter

如何验证出生日期应该是小于18岁Codeigneter

问题描述:

我用这么多的东西,但我无法验证出生日期应该是小于18岁如何验证出生日期应该是小于18岁Codeigneter

<?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'); ?>" > 

另外,您可以为此使用回调。例如:

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

哪里调用validate_age($岁)和如何通过价值我在codeignetor – 2014-09-01 08:42:30

+0

新的@ user3342546最重要的是你必须把这个代码(控制器),您正在使用的验证codeigniter正确吗? – Ghost 2014-09-01 08:44:56

+0

雅先生我需要在哪里我把这个代码 – 2014-09-01 08:46:49