0
votes

is there any way to show validation error message or border become red when user enter invalid date format?.In my demo app there two input field in which I am showing date like this "16-jun-1989" .Now user edit this field only in this format (that is valide) if user enter invalid format I want to show error message on button click or border become red on button click here is my code http://plnkr.co/edit/xoTH3uJZSVx0W78rwhMt?p=preview

var app = angular.module('plunker', ['angularMoment']);

app.controller('MainCtrl', function($scope ,moment) {
  $scope.name = 'World';
  console.log(moment)
  var d = new Date(613938600000);
  $scope.c = {
   ab: {
      name:'abc'
    },
   date: {
      name: moment(d).format('DD-MMM-YYYY')
    }
    };

    $scope.onclick =function(){
        console.log($scope.c)
    }

});
2

2 Answers

0
votes

Since you are using moment already, check if the date is valid with:

moment($scope.c.date.name).isValid()

A working example here.

0
votes

Though this already has answer. You might also be interested in ensuring the date is specified exactly how you want it. The validation in the accepted answer is not strict: This should fix that:

$scope.onclick = function() {
    if (!moment($scope.c.date.name, 'DD-MMM-YYYY').isValid()) {
      alert('Date must be in the format 02-Nov-2018');
    } else {
      alert('All good');
    }
}

The second argument to the moment function ensures this strictness.