Last active
September 27, 2016 08:15
-
-
Save bradberger/16b7f9e7eb91fc2ea81f to your computer and use it in GitHub Desktop.
A simple AngularJS directive to make sure a number is not zero. It sets the formName.elementName.$error.zero value accordingly.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @description A simple check to ensure a number is not zero. Sets the "zero" validation accordingly. | |
* @usage <input ng-model="myModel" not-zero> | |
*/ | |
angular.module("app").directive("notZero", function() { | |
var linkFunc = function(scope, element, attrs, ngModel) { | |
var zeroValidator = function(value) { | |
var num = parseFloat(value); | |
ngModel.$setValidity("zero", ! isNaN(num) && num !== 0); | |
return value; | |
}; | |
ngModel.$parsers.push(zeroValidator); | |
ngModel.$formatters.push(zeroValidator); | |
}; | |
return { | |
require: "ngModel", | |
link: linkFunc | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment