Shifting accents in Javascript

Sorry about the little post... I'm out of time...

There's 2 big problemns for accentuation of letters, codepages and database.
In Oracle for sample, A char 'Á' use 2 positions of a column varchar2 and can cause a error:

ORA-12899: value too large for column "OWNER_NAME"."TABLE_NAME"."COLUMN_NAME" (actual: 98, maximum: 50)


I created the follow script to bypass it.


<html>
<head>
</head>
<body>
<script>
var arrayAcentos = ['Á','á','Â','â','À','à','Å','å','Ã','ã','Ä','ä','Æ','æ','É','é','Ê','ê','È','è','Ë','ë','Ð','ð','Í','í','Î','î','Ì','ì','Ï','ï','Ó','ó','Ô','ô','Ò','ò','Ø','ø','Õ','õ','Ö','ö','Ú','ú','Û','û','Ù','ù','Ü','ü','Ç','ç','Ñ','ñ','Ý','ý'];
var arraySemAcentos = ['A','a','A','a','A','a','A','a','A','a','A','a','&','&','E','e','E','e','E','e','E','e','D','o','I','i','I','i','I','i','I','i','O','o','O','o','O','o','0','0','O','o','O','o','U','u','U','u','U','u','U','u','C','c','N','n','Y','y'];
function removeAcentos(e) {
var k = e.keyCode ? e.keyCode : e.charCode;
var a = String.fromCharCode(k)
var i = -1;
if ( !arrayAcentos.indexOf ) {
for(var n = 0; n < arrayAcentos.length; n++)
if(arrayAcentos[n] == a) i=n;
} else {
i = arrayAcentos.indexOf(a);
}
if (i == -1) return;
var b = arraySemAcentos[i];
if ( e.keyCode && String.fromCharCode(e.keyCode) == a ) {
e.keyCode = b.charCodeAt(0)
return
}
if (String.fromCharCode(e.charCode) == a) {
var newEvent = document.createEvent("KeyEvents")
newEvent.initKeyEvent("keypress", true, true, document.defaultView,
e.ctrlKey, e.altKey, e.shiftKey,
e.metaKey, 0, b.charCodeAt(0))
e.preventDefault()
e.target.dispatchEvent(newEvent)
}
}

</script>
<input onkeypress="return removeAcentos(event)" />
</body>
</html>
Postar um comentário

Postagens mais visitadas