lpoulter
lpoulter

Reputation: 158

Adding ng-minlength to input field

I'm attempting to add ng-minlength and ng-maxlength to a form input field. There is something strange happening. When the a number is entered is less than the min-length it disappears and is never added to the model.

However if you past in a number over 13 digits it renders but is not added to the model.

    app.directive('cmNumber', ['$compile', function ($compile) {
    return {
        restrict: 'A',
        compile: function (element) {
            var onlyNumbers = /^\d+$/;
            element.attr('ng-minlength', '13');
            element.attr('ng-maxlength', '19');
            element.attr('ng-pattern', onlyNumbers);

            element.attr('placeholder', '123456789112345');
            element.removeAttr('cm-Number'); //remove the attribute to avoid infinite loop

            return function (scope, element) {
                    $compile(element[0].form)(scope);
                };
        }
    };
}]);

plunker here http://plnkr.co/edit/G4DaGSt190NTdaognJrJ?p=preview

Upvotes: 1

Views: 109

Answers (1)

Pankaj Parkar
Pankaj Parkar

Reputation: 136144

Only you need to do change is cm-number instead of cm-Number all should be in small case.

Otherwise your code is totally correct. The reason behind above directive is not working is You are referring to angular beta version 1.3.0-beta.5 which has possibly has broken the thing which you are trying.

You should upgrade it to angular 1.3.15 that is stable version also you could go for angular latest angular 1.4+ versions

Working Plunkr

Upvotes: 1

Related Questions