验证日期 dd/mm/yyyy

4
我有一个过去的日期作为 textbox 的值,在验证表单提交时,我想要判断该日期是否大于当前日期,并且格式为 dd/mm/yyyy
在我的 form_validation.php 文件中:
'add_prod_serv_fact' => array(
    'quantidade_prod'           => array('field' => 'quantidade_prod',          'label' => 'Quantidade',        'rules' => 'required|trim|numeric|greater_than[0]|htmlspecialchars'),
    'desconto_prod'             => array('field' => 'desconto_prod',            'label' => 'Desconto',          'rules' => 'required|trim|numeric|greater_than[-1]|less_than[101]|htmlspecialchars'),
    'date'                      => array('field' => 'date',                     'label' => 'Date',              'rules' => 'required|trim|htmlspecialchars')
)

如何验证日期?

1
你可以在CI中添加自定义验证函数:http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#callbacks - sinisake
是的,但正则表达式有什么用呢?谢谢! - pc_oc
可能是[Codeigniter - 日期格式 - 表单验证]的重复问题 (https://dev59.com/l2Yq5IYBdhLWcg3wmRoD)。 - Prix
2
我不会使用正则表达式,我会使用更简单的解决方案:https://dev59.com/cHVD5IYBdhLWcg3wBm1g。你可以从这里得到一些想法,我相信... DateTime 函数非常棒。 :) - sinisake
3个回答

6

你可以通过正则表达式进行验证,首先你需要编写像这样的函数:

function validate_date_reg($input)
{
   if (preg_match('\d{1,2}/\d{1,2}/\d{4}', $input))
   {
      return true; // it matched, return true
   }
   else
   {
      return false;
   }
}

并且像这样调用:

if($this->validate_date_reg($this->input->post('some_data')))
{
    // true go ahead......
}

我希望这能对你有所帮助。

有没有一种方法可以在单行中使用正则表达式参数进行表单验证? - heySushil

3

1) 如何使用CI callbacks验证日期:

"验证系统支持回调到您自己的验证函数。这使您可以扩展验证类以满足您的需求。"

在您的方法中:

$this->form_validation->set_rules('text_date', 'Date', 'trim|exact_length[10]|callback_validate_date');

您自己的回调函数:

public function validate_date($incoming_date)
{
    //If in dd/mm/yyyy format
    if (preg_match("^\d{2}/\d{2}/\d{4}^", $incoming_date))
    {
        //Extract date into array
        $date_array = explode('/', $incoming_date);

        //If it is not a date
        if(! checkdate($date_array[1], $date_array[0], $date_array[2]))
        {
            $this->form_validation->set_message('validate_date', 'Invalid date');
            return false;
        }
    }
    //If not in dd/mm/yyyy format
    else
    {
        $this->form_validation->set_message('validate_date', 'Invalid date');
        return false;
    }

    return true;
}

2) 如何比较两个日期:

$date_one = (int) strtotime(str_replace('/', '-', $this->input->post('date_one', true)));
$date_two = (int) strtotime(str_replace('/', '-', $this->input->post('date_two', true)));

if($date_one < $date_two)
{
    echo 'Message';
}
else
{
    echo 'Message';
}

2

试试这个...

you need to down date.js from this url "https://code.google.com/p/datejs/downloads/list"

onChange()事件中调用datefunction()函数。

<script>
    function datefunction()
    {
        var startdate = document.getElementById('date1').value;
        var enddate  = document.getElementById('date2').value;
        // for current date use
        // var enddate  = new Date();

        var d1 = Date.parse(startdate );
        var d2 = Date.parse(enddate  ) ;

        if (d1 > d2) 
        {
            alert ("Start Date cannot be gratter than End Date!");

            return false;
        }
    }
</script>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接