-->

Datapicker allow invalid dates

2019-07-10 01:28发布

问题:

I need allow date like '99/99/9999' when user doesn't know exact date, how can I implement this?

$('#date').datepicker({
    language: "pt-BR",
    autoclose: true,
    format: 'dd/mm/yyyy',
    todayHighlight: true
})

Using this code, '99/99/9999' is converted to '07/06/9999'.

Here an example: https://jsfiddle.net/bvbtz2re/2/

回答1:

Working fiddle.

You could set the option constrainInput to false :

$('#date').datepicker({
    language: "pt-BR",
    autoclose: true,
    format: 'dd/mm/yyyy',
    todayHighlight: true,
    constrainInput: false 
})

Hope this helps.



回答2:

You can use forceParse option setting its value to false. As the documentation says:

Whether or not to force parsing of the input value when the picker is closed. That is, when an invalid date is left in the input field by the user, the picker will forcibly parse that value, and set the input’s value to the new, valid date, conforming to the given format.

Here a live example:

$('input.datum').datepicker({
  format: 'dd.mm.yyyy',
  language: 'de',
  weekStart: 1,
  autoclose: true,
  assumeNearbyYear: true,
  forceParse: false
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.4/css/bootstrap-datepicker3.css" rel="stylesheet"/>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.4/js/bootstrap-datepicker.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.4/locales/bootstrap-datepicker.de.min.js"></script>

<form action="#" method="post">
    <label>Date:</label>
    <input class="datum" type="text" name="vondatum" required>
    <input type="submit" value="save">
</form>