Created
May 22, 2013 20:25
-
-
Save ferronrsmith/5630621 to your computer and use it in GitHub Desktop.
disallow numeric entry in a input field
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
app.directive('ng-numeric', function($log) { | |
return { | |
require: 'ngModel', | |
link: function (scope, element, attr, ngModelCtrl) { | |
function fromUser(text) { | |
var transformedInput = text.replace(/[^0-9]/g, ''); | |
$log.info(transformedInput); | |
if(transformedInput !== text) { | |
ngModelCtrl.$setViewValue(transformedInput); | |
ngModelCtrl.$render(); | |
} | |
return transformedInput; // or return Number(transformedInput) | |
} | |
ngModelCtrl.$parsers.push(fromUser); | |
} | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment