How to leave the first capital letter?

4

I have the function, and it works perfectly:

$.fn.capitalize = function() {
function isTRChar(key) {
    var trchar = [231, 246, 252, 287, 305, 351];
    for (var i = 0; i < trchar.length; i++) {
        if (trchar[i] == key) return true;
    }
    return false;
}

$.each(this, function() {

    var $this = $(this);
    if ($this.is('textarea') || $this.is('input:text')) {
        $this.on({
            keypress: function(event) {
                var pressedKey = event.charCode == undefined ? event.keyCode : event.charCode;
                var str = String.fromCharCode(pressedKey);
                if (pressedKey == 0) {
                    if (!isTRChar(pressedKey)) return;
                }

                this.value = this.value.replace(/\b[a-z]/gi, function($0) {
                    return $0.toUpperCase();
                });
                this.value = this.value.replace(/@([a-z])[\w\.]*\b/gi, function($0, $1) {
                    return $0.substring(0, 2).toUpperCase() + $0.substring(2).toLowerCase();
                });

                if (!this.createTextRange) {
                    var startpos = this.selectionStart;
                    var endpos = this.selectionEnd;
                    if (!$(this).attr('maxlength') ||
                        this.value.length - (endpos - startpos) < $(this).attr('maxlength')) {
                        this.setSelectionRange(startpos + 1, startpos + 1);
                    }
                }
            }
        });
    }
});
}

But it does not work with accent , cedilla and proper names , and also uppercase.

Examples: (how it gets when typed)

Maria De FáTima

Jose GonçAlves

How do I fix this in this function?

    
asked by anonymous 27.07.2017 / 19:46

3 answers

2

Hello you can try this [edit]: Sorry I had not checked back now.

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) {
		    //alert(split[i]);
                    split[i] = split[i].charAt(0).toUpperCase() + split[i].slice(1);
                }
            }
        }
        this.value = split.join(' ');
    });
    return this;
};
$("textarea").keypress(function(e) {
    var str = String.fromCharCode(e.which);
    $(this).capitalize(str);
});
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
 
 <textarea></textarea>
    
27.07.2017 / 20:15
2

Regular expressions also work, and only one line is enough to solve the problem. The other two are just to get the value of the text box and then to reset its value:

$("textarea").keypress(function(e) {
    var str = $(this).val();
    str = str.replace(/(^|\s|$)(?!de|do|d$)(.)/g, (geral, match1, match2) => match1 + match2.toUpperCase());
    $(this).val(str);
});
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
 
 <textarea></textarea>
    
01.08.2017 / 03:15
1

Another possible solution is to use map to transform the value in your capitalized and an array of invalid values to capitalize:

const invalidos = ["de", "do"];

$("#capitalizar").on("click", function(){
    let novoTexto = $("#texto")
        .val()
        .split(" ")
        .map(palavra => invalidos.includes(palavra) || palavra.length < 2 ? 
            palavra : 
            palavra.charAt(0).toUpperCase() + palavra.slice(1))
        .join(" ");
 
    $("#texto").val(novoTexto);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputtype="text" id="texto">
<input type="submit" value="Capitalizar" id="capitalizar">
    
27.07.2017 / 21:20