Reputation: 113
On onkeyup, i want to replace accented characters to non-accented. With this code now, i didnt get nothing. If i dont give the this.value
to the function, i get an error for the split
.
I want to use this function for more input with onkeyup, but always for that input, what i am writing in.
<input onKeyUp="RemoveAccents(this.value);" type="text" required name="termek_seo" class="product-name" value="<?php echo isset($_POST["termek_seo"])?$_POST["termek_seo"]:""; ?>" />
function RemoveAccents(strAccents)
{
var strAccents = strAccents.split('');
var strAccentsOut = new Array();
var strAccentsLen = strAccents.length;
var accents = 'ÀÁÂÃÄÅàáâãäåÒÓÔÕÕÖØòóôõöøÈÉÊËèéêëðÇçÐÌÍÎÏìíîïÙÚÛÜùúûüÑñŠšŸÿýŽž';
var accentsOut = "AAAAAAaaaaaaOOOOOOOooooooEEEEeeeeeCcDIIIIiiiiUUUUuuuuNnSsYyyZz";
for (var y = 0; y < strAccentsLen; y++)
{
if (accents.indexOf(strAccents[y]) != -1)
{
strAccentsOut[y] = accentsOut.substr(accents.indexOf(strAccents[y]), 1);
}
else
{
strAccentsOut[y] = strAccents[y];
}
}
strAccentsOut = strAccentsOut.join('');
return strAccentsOut;
}
UPDATE:
How can i write the tolowercase in this code?
function RemoveAccents(s)
{
var i = 'ÀÁÂÃÄÅàáâãäåÒÓÔÕÕÖŐòóôõöőÈÉÊËèéêëðÇçÐÌÍÎÏìíîïÙÚÛÜŰùúûüűÑñŠšŸÿýŽž'.split('');
var o = 'AAAAAAaaaaaaOOOOOOOooooooEEEEeeeeeCcDIIIIiiiiUUUUUuuuuuNnSsYyyZz'.split('');
var map = {};
i.forEach(function(el, idx) {map[el] = o[idx]});
return s.replace(/[^A-Za-z0-9]/g, function(ch) { return map[ch] || ch; })
}
Upvotes: 2
Views: 6514
Reputation: 456
I had the same issue recently and created this function to replace the accented characters using a map and regex:
removeAccents = function (str) {
const accentsMap = {
'à': 'a', 'á': 'a', 'â': 'a', 'ã': 'a', 'ä': 'a', 'å': 'a', 'À': 'A', 'Á': 'A', 'Â': 'A', 'Ã': 'A', 'Ä': 'A', 'Å': 'A',
'ç': 'c', 'Ç': 'C',
'è': 'e', 'é': 'e', 'ê': 'e', 'ë': 'e', 'È': 'E', 'É': 'E', 'Ê': 'E', 'Ë': 'E',
'ì': 'i', 'í': 'i', 'î': 'i', 'ï': 'i', 'Ì': 'I', 'Í': 'I', 'Î': 'I', 'Ï': 'I',
'ñ': 'n', 'Ñ': 'N',
'ò': 'o', 'ó': 'o', 'ô': 'o', 'õ': 'o', 'ö': 'o', 'Ò': 'O', 'Ó': 'O', 'Ô': 'O', 'Õ': 'O', 'Ö': 'O',
'ß': 'ss',
'ù': 'u', 'ú': 'u', 'û': 'u', 'ü': 'u', 'Ù': 'U', 'Ú': 'U', 'Û': 'U', 'Ü': 'U',
}; return str.replace(/[àáâãäåÀÁÂÃÄÅèéêëÈÉÊËìíîïÌÍÎÏòóôõöÒÓÔÕÖùúûüÙÚÛÜñÑçÇß]/g, match => accentsMap[match]);
}
Hopefully this helps someone, should be pretty easy to map new characters if need be as well.
Upvotes: 0
Reputation: 113
Ive solve the problem with an another code:
function RemoveAccents(s)
{
var i = 'ĂĂĂĂĂĂà åâãäüĂĂĂĂĂĂĹòóôþÜĹĂĂĂĂèÊêÍðĂçĂĂĂĂĂĂŹĂĂŽĂŻĂĂĂĂŰÚúÝߏĂùŠťŸÿýŽŞ+_.:;[]()/*"<> '.split('');
var o = 'AAAAAAaaaaaaOOOOOOOooooooEEEEeeeeeCcDIIIIiiiiUUUUUuuuuuNnSsYyyZz---------------'.split('');
var map = {};
i.forEach(function(el, idx) {map[el] = o[idx]});
return s.replace(/[^A-Za-z0-9]/g, function(ch) { return map[ch] || ch; }).toLowerCase();
}
Upvotes: 2
Reputation: 35503
It just works...
function RemoveAccents(strAccents) {
var strAccents = strAccents.split('');
var strAccentsOut = new Array();
var strAccentsLen = strAccents.length;
var accents = 'ÀÁÂÃÄÅàáâãäåÒÓÔÕÕÖØòóôõöøÈÉÊËèéêëðÇçÐÌÍÎÏìíîïÙÚÛÜùúûüÑñŠšŸÿýŽž';
var accentsOut = "AAAAAAaaaaaaOOOOOOOooooooEEEEeeeeeCcDIIIIiiiiUUUUuuuuNnSsYyyZz";
for (var y = 0; y < strAccentsLen; y++) {
if (accents.indexOf(strAccents[y]) != -1) {
strAccentsOut[y] = accentsOut.substr(accents.indexOf(strAccents[y]), 1);
} else {
strAccentsOut[y] = strAccents[y];
}
}
strAccentsOut = strAccentsOut.join('');
console.log(strAccentsOut);
return strAccentsOut;
}
<input onKeyUp="RemoveAccents(this.value);" type="text" required name="termek_seo" class="product-name" value="ÀÁÂÃÄÅàáâãäåÒÓÔÕÕÖØòóôõöøÈÉÊËèéêëðÇçÐÌÍÎÏìíîïÙÚÛÜùúûüÑñŠšŸÿýŽž" />
Upvotes: 4