Ignore strings containing "@" - jquery

Asked

Viewed 69 times

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 word at also?

  • I want to check only the @, turns out it’s not working

  • Exchange the words for word: word.indexOf(wordContainAt)

  • hehe! that’s soft. Thanks

2 answers

2


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

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

The right thing would be word and not words:

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

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);
    }
  • I wanted to put everything in the same function.. the one I put in the example, actually, I want; whatever contains "@", continue everything minuscule

Browser other questions tagged

You are not signed in. Login or sign up in order to post.