Angularjs Directive For Preventing Max And Min Values

I want to implement an AngularJS directive that prevents user from entering some values to an input. This directive has three attributes max, min and decimals. Max attribute defines the maximum value that could be entered to the input, min attribute defines the minimum value that could be entered to the input if it is negative and decimals is the number of decimal point that could be entered in the input. Min attribute has a special functionality, it prevents user only if it has negative values, since preventing positive min values are meaningless.

I could use the jquery or javascript functions that use the keyup, keydown events, or provide a watch on the value of input and decides whether to render a value. However, I decided to use the ngModel and ngModelController life-cycle to implement this functionality.

You could add your custom parsers to ngModelController, so you decide whether render a value or not. I have just add a custom function for that:

In this custom parse function, I have decided whether to display that value or not.

You can find the full source code and demo at my github page:

Leave a Reply

Your email address will not be published. Required fields are marked *