• Resolved lucasl9

    (@lucasl9)


    Hi.
    I need to create a custom validation for a field called ‘billing_cpf’. I managed to do it through a Jquery script. What I need now is to make the error message appear as shown in the attached image.

    CLICK HERE TO VIEW IMAGE

    I also attach the code I used for validation.

    function calculacpf()
    {
        if (is_page('Criar conta'))
        {
            wc_enqueue_js( "
    
            $(function()
    {
        //Executa a requisição quando o campo username perder o foco
        $('#billing_cpf').blur(function()
        {
            var cpf = $('#billing_cpf').val().replace(/[^0-9]/g, '').toString();
    
            if( cpf.length == 11 )
            {
                var v = [];
    
                //Calcula o primeiro dígito de verificação.
                v[0] = 1 * cpf[0] + 2 * cpf[1] + 3 * cpf[2];
                v[0] += 4 * cpf[3] + 5 * cpf[4] + 6 * cpf[5];
                v[0] += 7 * cpf[6] + 8 * cpf[7] + 9 * cpf[8];
                v[0] = v[0] % 11;
                v[0] = v[0] % 10;
    
                //Calcula o segundo dígito de verificação.
                v[1] = 1 * cpf[1] + 2 * cpf[2] + 3 * cpf[3];
                v[1] += 4 * cpf[4] + 5 * cpf[5] + 6 * cpf[6];
                v[1] += 7 * cpf[7] + 8 * cpf[8] + 9 * v[0];
                v[1] = v[1] % 11;
                v[1] = v[1] % 10;
    
                //Retorna Verdadeiro se os dígitos de verificação são os esperados.
                if ( (v[0] != cpf[9]) || (v[1] != cpf[10]) )
                {                     
                    $('#billing_cpf').val('');
                    $('#billing_cpf').focus();
                }
            }
            else
            {
                $('#billing_cpf').val('');
                $('#billing_cpf').focus();
            }
        });
    });  
         ");
        }
      }
     add_action( 'wp_enqueue_scripts', 'calculacpf' , 10);
Viewing 8 replies - 1 through 8 (of 8 total)
  • Saroj Shah

    (@wpeverestsupportrep)

    Hi @lucasl9

    Thanks for writing in,

    Would you please let me know what error message appears now?

    Regards!

    Thread Starter lucasl9

    (@lucasl9)

    Hi,
    No message. That’s just what I need. Trigger an event that shows the message. The image I attached is an example of what I need to be displayed.

    Saroj Shah

    (@wpeverestsupportrep)

    Hi @lucasl9

    Thanks for writing back,

    I am sorry for the late response. Would you please provide me the link to the form? I will check and let you know.

    Thanks!

    Thread Starter lucasl9

    (@lucasl9)

    Saroj Shah

    (@wpeverestsupportrep)

    Hi @lucasl9,

    This query requires proper testing for which, I request you to contact our developer team. You can contact them here: https://wpeverest.com/contact/ so that they will try to replicate the issue on your site and provide you the fix ASAP.

    Regards!

    Thread Starter lucasl9

    (@lucasl9)

    In fact, that was the first thing I did. But I got no answer.

    Plugin Support sanjuacharya77

    (@sanjuacharya77)

    Hi @lucasl9,

    We have received your query on our chat support and we will follow up with you there.

    Thanks and Regards!

    Thread Starter lucasl9

    (@lucasl9)

    Here is the code, thanks

    /*VALIDA CPF*/
    function calculacpf()
    {
        if (is_page('Criar conta'))
        {
            wc_enqueue_js( "
    
            $(function()
    {
        //Executa a requisição quando o campo username perder o foco
        $('#billing_cpf').blur(function()
        {
            var cpf = $('#billing_cpf').val().replace(/[^0-9]/g, '').toString();
    
            if( cpf.length == 11 )
            {
                var v = [];
    
                //Calcula o primeiro dígito de verificação.
                v[0] = 1 * cpf[0] + 2 * cpf[1] + 3 * cpf[2];
                v[0] += 4 * cpf[3] + 5 * cpf[4] + 6 * cpf[5];
                v[0] += 7 * cpf[6] + 8 * cpf[7] + 9 * cpf[8];
                v[0] = v[0] % 11;
                v[0] = v[0] % 10;
    
                //Calcula o segundo dígito de verificação.
                v[1] = 1 * cpf[1] + 2 * cpf[2] + 3 * cpf[3];
                v[1] += 4 * cpf[4] + 5 * cpf[5] + 6 * cpf[6];
                v[1] += 7 * cpf[7] + 8 * cpf[8] + 9 * v[0];
                v[1] = v[1] % 11;
                v[1] = v[1] % 10;
    
                //Retorna Verdadeiro se os dígitos de verificação são os esperados.
                if ( (v[0] != cpf[9]) || (v[1] != cpf[10]) )
                {                     
                  var wrapper = $('#billing_cpf').closest('.form-row');
                  wrapper.find('#billing_cpf-error').remove();
                  var billing_cpf_msg_dom ='<label id=\"billing_cpf-error\" class=\"user-registration-error\" for=\"billing_cpf\">';
                  billing_cpf_msg_dom += 'O CPF digitado é inválido!';
                  billing_cpf_msg_dom += '</label>';
                  wrapper.append(billing_cpf_msg_dom);
                  wrapper.find('#billing_cpf').attr('aria-invalid', true); 
                  $('#billing_cpf').val('');         
                }
            }
            else
            {            
                alert(cpf);
            }
        });
    });  
         ");
        }
      }
     add_action( 'wp_enqueue_scripts', 'calculacpf' , 10);
    
Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Validate field and configure label errors’ is closed to new replies.