Skip to content

Instantly share code, notes, and snippets.

@harishkotra
Created March 19, 2017 04:35
Show Gist options
  • Save harishkotra/89a5f00f830ac3aefe1103ab8b8884e1 to your computer and use it in GitHub Desktop.
Save harishkotra/89a5f00f830ac3aefe1103ab8b8884e1 to your computer and use it in GitHub Desktop.
Ionic 1 directive to disable special characters in an input field
.directive('noSpecialChar', function() {
return {
require: 'ngModel',
restrict: 'A',
link: function(scope, element, attrs, modelCtrl) {
modelCtrl.$parsers.push(function(inputValue) {
if (inputValue == null)
return ''
cleanInputValue = inputValue.replace(/[^\w\s]/gi, '');
if (cleanInputValue != inputValue) {
modelCtrl.$setViewValue(cleanInputValue);
modelCtrl.$render();
}
return cleanInputValue;
});
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment