Error script JS - Canoot read Property 'match'

Asked

Viewed 138 times

0

Hello!

This script is used to turn capital letters into minuscule, keeping the first letter uppercase..

Put in line return str.match(/\S+\s*/g); an error is occurring:

Uncaught TypeError: Canoot read property 'match'

Follow script and follow error print:

$(window).load(function() {
    $.fn.capitalize = function() {
        //palavras para serem ignoradas
        var wordsToIgnore = ["DOS", "DAS", "de", "do"],
            minLength = 3;

        function getWords(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 (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();
                }
            });
            this.value = words.join("");
        });
    };

    //onblur do campo com classe .title
    $('.title').on('blur', function() {
        $(this).capitalize();
    }).capitalize();

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<input type="text" id="txt_nome_fantasia" class="nome_fantasia title form-control input-sm" autocomplete="off" placeholder="Nome Fantasia" >

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

  • Put the html to do the tests

  • In some monento its variable str empty generating the error of the function match

  • str has its value as null, so it is returning this error...

  • @Otto even if it was empty it would not return the error, nor initialized it is.

  • @Phelipe, I included the code as an example, see that the code does its job, but with error.

1 answer

0

Simple, its function getWords is not receiving your parameter, so it does not initialize the variable str and since it is not initialized you cannot use the method .match, string-specific.

That’s why the mistake writes, [...] Cannot read property 'match' of undefined.
Translated: [...] Não consegue ler a propriedade 'correspondência' de não definido

You must pass the parameter even if empty, for example if you pass "" as parameter its function would not return error, only it would not find match in your text and return "".


I recommend that you insert a breakpoint into:

   this.each(function() {
        var words = getWords(this.value);

And make sure this.value has a value.
Unfortunately we can’t help you beyond that, because your code or your question reveal assignment or purpose of the variable this.value, that is directly linked to the problem.

But one thing I can tell you:

The cause of your problem is that the variable passed as parameter to getWords, in other words the this.value is not started.

  • this.value is the field whose class is equal to title, or the fields that should have their strings changed, I put an image for you to see.

  • @wagnerson so there’s your problem, you can’t take the value of several items and put ". value" expecting return with a general string, you should treat one string at a time, maybe it works to put its function in the ". each"

Browser other questions tagged

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