See the function below, it is used to change the first character of string
to upper case and the other characters in lower case, in fields that have class lower
This function ignores strings smaller than 4 characters, and now I want you to ignore strings that contain @
, that is, ignore email, and do not change the first character to uppercase.
I tried the following way and it did not.
$(window).load(function() {
$.fn.capitalize = function() {
//palavras para ser ignoradas
var wordContainAt = "@";
var wordsToIgnore = ["DOS", "DAS", "de", "do"],
minLength = 3;
function getWords(str) {
if (str == undefined) {
str = "abc def";
} else {
str = str;
}
return str.match(/\S+\s*/g);
}
this.each(function() {
var words = getWords(this.value);
$.each(words, function(i, word) {
// somente continua se a palavra nao estiver na lista de ignorados
if (words.indexOf(wordContainAt) != -1){
words[i] = words[i].toLowerCase();
} else if (wordsToIgnore.indexOf($.trim(word)) == -1 && $.trim(word).length > minLength) {
words[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1).toLowerCase();
} else {
words[i] = words[i].toLowerCase();
}
});
if (this.value != ""){
this.value = words.join("");
}
});
};
//onblur do campo com classe .title
$('.lower').on('blur', function() {
$(this).capitalize();
}).capitalize();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><label>Título</label><br><inputtype="text" class="lower "/>
Line to be reviewed:
if (words.indexOf(wordContainAt) != -1){
words[i] = words[i].toLowerCase();
}