Created
September 5, 2011 05:54
-
-
Save tutuca/1194193 to your computer and use it in GitHub Desktop.
Corrector de caracteres no deseados
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
(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