Skip to content

Instantly share code, notes, and snippets.

@Lodo4ka
Created July 17, 2019 09:36
Show Gist options
  • Select an option

  • Save Lodo4ka/dd68bf00433b7d56490dcb67e86887b2 to your computer and use it in GitHub Desktop.

Select an option

Save Lodo4ka/dd68bf00433b7d56490dcb67e86887b2 to your computer and use it in GitHub Desktop.
custom directive angularjs for replace input with only numbers
(function() {
angular.module("BauVoiceApp").directive("customValidation", function() {
return {
require: "ngModel",
link: function(scope, element, attrs, modelCtrl) {
modelCtrl.$parsers.push(function(inputValue) {
const numberPattern = /\d+/g;
let transformedInput = inputValue.match(numberPattern)[0];
if (transformedInput != inputValue) {
modelCtrl.$setViewValue(transformedInput);
modelCtrl.$render();
}
return transformedInput;
});
},
};
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment