Created
March 19, 2017 04:35
-
-
Save harishkotra/89a5f00f830ac3aefe1103ab8b8884e1 to your computer and use it in GitHub Desktop.
Ionic 1 directive to disable special characters in an input field
This file contains hidden or 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
.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