繁体   English   中英

验证日期dd / mm / yyyy

[英]Validate date dd/mm/yyyy

我有一个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')
)

如何确认日期?

您可以先通过正则表达式进行验证,然后编写这样的函数

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

希望对您有帮助。

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

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

在您的方法中:

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

试试这个..

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

调用datefunction()函数形式onChange()

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM