Search code examples
javascriptnon-ascii-characters

Replacing accented characters with javascript


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; })
}

Solution

  • 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();
    }