使用Zebra Datepicker验证我的用户表单中的无效日期格式

时间:2015-11-05 13:44:27

标签: php jquery validation datepicker

我在用户表单页面添加了http://stefangabos.ro/jquery/zebra-datepicker/#download zebra datepicker代码!

看起来很好:

表单上的错误图像 enter image description here

我在这里给你代码,可以查一下,我的代码有什么不对!

休息所有其他属性都工作正常,只停留在日期格式:( 整个项目文件也在这里上传!

下载 dropbox.com/s/wa5hnugwcn5s6ox/project.zip?dl=0 我也放了一个代码,哪个部分我遇到了问题! 页面重命名:user.php

private function set_date_of_birth($date_of_birth) {
        $reg = "/^\d{2}\-\d{2}\-\d{4}$/";

        if(!preg_match($reg, $date_of_birth)){
            throw new Exception("Invalid Date Format");
        }

        $parts = explode("-", $date_of_birth);

        list($day, $month, $year) = $parts;

        if(!isset($day) || !isset($month) || !isset($year)){
            throw new Exception("Invalid/Date PArameters");
        }

        if(!checkdate($month, $day, $year)){
            throw new Exception("Invalid/Missing Date of Birth");
        }

        //$this->date_of_birth = mktime(0, 0, 0, $month, $day, $year);
        $this->date_of_birth = strtotime($date_of_birth);

    }

    private function get_day() {
        if(is_null($this->date_of_birth)){
            return 0;
        }
        $date = date("d", $this->date_of_birth);
        return $date;

    }

    private function get_month() {
        if(is_null($this->date_of_birth)){
            return 0;
        }
        $date = date("m", $this->date_of_birth);
        return $date;

    }
    private function get_year() {
        if(is_null($this->date_of_birth)){
            return 0;
        }
        $date = date("Y", $this->date_of_birth);
        return $date;

    }

    private function get_date_of_birth() {

        if(is_null($this->date_of_birth)){
            return "";
        }
        $date = date("d-m-Y", $this->date_of_birth);
        return $date;

    }

2)Singup.php

<div class="row">
        <div class="cell cell-left">Date Of Birth</div>
        <div class="cell cell-right">
            <input type="text" id="date_of_birth" name="date_of_birth" class="datepicker" placeholder="dd-mm-YYYY" value="<?php echo($obj_user->date_of_birth); ?>">
            <span id="dob_error">
                <?php
                if(isset($errors['date_of_birth'])){
                    echo($errors['date_of_birth']);
                }

                ?></span>
        </div>
        <div class="clear-box"></div>
    </div>

3)Top.php

<script src="scripts/jquery-2.1.4.min.js" type="text/javascript"></script>

<link href="styles/default.css" media="all" type="text/css" rel="stylesheet"/>


<script src="scripts/zebra_datepicker.js"></script>
<script>
    $(document).ready(function(){
        $(".datepicker").Zebra_DatePicker({
            direction:1,
                format: 'd-m-Y',
                show_week_number: 'Wk'

        });
    });
</script>

4)process.php

try {
    $obj_user->date_of_birth = $_POST['date_of_birth'] ;

} catch (Exception $ex) {
    $errors['date_of_birth'] = $ex->getMessage();

}

2 个答案:

答案 0 :(得分:0)

作为测试我做了以下事情:

class zebra{

    public static function set_date_of_birth( $date_of_birth ) {
        try{
            $reg = "/^\d{2}\-\d{2}\-\d{4}$/";

            if(!preg_match($reg, $date_of_birth)){
                throw new Exception("Invalid Date Format");
            }
            $parts = explode("-", $date_of_birth);

            list($day, $month, $year) = $parts;
            if(!isset($day) || !isset($month) || !isset($year)){
                throw new Exception("Invalid/Date PArameters");
            }
            if(!checkdate($month, $day, $year)){
                throw new Exception("Invalid/Missing Date of Birth");
            }

            $date_of_birth = strtotime( $date_of_birth );

            return $date_of_birth;
        }catch( Exception $e ){
            return $e->getMessage();
        }
    }

}
echo zebra::set_date_of_birth('19-11-2015');
echo '<br />';
echo zebra::set_date_of_birth('19-11-15');
/* 
    and the output was:-
    1447891200
    Invalid Date Format 
*/

因此该函数可以正常工作,但我没有看到代码中的哪个部分称为函数set_date_of_birth

答案 1 :(得分:0)

谢谢你们所有人!

额外</div>在提交表单时创建了一个问题,现在已经修复了