6

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;
  };
});

1 Answer 1

7

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;
  };

 });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.