"clear" written text in input A and rewrite it in input B using jquery

2

I have < input type="text" id="A" > , I would like that when entering any text in this input, it was rewritten in input B, but before the text is rewritten, I would like it to be "cleaned". This cleanup is to remove special characters from the text, and to replace "spaces" with "-".

I already do this "cleanup" with php, but it's not something dynamic. So the clean text is not always the way I want it! Example of this is the following situation!

Before:

"Esse é um TEXTO que ééu quero LIMpar! Verá ele limpo em Seguida!"

Then

"esse-e-um-texto-que-eeu-quero-limpar--vera-ele-limpo-em-seguida-"

Now with this rewrite of the text I will be able to control the final result better! So if I do not like it I'll be able to edit the final text before it even registers in the database.

The php code I use to clear this text is:

$ltag = strtolower(preg_replace('{\W}', ' ', preg_replace('{ +}', ' ', strtr(utf8_decode(html_entity_decode($tag)), utf8_decode('ÀÁÃÂÉÊÍÓÕÔÚÜÇÑàáãâéêíóõôúüçñ'), 'AAAAEEIOOOUUCNaaaaeeiooouucn'))));
    
asked by anonymous 13.04.2015 / 02:57

1 answer

5

To make a replaceAl l in all "spaces" need to implement the rest is simple, I made an example:

String.prototype.replaceAll = function(de, para){
        var str = this;
        var pos = str.indexOf(de);
        while (pos > -1){
                    str = str.replace(de, para);
                    pos = str.indexOf(de);
            }
        return (str);
    };
    
    var teste ={};

    teste.limpar=function(){
        $('#A').keyup(function(){
            var aux = new String( $(this).val() );            
            aux = aux.replaceAll(" ","-").toLowerCase();
            aux = teste.replaceSpecialChars(aux);
            $("#B").val(aux);
        });
    };

/**
 * Array de objectos de qual caracter deve substituir seu par com acentos
 */
var specialChars = [
	{val:"a",let:"áàãâä"},
	{val:"e",let:"éèêë"},
	{val:"i",let:"íìîï"},
	{val:"o",let:"óòõôö"},
	{val:"u",let:"úùûü"},
	{val:"c",let:"ç"},
	{val:"A",let:"ÁÀÃÂÄ"},
	{val:"E",let:"ÉÈÊË"},
	{val:"I",let:"ÍÌÎÏ"},
	{val:"O",let:"ÓÒÕÔÖ"},
	{val:"U",let:"ÚÙÛÜ"},
	{val:"C",let:"Ç"},
	{val:"",let:"?!()"}
];
 
/**
 * Função para substituir caractesres especiais.
 * @param {str} string
 * @return String
 */
teste.replaceSpecialChars= function (str) {
	var $spaceSymbol = '-';
	var regex;
	var returnString = str;
	for (var i = 0; i < specialChars.length; i++) {
		regex = new RegExp("["+specialChars[i].let+"]", "g");
		returnString = returnString.replace(regex, specialChars[i].val);
		regex = null;
	}
	return returnString.replace(/\s/g,$spaceSymbol);
};

    teste.limpar();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputtype="text" id="A" />
<input type="text" id="B" />

The part of the regular expression follows the link link

Tested with Chrome on: link

    
13.04.2015 / 14:02