.directive('urlSafe', function(){ return { require: 'ngModel', link: function(scope, element, attrs, modelCtrl) { modelCtrl.$parsers.push(function (inputValue) { if(inputValue){ if(!inputValue.length) { return inputValue; } var transformedInput = inputValue.replace(/[^a-zA-Z0-9_-]/g, ''); if (transformedInput!=inputValue) { modelCtrl.$setViewValue(transformedInput); modelCtrl.$render(); } return transformedInput; } return inputValue; }); } }; });