Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular ui-bootstrap datepicker mindate not validated when typed in

I have a form with a ui-bootstrap datepicker. I want to prevent the date from being in the past.

I set the min-date setting on the directive to a new Date() as shown below. This correctly prevents selection of dates in the past when using the popup, however I can still type in a date in the past and this validates OK.

How can I enforce min-date validation on dates that are typed in?

Plunkr: https://plnkr.co/edit/EHO1BG8BspNMvsoKT30l?p=preview

Html:

<div class="input-group">
    <input type="text" 
           class="form-control" 
           uib-datepicker-popup="{{format}}" 
           ng-model="dt" 
           is-open="popup1.opened" 
           min-date="minDate" 
           datepicker-options="dateOptions" 
           ng-required="true" 
           close-text="Close"
           alt-input-formats="altInputFormats"
           name="dt"/>
    <span class="input-group-btn">
        <button type="button" class="btn btn-default" ng-click="open1()">
            <i class="glyphicon glyphicon-calendar"></i>
        </button>
    </span>
</div>

JS:

app.controller('MainCtrl', function($scope) {
  $scope.dt = new Date();
  $scope.minDate = new Date();
  $scope.format = "dd/MM/yyyy";
  $scope.altInputFormats = ['d!/M!/yyyy'];
  $scope.dateOptions = {
    formatYear: 'yy',
    startingDay: 1
  };

  $scope.popup1 = {
    opened: false
  };

  $scope.open1 = function() {
    $scope.popup1.opened = true;
  };
});
like image 657
ChaoticNadirs Avatar asked Jan 12 '16 13:01

ChaoticNadirs


1 Answers

This should work correctly, I have added changeHandler function in your controller and called it on ng-change of input.

Html:

<div class="input-group">
      <input type="text" 
             class="form-control" 
             uib-datepicker-popup="{{format}}" 
             ng-model="dt" 
             is-open="popup1.opened" 
             min-date="minDate" 
             datepicker-options="dateOptions" 
             ng-required="true" 
             close-text="Close"
             alt-input-formats="altInputFormats"
             ng-change="changeHandler()"
             name="dt"/>
      <span class="input-group-btn">
        <button type="button" class="btn btn-default" ng-click="open1()">
          <i class="glyphicon glyphicon-calendar"></i>
        </button>
      </span>
</div>

JS:

app.controller('MainCtrl', function($scope) {
  $scope.dt = new Date();
  $scope.minDate = new Date();
  $scope.format = "dd/MM/yyyy";
  $scope.altInputFormats = ['d!/M!/yyyy'];
  $scope.dateOptions = {
     formatYear: 'yy',
     startingDay: 1
  };

  $scope.popup1 = {
     opened: false
  };

  $scope.changeHandler = function () {
    $scope.dateForm.dt.$setValidity('$valid', $scope.dt.getTime() >= $scope.minDate.getTime());
  }

  $scope.open1 = function() {
     $scope.popup1.opened = true;
  };

 });
like image 118
Ruben Karapetyan Avatar answered Sep 18 '22 11:09

Ruben Karapetyan