How do you prevent bootstrap-datepicker from clearing existing value from input if no date is selected?

nikmav picture nikmav · Apr 26, 2014 · Viewed 36.4k times · Source

I have have a div with a user's date of birth pre-populated with php:

<div id="datepicker" class="input-group date">
    <label for="dob">Date of Birth</label> <input type="text" class="form-control" id="dob" name="dob" value="<?php $date = new DateTime($this->swimmers->dob); echo $date->format('d/m/Y');?>"><span class="input-group-addon"><i class="glyphicon glyphicon-th"></i></span>
</div>

The javascript:

<script>     
    $('.container .input-group.date').datepicker({
    startDate: "01/01/1900",
    endDate: "01/01/2100",
    format: "dd/mm/yyyy",
    autoclose: true,
    todayHighlight: true
    });
</script>

Bootstrap is setup and working correctly however, when the "dob" input gets and loses focus without making a date selection it clears the value that was pre-populated.

This however doesn't happen if a date has been selected via the datepicker and then the input gets and loses focus.

How can the pre-populated value be kept in the field if someone clicks in it?

Edit: The first time you make a date selection, it clears the pre-populated value but doesn't insert the selected date until you make a date selection a second time.

Update: Working code updated!

Answer

Datta picture Datta · Feb 27, 2015

Set Force parse value to false: forceParse: false

    $(".applyDatepicker").datepicker({
            forceParse: false
    });