javascript - Angularjs start date and end date validations -


i new angularjs , trying validate 2 scenarios. have 2 text boxes 1 start date , other end date. checking

  1. show validation error on ui if start date not greater or equal today. should today or day after today.
  2. show validation error on ui if start date greater end date. end date should greater start date.

i tried below code did not work. suggestions please.

html code

<label for="enddate" class="control-label">start date:</label> <div>     <input type="text" class="form-control"             id="startdate" ng-model="startdate" /> </div>  <label for="text" class="control-label">end date:</label> <div>     <input type="text" class="form-control"             id="enddate" ng-model="enddate"              ng-change='checkerr(startdate,enddate)' />  </div>  <span>{{errmessage}}</span> 

js code

$scope.checkerr = function(startdate,enddate){     $scope.errmessage = '';     $scope.curdate = new date();      if(startdate < enddate){       $scope.errmessage = 'end date should greate start date';       return false;     }     if(startdate < curdate){        $scope.errmessage = 'start date should not before today.';        return false;     }    }; 
  • i have input type text both date controls.i using bootstrap date picker.

you have logic reversed on first bit , have construct new date startdate compare today's date. set curdate scope, $scope.curdate = new date() referencing curdate without $scope undefined. lastly, need cast statedate , enddate date well. otherwise you're comparing strings.

$scope.checkerr = function(startdate,enddate) {     $scope.errmessage = '';     var curdate = new date();      if(new date(startdate) > new date(enddate)){       $scope.errmessage = 'end date should greater start date';       return false;     }     if(new date(startdate) < curdate){        $scope.errmessage = 'start date should not before today.';        return false;     } }; 

working example: http://jsfiddle.net/pecelm14/


Comments