Put mask to @Html.Editorfor

Asked

Viewed 1,594 times

-4

In the database, the CPF field is bigint and long in the model. I have two problems with this: 1) If Cpf starts with 0 or 00, it will not record this and then, to display on screen, I will have to use Algon as padleft(11) or similiar. 2) Since I have a cshtml that was generated based on the model, then I am not able to generate the mask in the field to type Cpf. The problem is that in the bank, I will have different field sizes, for when Cpf starts with 0 or 00 and so on, I know that this would not be the problem, thanks to padleft(11).

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

With the above code, I’m not able to mask the field, for the reasons already presented. Someone suggests?

  • 2

    why didn’t it work, gave error? elements are found by selector?

  • Error did not give, but the mask did not load.

  • 1

    Just as a tip and with all the good intentions. Review your participation in pnet site. Strive to make the questions as clear as possible as soon as you open them. Always include the mistakes they make, what you tried in code, why it doesn’t work, what you think the solution might be, etc. Remember that in addition to your questions helping you, they should also help any reader who has a similar question. Taking this for example, you asked the question more or less like this "I have this code and it gives error, someone has idea of how to correct ?"

  • 1

    (...) If you didn’t even say what you were wrong about or what you’re trying to do, how is anyone going to be able to help? This forces those who want to answer to have to make several comments until they can find out, or to respond with something that was not what you wanted.

  • I made an issue and see that now I get something

1 answer

1


You need to include the reference of the Jquery.inputmask in his View.

$("#cpf").inputmask("mask", {
  "mask": "999.999.999-99"
}, {
  reverse: true
});
$("#nascimento").inputmask("mask", {
  "mask": "99/99/9999"
}, {
  reverse: true
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://rawgit.com/RobinHerbots/Inputmask/3.x/dist/jquery.inputmask.bundle.js"></script>

<p>CPF: <input type="text" id="cpf" /></p>
<p>Nascimento: <input type="text" id="nascimento" /></p>

  • It now worked only the date. Cpf no

  • Of course, CPF is numerical. As I do in such cases, assign a mask and when I record, only numbers?

  • @pnet to remove the mask in Submit use removeMaskOnSubmit. In the documentation you have all the options that the plugin accepts: https://github.com/RobinHerbots/Inputmask

Browser other questions tagged

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