【问题标题】:How to accept only date as input field value for bootstrap datepicker in angular如何仅接受日期作为角度引导日期选择器的输入字段值
【发布时间】:2016-03-02 17:47:54
【问题描述】:

html

                 <form name="eventInformation" id="eventInformation"><label class="required"> Occurence Date </label>
                     <p class="input-group">
                       <input type="text" class="form-control" uib-datepicker-popup="{{format}}" ng-model="formData_EventDetails.eventOccurDate"
                              is-open="popup[0].opened" datepicker-options="dateOptions" close-text="Close" required/>
                    <span class="input-group-btn">
                    <button type="button" class="btn btn-default" ng-click="open(0)"><i class="glyphicon glyphicon-calendar"></i></button>
                    </span>
                    </p>

                <div class="col-sm-2 pull-right">
                          <button class="btn btn-block btn-primary ebtn"
                                  ng-disabled="eventInformation.$invalid"
                                  ng-click="submit()">Save</button>
                      </div>
</form>

javascript

                        $scope.format = 'yyyy/MM/dd';


                        $scope.dateOptions = {
                            formatYear : 'yy',
                            startingDay : 1
                        };

                        $scope.popup = [];
                        for (i = 0; i < 10; i++) {
                            $scope.popup[i] = {
                                opened : false
                            };
                        }

                        $scope.open = function(i) {
                            $scope.popup[i].opened = true;
                        };

我有引导日期选择器元素,我试图只接受日期作为输入,但它接受各种输入,即字符串、数字等。我如何限制它只接受日期作为输入

【问题讨论】:

标签: html angularjs twitter-bootstrap validation bootstrap-datepicker


【解决方案1】:

由于是输入文本框,不能限制用户输入无效字符(默认行为)。

但是你可以停止模型更新操作,当用户提供错误的日期(为此你需要在你的输入元素上设置以下属性)

ng-model-options="{allowInvalid: false}"

【讨论】:

  • 要注意的另一件奇怪的事情是,当我输入一些字符串值时,提交按钮被禁用,这意味着 ng-disabled="eventInformation.$invalid" 出于某种原因是正确的并且被识别为日期字段
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-13
  • 1970-01-01
  • 1970-01-01
  • 2022-01-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多