Created
April 24, 2014 18:22
-
-
Save bassettsj/11264407 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| .directive('allowedNumbers',function(){ | |
| return { | |
| restrict: 'A', | |
| require: 'ngModel', | |
| link: function(scope, el, attrs, ctrl){ | |
| function fromUser(text) { | |
| var old = ctrl.$modelValue || ''; | |
| //console.log(old); | |
| //console.log(text); | |
| if(RegExp(/^(\d{0,9}(\.\d{0,4})?)$/).test(text)) { | |
| ctrl.$setValidity('allowedNumbers', true); | |
| return text; | |
| }else{ | |
| ctrl.$setValidity('allowedNumbers', false); | |
| if(typeof text == "undefined"){ | |
| return ''; | |
| }else{ | |
| ctrl.$setViewValue(old); | |
| ctrl.$render(); | |
| return old; | |
| } | |
| } | |
| } | |
| ctrl.$parsers.push(fromUser); | |
| } | |
| }; | |
| }) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
<div allowedNumbers></div>