Skip to content

Instantly share code, notes, and snippets.

@bassettsj
Created April 24, 2014 18:22
Show Gist options
  • Select an option

  • Save bassettsj/11264407 to your computer and use it in GitHub Desktop.

Select an option

Save bassettsj/11264407 to your computer and use it in GitHub Desktop.
.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);
}
};
})
@bassettsj

Copy link
Copy Markdown
Author

<div allowedNumbers></div>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment