Remove jquery field mask

Asked

Viewed 27,365 times

6

I have this code to put mask on the field:

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

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

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 on that line:

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

Uncaught Typeerror: Converting circular Structure to JSON

  • Could you tell which library you are using?

  • @pnet, it seems to me that you are using a plugin, it is important to mention that

  • @pnet, what is the result of alert(resultado);??

5 answers

9


Use with the replace of javascript.

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

namely on the line

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

He’ll bring you only the numbers

  • Your solution works, but if he wants to take away the value of another different mask he must always carry the replace above, still valid solution :)

  • I don’t get it @Cesarmiguel ?

  • It seems to me that @pnet has a Plugin where it adds masks from the .mask. You must have used this: http://igorescobar.github.io/jQuery-Mask-Plugin/

  • I know this ... he used jQuery Mask (which can be several) and the solution of replace is ideal in this case and in any case mask !!!

2

Although your question is not well clarified (always informs which Plugin you are using), I will try to help.

To find the value without the defined mask tries:

$("#txtCnpjPesquisa").unmask();
  • So, I did as Cesar passed me: if ($("#txtCnpjPesquisa").val() != "") result = ({ "cnpj": $("#txtCnpjPesquisa").unmask() }); Only that gives error in passing the parameter to my controller, the error is: Uncaught Typeerror: Circular converting Structure to JSON

  • Here is pa my Json: $.ajax({ url: '/Search/Montatelapesquisa', datatype: 'json', contenttype: "application/json; charset=utf-8", type: "POST", date: JSON.stringify({ searchCnpj: result }), Success: Function (date) { Alert(2);

  • @pnet makes var varSemMask = $("#txtCnpjPesquisa").unmask(); alert(varSemMask); and tell me what happens in the alert

  • I solved the error like this: var result = jQuery.parseJSON('{ "cnpj": "' + $("#txtCnpjPesquisa").unmask() + '"}'); however what is coming is an Object Object and not cnpj itself, so in my controller is passed Object Object and not cnpj. Shit, you don’t know shit

  • I think I’d better remove the mask in the controller. I’ll search how to do this. I think it’s easier with jquery.

2

You can remove the characters on 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);
  • I had done it by code Behind and it worked, but doing it by javascript I think it’s better in my context. Validate by browser, almost always better than in Behind, except exceptions, I think.

0

If you are using the Jquery plugin you can use cleanVal, using unmask you will be removing the mask from the field, already cleanVal you will be returning the value of the field without the mask, are separate things. I suggest you take the test with both of them and see how they behave.

 var cnpjPesquisa = $("#txtCnpjPesquisa").cleanVal();

0

Put a "clean" mask on the field, take the value and then put the real mask back on:

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

Browser other questions tagged

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