AngularJs BootstrapUI Check Print Date

I need to check the datepicker date and also limit the years (e.g. 15-05-9999).

This is my HTML code:

 <p class="input-group">
                        <input type="text" name="raisedOnDate" class="form-control" ng-model="date" ng-disabled="!viewSearchEvent.raisedOnActive"
                               datepicker-popup="{{format}}" is-open="opened1" datepicker-options="dateOptions" date-validator required />
                        <span class="input-group-btn">
                            <button type="button" class="btn btn-default" ng-click="open($event,'on')"><i class="glyphicon glyphicon-calendar"></i></button>
                        </span>
                    <p ng-show="!searchEventsForm.$valid" style="color: red;">Date is invalid!</p>
                    <p ng-show="searchEventsForm.$valid" style="color: green;">Date is valid!</p>

It is inside <form name="searchEventsForm" role="form" class="form-horizontal" novalidate>.

I made this directive to check the date type:

app.directive('dateValidator', function() {
return {
    require: 'ngModel',
    link: function (scope, elem, attr, ngModel) {
        function validate(value) {

            var d = Date.parse(value);

            // it is a date
            if (isNaN(d)) { // d.valueOf() could also work
                ngModel.$setValidity('valid', false);
            } else {
                ngModel.$setValidity('valid', true);
            }
        }

        scope.$watch(function () {
            return ngModel.$viewValue;
        }, validate);
    }
};

});

But I can enter dates like 25-05-999999999, and this is not an idea.

I managed to get it to work, so if it is a date, it displays the message "Date is valid", and if "date is not valid".

But I'm stuck there.

Any ideas on how I can set limits on the year limit and limit and make it work?

I tried already with max-date, maxand others.

If you need more information, just ask, I don’t know if this is enough or well explained. Thank you for your help !;)

+4
3

: maxlength="10" html .

( 1/1/1 11/06/201):

MyDirective.directive('dateValidator', function() {
     return {
         require: 'ngModel',
         link: function (scope, elem, attr, ngModel) {
             function validate(value) {
                 if (value !== undefined && value != null) {
                     ngModel.$setValidity('badDate', true);
                     if (value instanceof Date) {
                         var d = Date.parse(value);
                         // it is a date
                         if (isNaN(d)) {
                             ngModel.$setValidity('badDate', false);
                         }
                     } else {
                         var myPattern = new RegExp(/^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/);
                         if (value !='' && !myPattern.test(value)) {
                             ngModel.$setValidity('badDate', false);
                         }
                     }
                 }
             }

             scope.$watch(function () {
                 return ngModel.$viewValue;
             }, validate);
         }
     };
});

- , . , .

+6

ng-pattern? .

NG-PATTERN DOCS

 ng-pattern="/^(\d{4})-(\d{2})-(\d{2})$/"

ng-pattern

+7

"scope. $watch", , ngModel '$ parsers' '$ formatters'. : https://docs.angularjs.org/api/ng/type/ngModel.NgModelController

-1
source

Source: https://habr.com/ru/post/1546001/


All Articles