Skip to content

Instantly share code, notes, and snippets.

@harmoniemand
Created December 9, 2016 11:00
Show Gist options
  • Save harmoniemand/d6fd7aec43224ae4010f8791bf391aa0 to your computer and use it in GitHub Desktop.
Save harmoniemand/d6fd7aec43224ae4010f8791bf391aa0 to your computer and use it in GitHub Desktop.
angular.module('myApp')
.directive('dateValidation', [function () {
'use strict';
return {
restrict: 'EA',
replace: true,
require: 'ngModel',
link: function (scope, element, attrs, ngModel) {
function parseDate(value) {
// do your custom parsing here
var date = moment(value, "DD.MM.YYYY");
return date;
}
ngModel.$parsers.unshift(function (value) {
var date = parseDate(value);
ngModel.$setValidity("validdate", date.isValid());
return date.isValid() ? value : undefined;
});
ngModel.$formatters.unshift(function (value) {
var date = parseDate(value);
ngModel.$setValidity('validdate', date.isValid());
return value;
});
}
};
}]);
<md-input-container class="md-icon-float md-icon-right md-block">
<label>My Date</label>
<md-datepicker date-validation ng-model="mydate" name="dateField"></md-datepicker>
<div ng-messages="newZaehlerstand.dateField.$error">
<div ng-message="validdate">Date validation error message!</div>
</div>
</md-input-container>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment