I have a text box in a form to enter time and I use to check the validation using jquery. But now I want to check the validation of the text box using the codeigniter's built-in validation system. Would you please kindly tell me how to validate time input using codeigniter's built-in validation system?
Here's the code how I use to do it using jquery:
<script type="text/javascript">
$().ready(function() {
$.validator.addMethod("time", function(value, element) {
return this.optional(element) || /^(([0-1]?[0-2])|([2][0-3])):([0-5]?[0-9])\s(a|p)m?$/i.test(value);
}, "Enter Valid Time");
$("#theForm").validate({
rules: {
time: "required time",
},
});
开发者_如何学运维 });
</script>
And here is the html
<input class="time" type="text" name="time1" size="15">
Something like this perhaps
// Validation rule in controller
$this->form_validation->set_rules('time', 'time', 'trim|min_length[3]|max_length[5]|callback_validate_time');
and a callback:
public function validate_time($str)
{
//Assume $str SHOULD be entered as HH:MM
list($hh, $mm) = split('[:]', $str);
if (!is_numeric($hh) || !is_numeric($mm))
{
$this->form_validation->set_message('validate_time', 'Not numeric');
return FALSE;
}
else if ((int) $hh > 24 || (int) $mm > 59)
{
$this->form_validation->set_message('validate_time', 'Invalid time');
return FALSE;
}
else if (mktime((int) $hh, (int) $mm) === FALSE)
{
$this->form_validation->set_message('validate_time', 'Invalid time');
return FALSE;
}
return TRUE;
}
Modifying what @danneth posted, in order to accept time as HH:MM:SS
public function validate_time($str){
if (strrchr($str,":")) {
list($hh, $mm, $ss) = explode(':', $str);
if (!is_numeric($hh) || !is_numeric($mm) || !is_numeric($ss)){
return FALSE;
}elseif ((int) $hh > 24 || (int) $mm > 59 || (int) $ss > 59){
return FALSE;
}elseif (mktime((int) $hh, (int) $mm, (int) $ss) === FALSE){
return FALSE;
}
return TRUE;
}else{
return FALSE;
}
}
*Changed deprecated split for explode
*Added one more condition in case the parameter received is a string like 'aaaaa'
*Changed form validation min_length from 3 to 8 (so you make sure the 8 characters from HH:MM:SS are entered):
$this->form_validation->set_rules('time','Time','required|trim|min_length[8]|max_length[8]|callback_validate_time');
have improvised @danneth 's code hope it helps
function _validate_date($str_date)
{
if($str_date!='')
{
/*
Remove Whitespaces if any
*/
$str_date = trim($str_date);
list($hh,$sub) = split('[:]', $str_date);
/*
Separate Minute from Meridian
e.g 50 PM ===> $mm=60 and $md=PM
*/
$mm = substr($sub, 0,2);
$md = trim(substr($sub, 2,strlen($sub)));
/*
Make Meridian uppercase
Implicitly Check if Meridian is PM or AM
if not then make it PM
*/
$md = strtoupper($md);
if(!in_array($md, array('PM',"AM")))
{
return FALSE;
}
/*
Check if MM and HH are numeric
*/
if(!is_numeric($hh) || !is_numeric($mm))
{
$this->form_validation->set_message('Invalid Time','Illegal chars found');
return 11;
}
$hh = (int)$hh;
$mm = (int)$mm;
/*
Check HH validity should not be less than 0 and more 24
*/
if($hh<0 || $hh>24)
{
return FALSE;
}
/*
Check MM validity should not be less than 0 and more than 59
*/
if($mm<0 | $mm>59)
{
return FALSE;
}
/*
Parse HH and MM to int for further operation and check it generates validate time
*/
if(mktime($hh,$mm)==FALSE)
{
$this->form_validation->set_message('Invalid Time','Check Time');
return FALSE;
}
return TRUE;
}
else
{
return FALSE;
}
}
精彩评论