JavaScript to extract accents and special characters does not work the same way in all browsers?

1

I have the following JavaScript:

function validaCaracteres(strToReplace) {
strSChar = "áàãâäéèêëíìîïóòõôöúùûüçÁÀÃÂÄÉÈÊËÍÌÎÏÓÒÕÖÔÚÙÛÜÇ";
strNoSChars = "aaaaaeeeeiiiiooooouuuucAAAAAEEEEIIIIOOOOOUUUUC";
var newStr = "";
for (var i = 0; i < strToReplace.length; i++) {
    if (strSChar.indexOf(strToReplace.charAt(i)) != -1) {
        newStr += strNoSChars.substr(strSChar.search(strToReplace.substr(i, 1)), 1);
    } else {
        newStr += strToReplace.substr(i, 1);
    }
}

return newStr.replace(/[^a-zA-Z 0-9]/g, '').toUpperCase();

}

The purpose of this is to remove special characters at typing time, special characters, accents, and if the String is a lowercase letter, return it at typing time.

However, the following error occurs, it works differently in certain Browsers, such as IE, it does not allow placement within the String, sending to the last position of the String, does not allow the reprint of the Text.

I need the text to be typed wrong if I have the option to re-edit it.

Is there any way to do this?

I'm calling this function this way:

<h:inputText value="#{manBean.manVO.field}" 
                        onblur="this.value = validaCaracteres(this.value);"
                        onkeyup="this.value = validaCaracteres(this.value);"
                        onkeydown="this.value = validaCaracteres(this.value);"/>

Is there any other way to do this, and can I use the reprint of the text?

Thank you in advance!

    
asked by anonymous 09.09.2015 / 20:16

3 answers

0

I finished using onkeypress

onkeypress="if(event.which &lt; 65 || event.which &gt; 93 ) if(event.which &lt; 97 || event.which &gt; 122 ) if(event.which &lt; 48 || event.which &gt; 57 ) if(event.which != 8) if(event.which != 32) if(event.which != 45) if(event.which != 95) return false;"/>

Thank you!

    
10.09.2015 / 13:51
3

look, you can use script fold-to- ascii to remove special characters and accents.

Instead of using the event onblur , onkeyup and onkeydown , use only oninput because it is triggered whenever the user enters some text in the input.

var teste = document.getElementById("teste");
teste.addEventListener("input", function (event) {
	event.target.value = foldToASCII(event.target.value).toUpperCase();
});
<script src="https://rawgit.com/mplatt/fold-to-ascii/eae6030cc155a59fe7859666b4fb45171c67a17f/fold-to-ascii.js"></script><inputid="teste" type="text" />
    
10.09.2015 / 14:58
2

Follow simple JS function to remove accents and special characters. Works in any browser:

function retira_acentos(str) {
	com_acento = "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝŔÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿŕ";
	sem_acento = "AAAAAAACEEEEIIIIDNOOOOOOUUUUYRsBaaaaaaaceeeeiiiionoooooouuuuybyr";
	novastr="";
	for(i=0; i<str.length; i++) {
		troca=false;
		for (a=0; a<com_acento.length; a++) {
			if (str.substr(i,1)==com_acento.substr(a,1)) {
				novastr+=sem_acento.substr(a,1);
				troca=true;
				break;
			}
		}
		if (troca==false) {
			novastr+=str.substr(i,1);
		}
	}
	return novastr;
}		

var palavras="Último coração alumínio latência opções blablabla...";
alert (retira_acentos(palavras));
    
20.07.2017 / 03:26