Remove jquery field mask

6

I have this code to put mask in the field:

(function ($) {
    $(function () {
        $("#txtCnpjPesquisa").mask("99.999.999/9999-99");
    });
})(jQuery);

Now here I need to pass the cnpj($("#txtCnpjPesquisa").val()) field without the mask. How do I do this?

function MontaPesquisa() {

    if ($("#txtCnpjPesquisa").val() != "")
        resultado = ({ "cnpj": $("#txtCnpjPesquisa").val() });
    else
        return;

    alert(resultado);

    $.ajax({

        url: '/Pesquisa/MontaTelaPesquisa',
        datatype: 'json',
        contentType: "application/json; charset=utf-8",
        type: "POST",
        data: JSON.stringify({ pesquisaCnpj: resultado }),
        success: function (data) {

            str += '<label>CNPJ digitado incorretamente!</label>';

            $('#filtroPesquisa').html(str);
        },
        error: function (error) {
        }

    });
}

Here is the error in Json:

$.ajax({

        url: '/Pesquisa/MontaTelaPesquisa',
        datatype: 'json',
        contentType: "application/json; charset=utf-8",
        type: "POST",
        data: JSON.stringify({ pesquisaCnpj: resultado }),
        success: function (data) {

Specifically in this line:

data: JSON.stringify({ pesquisaCnpj: resultado }),
  

Uncaught TypeError: Converting circular structure to JSON

    
asked by anonymous 05.05.2014 / 18:47

4 answers

7

Use with replace from javascript .

$("#txtCnpjPesquisa").val().replace(/[^\d]+/g,'')

that is the line

resultado = ({ "cnpj": $("#txtCnpjPesquisa").val().replace(/[^\d]+/g,'') });

It will bring you only the numbers

    
05.05.2014 / 19:21
3

Although your question is not clear (always tell me which Plugin you're using), I'll try to help.

To search for the value without the defined mask try:

$("#txtCnpjPesquisa").unmask();
    
05.05.2014 / 18:55
1

You can remove the characters in client side with jquery or in server side with PHP:

Jquery:

str = $("#txtCnpjPesquisa").val();
str = str.replace(/[^\d]+/g,"");

PHP:

$cnpj = $_POST['txtCnpjPesquisa'];
$cnpj = preg_replace("/[^0-9]/","", $cnpj);
    
05.05.2014 / 19:23
0

Put a "clean" mask on the field, grab the value and then put the actual mask again:

if ($("#txtCnpjPesquisa").val() != "")
    $("#txtCnpjPesquisa").mask("99999999999999");
    resultado = ({ "cnpj": $("#txtCnpjPesquisa").val() });
    $("#txtCnpjPesquisa").mask("99.999.999/9999-99");
else
    return;
    
05.05.2014 / 21:30