Skip to content

Instantly share code, notes, and snippets.

@guilhermehn
Created May 2, 2013 23:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guilhermehn/5506328 to your computer and use it in GitHub Desktop.
Save guilhermehn/5506328 to your computer and use it in GitHub Desktop.
AngularJS directive - strip special characters
angular.module('ng', []).directive('removeSpecials', function(){
return {
require: 'ngModel',
link: function(scope, element, attrs, modelCtrl){
var clearString = function(inputValue){
if(inputValue && inputValue.length){
var cleanString = inputValue
.replace(/(^\s+)|(\s+$)/g, '')
.replace(/[\d\-\.\/\\\[\]\,\_\*\|\;\:\'\~\^\´\`\¨\%$$\@\!\(\)\=\+\#\¹\²\³\£\¢\¬\&<\>\°\º\ª\?]/g, '')
.replace(/\s+/g, ' ')
.replace(/ç/gi, 'C')
.replace(/[ãáàâä]/gi, 'a')
.replace(/[éèêë]/gi, 'e')
.replace(/[íìïî]/gi, 'i')
.replace(/[óòõôö]/gi, 'o')
.replace(/[úùüû]/gi, 'u');
if(cleanString !== inputValue){
modelCtrl.$setViewValue(cleanString);
modelCtrl.$render();
}
return cleanString;
}
};
modelCtrl.$parsers.push(clearString);
clearString(scope[attrs.ngModel]);
}
};
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment