0

I know its too silly to ask this,But am confused y is this not working.Even though every values looks fine in debug mode.

Actually what I want to achieve is to display error if productionStartFrom is less than current date.

Controller

scope.currentDate={};
scope.checkDate=function(productionStartFrom){
          currentDate = $filter('date')(new Date(), "dd/MM/yyyy", "UTC");
          console.log(currentDate); //28/04/2017
          console.log(productionStartFrom); //05/04/2017
          if(currentDate > productionStartFrom){
              scope.dateErrMsg="Date cant be less than today";
              alert("Invalid Date");
              scope.myForm.$invalid="true";
          }
          else if(currentDate < productionStartFrom){
              scope.dateErrMsg="";
          }
    }

html page

<datepicker date-format="dd/MM/yyyy" selector="form-control">
<input name="productionStartFrom" type="text" ng-model="produce.productionStartFrom"                                                                                  pattern="(0[1-9]|1[0-9]|2[0-9]|3[01])/(0[1-9]|1[012])/[0-9]{4}"
ng-minlength="10" maxlength="10" x-ng-change="checkDate(produce.productionStartFrom)"                                /> </datepicker>
4
  • I guess, the way you are comparing dates causing issue Commented Apr 28, 2017 at 11:30
  • Whats wrong with comparison Commented Apr 28, 2017 at 11:31
  • make sure productionStartFrom is of date type Commented Apr 28, 2017 at 12:15
  • yes its date type only. Its coming from datePicker. so will that effect any Commented Apr 28, 2017 at 12:22

2 Answers 2

1
scope.currentDate={};
scope.checkDate=function(productionStartFrom){
      currentDate = $filter('date')(new Date(), "dd/MM/yyyy", "UTC");
      console.log(currentDate); 
      if(currentDate >= productionStartFrom){
          scope.dateErrMsg="Date cant be less than today";
          alert("Invalid Date");
          scope.myForm.$invalid="true";
      }
      else if(currentDate <= productionStartFrom){
          scope.dateErrMsg="";
      }
}
Sign up to request clarification or add additional context in comments.

Comments

0

you can try like the below code,

$scope.checkDate=function(productionStartFrom){
        currentDate = $filter('date')(new Date(), "dd/MM/yyyy", "UTC");
        productionStartFrom = $filter('date')(new Date(productionStartFrom), "dd/MM/yyyy", "UTC");
        if(currentDate > productionStartFrom){
            $scope.dateErrMsg="Date cant be less than today";
            console.log("Invalid Date");
        }
        else if(currentDate < productionStartFrom){
            $scope.dateErrMsg="Valid Date";
        }
  }

1 Comment

Please check my plunkr link which is working (check the console log). plnkr.co/edit/W9VspRk3fREKJvxjuRcM?p=preview

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.