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

Asked

Viewed 184 times

2

I have a < input type="text" id="A" >, I would like that when typing any text in this input, it would be rewritten in input B, but before the text is rewritten, I would like it to be "cleaned". This cleaning is to take out the special characters of the text, and to replace the "spaces" by "-".

Currently I already do this "cleaning" with php, only it is not something dynamic. So not always the clean text is the way I want! An example of this is the following situation!

Before:

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

Afterward

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

Now with this rewriting of the text I will be able to better control the final result! So in case I don’t like it I’ll be able to edit the final text even before registering 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'))));

1 answer

5


To make a replaceAll in all "spaces" need to implement the rest is simple thing, 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>
<input type="text" id="A" />
<input type="text" id="B" />

The regular expression part follows the link http://snipplr.com/view/15532/replace-de-caracteres-especiais-por-seu-equivalente-sem-acento/

Tested with Chrome on: http://fiddle.jshell.net/sneeps_ninja/wxse2p7u/1/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.