Created
December 9, 2016 11:00
-
-
Save harmoniemand/d6fd7aec43224ae4010f8791bf391aa0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}); | |
} | |
}; | |
}]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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