Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 验证日期年月日_Php_Regex_Codeigniter_Validation_Date - Fatal编程技术网

Php 验证日期年月日

Php 验证日期年月日,php,regex,codeigniter,validation,date,Php,Regex,Codeigniter,Validation,Date,我有一个文本框的过去日期,如果日期大于当前日期且格式为dd/mm/yyyyy,我想验证表单时提交 在我的表单_validation.php中: 'add_prod_serv_fact' => array( 'quantidade_prod' => array('field' => 'quantidade_prod', 'label' => 'Quantidade', 'rules' => 'required

我有一个
文本框的过去日期
,如果日期大于当前日期且格式为
dd/mm/yyyyy
,我想验证表单时提交

在我的表单_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')
)
如何验证日期?

试试这个

you need to down date.js from this url "https://code.google.com/p/datejs/downloads/list"
调用
datefunction()
function表单
onChange()


函数datefunction()
{
var startdate=document.getElementById('date1')。值;
var enddate=document.getElementById('date2')。值;
//供当前日期使用
//var enddate=新日期();
var d1=Date.parse(startdate);
var d2=Date.parse(enddate);
如果(d1>d2)
{
警报(“开始日期不能大于结束日期!”);
返回false;
}
}

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

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)如何通过以下方式验证日期:

“验证系统支持回调您自己的验证函数。这允许您扩展验证类以满足您的需要。”

在您的方法中:

$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;
}
您自己的回调函数:

$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';
}
$date\u one=(int)strottime(str\u replace('/','-',$this->input->post('date\u one',true));
$date_two=(int)strtotime(str_replace('/','-',$this->input->post('date_two',true));
如果($date\u one<$date\u two)
{
回应‘讯息’;
}
其他的
{
回应‘讯息’;
}

您可以在CI中添加自定义验证函数:是的,但是什么使用regex?谢谢可能重复我不会使用正则表达式,我会使用更简单的解决方案:。你会从这里得到一些想法,我相信。。。DateTime函数很棒。:)