Skip to content

Instantly share code, notes, and snippets.

@tutuca
Created September 5, 2011 05:54
Show Gist options
  • Save tutuca/1194193 to your computer and use it in GitHub Desktop.
Save tutuca/1194193 to your computer and use it in GitHub Desktop.
Corrector de caracteres no deseados
(function($){
$.fn.corrector = function(options) {
var defaults = {
'withespace': ' ',
'doublespace': ' ',
};
var options = $.extend(defaults, options);
return this.each(function(){
$(this).keypress(function() {
var obj = this;
setTimeout(function() {
var text = obj.value;
var selStart = obj.selectionStart;
/*
(\W|\s)\1+ = encuentra dos repetidos iguales, falla para ` ; `
\s+?['";:,.\/?\\-] = encuentra ` ; ` pero no `;;`
*/
re = /\s+?['";:,.\/?\\-]|(\W|\s)\1+/g;
var newText = text.replace(re, function(match, index) {
if (index < selStart) {
selStart -= (match.length - 1); // correct the selection location
}
return(match.substr(0,1));
});
if (newText != text) {
obj.value = newText;
obj.selectionStart = obj.selectionEnd = selStart;
}
}, 1);
})
});
}
})($);
$('document').ready(function(){
$('textarea').corrector()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment