Write to BD without mask applied by jQuery

Asked

Viewed 208 times

-1

I am doing a project in MVC and with the use of jQuery I made CPF mask as below:

$(document).ready(function()
{
    $(".cpf").mask("999.999.999-99");
    $(".cpf").addClass("form-control");
});

Follow my div:

<div class="form-group">
    @Html.LabelFor(model => model.Cliente_CPF, htmlAttributes: new { @class = "control-label" })
    <div>
        @Html.EditorFor(model => model.Cliente_CPF, new { htmlAttributes = new { @class = "cpf" } })
        @Html.ValidationMessageFor(model => model.Cliente_CPF, "", new { @class = "text-danger" })
    </div>
</div>

The mask is working properly, but I need this information to be recorded in my BD without the mask! Just the CPF numbers.

Like I should do?

  • I believe that if you add a function to the event submit form and within it do something like $(".cpf").val($(".cpf").val().replace(/^\D+/g, '')) would work as it would be removing any character that is not a digit.

1 answer

1

I see two options: (1) treat client-side value with Javascript (2) treat server-side value with C#. For full guarantee, you can implement both forms.

Javascript

With Javascript, you can assign a role to the event submit form and in this function remove all characters that are not CPF digits.

$(document).ready(function()
{
    $(".cpf").mask("999.999.999-99");
    $(".cpf").addClass("form-control");

    // vvvvv--- Lembre-se de colocar o seletor correto para o seu caso:
    $("#form").submit(function () {
        var cpfValue = $(".cpf").val();

        // Remove os caracteres que não são dígitos:
        cpfValue = cpfValue.replace(/\D/g, '');

       // Atualiza o valor no campo do formulário:
       $(".cpf").val( cpfValue );
    });
});

This way, the value sent will only be numerical.

C#

This language is not my area, but a quick search I saw that you can do something like:

string cpfOnlyDigits = Regex.Replace(cpf, "[^\d]", "");

So in the database, instead of saving the value of cpf, which would be the value received in the HTTP request, you would save the value of cpfOnlyDigits, the value being filtered, with only digits.

  • What would be the correct "selector"?

  • Something that identifies the element form on your page, such as id, if it does. It is the selector used by jQuery to know which element on the page it should handle.

  • I put #Registration which is the ID of my sign-up button. But I have not been successful yet. By clicking register I get the message: The value '353.655.450-44' is not Valid for CPF. That is, did not remove the mask.

  • The id must be from form, not the button. Something like <form id="idDoForm">.

  • Excuse the stupid question, but where is the form ID? No Create same?

  • I have no idea. How you are displaying the form element in HTML?

  • <div class="form-group" style="margin-left:20%; margin-top:10%;"> <div class="col-Xs-5" style="margin-left:-40px;"> <input type="Submit" value="Register" class="btn btn-Success btnNext" id="Prox" /> </div></div> for the registration button

  • But if the CPF field is numerical, as in my case, as I create a mask in an Editfor that already comes from a model, like: model => model.Cpf, this forces Editfor to only accept numbers and masks are not welcome

  • @pnet but why would the CPF be numerical? I see no reason for such.

  • @Andersoncarloswoss, I received the rule that way(bigint) in the bank and long(model-c#)

  • @pnet So I recommend you review this with whoever gave you this rule. See more on CPF or CNPJ field type in VARCHAR or INT database? Without a good reason, there’s no point in doing it. In my reply, I put the section about receiving only the digits, removing the mask; if your field is numerical, just do the conversion.

  • I get it. I think there was a lack of dialogue, but mostly understanding

Show 8 more comments

Browser other questions tagged

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