Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJs BootstrapUI Datepicker validation

I need to validate the date of the datepicker and also limit the years (avoid 15-05-9999 for example).

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>

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

I made this directive to validate 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 that is not the idea.

I managed to make it work so if it is a date it shows a message saying "Date is valid" and if not "date is invalid".

But I'm stuck there.

Any ideas on how can I put year limit and character limits and make it work?

I tried already with max-date, max, and others.

If you need more info just ask, I don't know if it is clear enough or well explained. Thank you for the help! ;)

like image 726
SrAxi Avatar asked Dec 14 '22 21:12

SrAxi


2 Answers

Why dont you use ng-pattern?? instead of making a new directive.

Read more about it here NG-PATTERN DOCS

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

Take a look at this fiddle with a better solution

validate datepicker using ng-pattern

like image 124
ppollono Avatar answered Dec 26 '22 17:12

ppollono


used this: maxlength="10" on the html to limit characters.

and this is to validate the date format is correct (avoid 1/1/1, or 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);
         }
     };
});

Still didn't manage to fix a min date and max date, but is not urgent. Hope it can help people in future issues.

like image 44
SrAxi Avatar answered Dec 26 '22 17:12

SrAxi