How to restrict user to input data using datetimep

2019-07-15 08:09发布

I want to restrict the user to only input data using the datetimepicker. Below is the code I am using:

<div class="form-group">
    <label>Start</label>
    <div class="input-group date" id="dtp1">
        <input type="text" id="txtStart" class="form-control" readonly="readonly"/>
        <span class="input-group-addon">
            <span class="glyphicon glyphicon-calendar"></span>
        </span>
    </div>
</div>
$('#dtp1').datetimepicker({
    format: 'DD/MM/YYYY HH:mm A',
    daysOfWeekDisabled: [0, 6]
});

I have tried using the readonly attribute, but this disables both the input and datetimepicker. Is there any way through which I can disable the input field but still allow the user to click on the calendar icon to pick a date?

calendar screen shot

3条回答
时光不老,我们不散
2楼-- · 2019-07-15 08:36

Both readonly="readonly" & disabled="disabled" didn't worked for me. Below line worked;

<input type="text" onkeydown="return false"/>
查看更多
Rolldiameter
3楼-- · 2019-07-15 08:37

You can use the ignoreReadonly option that:

Allow date picker show event to fire even when the associated input element has the readonly="readonly" property.

Here a working sample:

$('#dtp1').datetimepicker({
    format: 'DD/MM/YYYY HH:mm A',
    daysOfWeekDisabled: [0, 6],
    ignoreReadonly: true
});
<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-datetimepicker/4.17.47/css/bootstrap-datetimepicker.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/moment.js/2.18.1/moment.min.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-datetimepicker/4.17.47/js/bootstrap-datetimepicker.min.js"></script>

<div class="form-group">
    <label>Start</label>
    <div class="input-group date" id="dtp1">
        <input type="text" id="txtStart" class="form-control" readonly="readonly"/>
        <span class="input-group-addon">
            <span class="glyphicon glyphicon-calendar"></span>
        </span>
    </div>
</div>

查看更多
做个烂人
4楼-- · 2019-07-15 08:44

The only way I could think of right now is to use the event.preventDefault() on the keydown event on the input:

<input type="text" id="txtStart" class="form-control" onkeydown="event.preventDefault()">
查看更多
登录 后发表回答