2

I have an input in a form that looks like this:

<input type="number" name="inputStageNumTeams" id="inputStageNumTeams"
 ng-model="s.numTeams" validate-greaterthan="2" required>

However, for some reason my custom directive validateGreaterthan isn't running correctly. If I change the input type to "text" it works like a charm! I'd like to keep the input type to number if possible.

Here is the directive in question:

app.directive('validateGreaterthan', function() {
return {
    require: 'ngModel',
    link: function(scope, elm, attrs, ctrl) {
        ctrl.$parsers.unshift(function(viewValue) {
            var number = attrs.validateGreaterthan;
            if (parseInt(viewValue) !== NaN) {
                scope.numberValid = ((viewValue && (parseInt(viewValue) >= number)) ? 'valid' : undefined);
            }

            if(scope.numberValid) {
                ctrl.$setValidity('number', true);
                return viewValue;
            } else {
                ctrl.$setValidity('number', false);
                return undefined;
            }

        });
    }
};
});
1
  • To be clear, when input type is set to number, the directive won't ever run, but when set to text it will run on every change of the input (what I want). Commented Feb 4, 2013 at 4:08

2 Answers 2

3

Why do you not try to use the already existing [min="{string}"] directive?

http://docs.angularjs.org/api/ng.directive:input.number

Sign up to request clarification or add additional context in comments.

Comments

0

I am pretty sure that the attribute value (attrs.validateGreaterthan) is a string. Convert that string to an integer, to make sure you are comparing the same type.

1 Comment

I don't think that's the problem. I tried logging the attribute and the directive is never actually getting called when the input is set to number.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.