Validation of Invalid Date Format in my user form using Zebra Datepicker

i have added http://stefangabos.ro/jquery/zebra-datepicker/#download zebra datepicker code in my user form page!

Download project all files : https://www.dropbox.com/s/wa5hnugwcn5s6ox/project.zip?dl=0

it is appearing fine Error image on Form

i am giving you code here, can one check, whats wrong on my code!

rest all my other properties are working fine, only stuck on date format :frowning:

page name: 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;

    }
  1. Singup.php

    Date Of Birth
    <?php if(isset($errors['date_of_birth'])){ echo($errors['date_of_birth']); }
                 ?></span>
         </div>
         <div class="clear-box"></div>
     </div>
    
  2. Top.php

  3. process.php

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

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

    }

Does reformatting date parts to Y-m-d before strtotime fix the issue?

    //Date of Birth
        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");
        }
        $properdate = $year.'-'.$month.'-'.$day;
        //$this->date_of_birth = mktime(0, 0, 0, $month, $day, $year);
        //$this->date_of_birth = strtotime($date_of_birth);
        $this->date_of_birth = strtotime($properdate);
        
    }

i have replaced with this code

//Date of Birth

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");
    }
    $properdate = $year.'-'.$month.'-'.$day;
    //$this->date_of_birth = mktime(0, 0, 0, $month, $day, $year);
    //$this->date_of_birth = strtotime($date_of_birth);
    $this->date_of_birth = strtotime($properdate);
    
}

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

Still getting same error :frowning:

Do you get “date_of_birth” to show in POST when you print POST?

echo "<pre>";
print_r($_POST); 
echo "</pre>";

I have tested your form and without class="datepicker", “date_of_birth” is available in POST. When adding the class back to the line, the JS disables the input.

Maybe someone who has used Zebra datepicker might know what is going on. I don’t know why it would disable the input.

js disables the input you are right ! i have checked the path, it is correct, dont know why it is creating a problem! any javascript expert can help!

Only a wild guess and I really don’t know, but my guess is it’s a naming conflict.

That is, “datepicker” is in some way like a “reserved word”.

Try giving it a unique class name like “aliqayyum-datepicker” or “my-datepicker” etc.

You might try a different datepicker as well.
http://jqueryui.com/datepicker/

When I tested your code, the DOB field was not being included in the submitted form data. The problem seems to be that you have some extra closing </div> tags in your page which are causing problems with the form. When I removed them, I was able to submit the form and the DOB field was included in the data.

You should also make sure that you set PHP to display all errors during development, as you’ll see there are some other problems with your code in process_signup.php

ok let me check

thanks fixed :slight_smile: extra

caused a problem :slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.