如何在Codeigniter中验证时间输入

时间:2011-09-16 06:32:28

标签: codeigniter codeigniter-2

我在表单中有一个文本框来输入时间,我用jquery检查验证。但现在我想使用codeigniter的内置验证系统检查文本框的验证。请您告诉我如何使用codeigniter的内置验证系统验证时间输入?

以下是我使用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>

这是html

<input class="time" type="text" name="time1" size="15">

3 个答案:

答案 0 :(得分:5)

这样的事可能

// Validation rule in controller
$this->form_validation->set_rules('time', 'time', 'trim|min_length[3]|max_length[5]|callback_validate_time');

和回调:

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

答案 1 :(得分:0)

修改@danneth发布的内容,以便接受时间为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;
    }   
}

*更改了已弃用的拆分

*如果收到的参数是像&#39; aaaaa&#39;

这样的字符串,则增加一个条件

*将表单验证min_length从3更改为8(这样您就可以确保输入HH:MM:SS中的8个字符):

$this->form_validation->set_rules('time','Time','required|trim|min_length[8]|max_length[8]|callback_validate_time');

答案 2 :(得分:0)

有即兴的@danneth代码希望它有所帮助

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