Uncaught Typeerror: Cannot read Property 'validaDocument' of Undefined

Asked

Viewed 1,382 times

1

I am trying to create a validation function within Validationengine, however I am getting this error:

Uncaught Typeerror: Cannot read Property 'validaDocument' of Undefined

And I can’t identify what the problem is. I have another site using the same function, and I can’t see where I’m going wrong.

My code:

<!DOCTYPE html>
<html class="no-js" lang="pt-br">
    <head>
        <title>Teste</title>
        <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
        <script src="http://code.jquery.com/jquery-migrate-1.4.0.js"></script>
        <script src="/site/js/script/validationEngine/jquery.validationEngine.js"></script>
        <script src="/site/js/script/validationEngine/jquery.validationEngine-pt-BR.js"></script>
    </head>
    <body>
        <script>
            $(function(){
                $(".validar").validationEngine("attach");
                function validaDocumento(field, rules, i, options){ return true; }
                $(this).submit(function(e){ e.preventDefault(); });
            });
        </script>
        <form method="POST" class="validar formConsulta">
            <label>CPF/CNPJ:</label>
            <input type="text" class="validate[funcCall[validaDocumento]]" data-errormessage="Documento invalido!" />
            <input type="submit" value="Consultar">
        </form>
    </body>
</html>

Link to access the code: https://codepen.io/maykelesser/pen/zEgovq

I tried to change the order of Javascript (put on top, in the head, below everything, reverse functions) and always gives the same error.

What can I do to fix the problem?

1 answer

5


The problem lies here

$(function(){
    $(".validar").validationEngine("attach");
        function validaDocumento(field, rules, i, options){ return true; }
            $(this).submit(function(e){ e.preventDefault(); });
    });
});

The above code will only run after the page is loaded, you are importing the Scripts, the page is being rendered, consequently validate[funcCall[validaDocumento]] is running after import and before $(function(){, for the script will only be executed when the page is loaded.

You must put the method validaDocumento out of.

$(function(){
    $(this).submit(function(e){ e.preventDefault(); });
    $(".validar").validationEngine("attach");
});
function validaDocumento(field, rules, i, options){ return true; }

See working:

<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="https://code.jquery.com/jquery-migrate-1.4.0.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jQuery-Validation-Engine/2.6.4/jquery.validationEngine.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jQuery-Validation-Engine/2.6.4/languages/jquery.validationEngine-pt_BR.min.js"></script>
<script>
$(function(){
    $(this).submit(function(e){ e.preventDefault(); });
    $(".validar").validationEngine("attach");
});
    function validaDocumento(field, rules, i, options){ return true; }
</script>
<form method="POST" class="validar formConsulta">
    <label>CPF/CNPJ:</label>
    <input type="text" class="validate[funcCall[validaDocumento]]" data-errormessage="Documento invalido!" />
    <input type="submit" value="Consultar">
</form>

  • Perfect! Thank you very much! I will apply to the actual code now ;-)

Browser other questions tagged

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