Names in inputs, with ignoring capital letters (eg, from, of, the, etc)!

1

In which personal names I want to leave it but organized, I have a script that leaves the first capital letters of the name, but it does not ignore those of, etc, etc. I wanted to know if it is possible to add this exception to the script !

function c(id) {
    var letra = document.getElementById(id).value;
    letra = letra.split("");
    var tmp = "";
    for (i = 0; i < letra.length; i++) {
        if (letra[i - 1]) {
            if (letra[i - 1] == " " || letra[i - 1] == ".") {
                letra[i] = letra[i].replace(letra[i], letra[i].toUpperCase());
            }
        } else {
            letra[i] = letra[i].replace(letra[i], letra[i].toUpperCase());
        }
        tmp += letra[i];
    }
    document.getElementById(id).value = tmp;
}
<input onkeyup="c('input-1')"  required class="inp_editar" type="text" name="nome_cont" id="input-1"/>

Obg!

    
asked by anonymous 01.08.2016 / 20:07

1 answer

2

Developing more the other answer you can do this:

var ignorar = ["das", "dos", "e", "é", "do", "da", "de"];
function caixaAlta(string) {
    return String(string).replace(/([^A-zÀ-ú]?)([A-zÀ-ú]+)/g, function(match, separator, word) {
        if (ignorar.indexOf(word) != -1) return separator + word;
        return separator + word.charAt(0).toUpperCase() + word.slice(1);
    });
}

var testes = ['a música é universal', 'a democracia é de todos'].map(caixaAlta);
console.log(JSON.stringify(testes)); // ["A Música é Universal","A Democracia é de Todos"]

And when you find a word that is not in the list, add it.

jsFiddle: link

And to integrate this into your code you can do this:

HTML:

<input onkeyup="corrigirValor(this)" ...etc

JavaScript:

function corrigirValor(el) {
    el.value = caixaAlta(el.value);
}

Example: link

    
01.08.2016 / 20:28