Created
May 2, 2013 23:48
-
-
Save guilhermehn/5506328 to your computer and use it in GitHub Desktop.
AngularJS directive - strip special characters
This file contains 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
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