Ignore strings that contain "@" - jquery

1

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();
}
    
asked by anonymous 08.02.2018 / 20:15

2 answers

2

The problem is that you are not getting the value correctly passed in the .each loop in the word parameter:

if (words.indexOf(wordContainAt) != -1){

The correct would be word and not words :

if (word.indexOf(wordContainAt) != -1){
    
08.02.2018 / 20:32
0

To check whether a string is an email or not, use this function:

//Verifica se a string é um email
    function isEmail(email) {
        var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        return regex.test(email);
    }
    
08.02.2018 / 20:22