1
See the function, below it serves to change the first character of the string
for uppercase and other minuscule characters in the fields they have class lower
This function ignores strings smaller than 4 characters, and now I want you to ignore strings that contain @
, anyway, ignore email, and do not change the first character to uppercase.
I tried it this way and it didn’t work.
$(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>
<input type="text" class="lower "/>
Line to be revised:
if (words.indexOf(wordContainAt) != -1){
words[i] = words[i].toLowerCase();
}
From what I’ve seen you’ve done checking out the arroba
@
, right? You want me to check if it contains the wordat
also?– Sam
I want to check only the
@
, turns out it’s not working– Wagner Fillio
Exchange the
words
forword
:word.indexOf(wordContainAt)
– Sam
hehe! that’s soft. Thanks
– Wagner Fillio