1) I am trying to apply the first letter in upper case and as lower case . If the user writes in the input, it should be automatically converted. Examples:
"isaac guilherme araújo" in "Isaac Guilherme Araújo"
"ISAAC guILHErme aRAÚJO" in "Isaac Guillerme Araujo"
2) In Brazil, there are names with links. Examples: "das" "da" "dos" "do" "de" "e".
Carlos Eduardo Julio dos Santos
Carlos Eduardo dos Santos e Silva
Carlos Eduardo da Silva
3) I am having a problem with name fields. With the following code, I could apply the first letter in upper case, but the rest as lowercase letters I could not. Then, according to problem number 2, if I write:
: make uglas de oliveira júnior "
should be: "Douglas de Oliveira Junior"
should not: “Douglas de Oliveira Junior.” // value shown with current code
function contains(str, search){ if(str.indexOf(search) >= 0){ return true; } else { return false; } } $.fn.capitalize = function(str) { $.each(this, function() { var split = this.value.split(' '); for (var i = 0, len = split.length; i < len; i++) { var verify = (split[len - 1] == "D" || split[len - 1] == "d") && (str == "e" || str == "E") || (str == "o" || str == "O"); if (verify == false) { if ( contains(split[i], 'de') == false && contains(split[i], 'do') == false) { split[i] = split[i].charAt(0).toUpperCase() + split[i].slice(1); } } } this.value = split.join(' '); }); return this; }; $(".capitalize").keypress(function(e) { var str = String.fromCharCode(e.which); $(this).capitalize(str); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <label>Nome: </label> <input type="text" id="nome" name="nome" class="form-control input-sm capitalize">
I am a new member here on Stackoverflow and I apologize for the errors; I am learning javascript. Thanks!
javascript jquery
Paulo pitta
source share