How to disable ZIP Mask to save only Typed numbers when performing a Post - Asp.net MVC

Asked

Viewed 198 times

1

I’m using a Remark component that has a data-plugin="formatter" where it applies a CEP mask to field. The field has a size of 8 characters, but the mask has 9 because of the "-" (Ex: 29780-000). When saving the record the JavaScript Validation validates on the client side and does not give the post because of the character rating.

inserir a descrição da imagem aqui

Viewmodel:

[DisplayName("CEP")]
[Required(ErrorMessage = "O campo CEP é obrigatório")]
[MaxLength(8, ErrorMessage = "O campo {0} deve ter no máximo {1} caracteres")]
public string CEP { get; set; }

View:

<div class="col-md-2">
    <label asp-for="PessoaFisicaViewModel.PessoasFisicasEnderecosViewModel[i].CEP" class="control-label lb-cep">CEP</label>
    <input type="text" asp-for="PessoaFisicaViewModel.PessoasFisicasEnderecosViewModel[i].CEP" data-plugin="formatter" data-pattern="[[99999]]-[[999]]" class="form-control txt-cep" />
    <span asp-validation-for="PessoaFisicaViewModel.PessoasFisicasEnderecosViewModel[i].CEP" class="text-danger validation-cep"></span>
</div>

Is there any configuration or way to have only numbers sent when giving post? In the Desktop applications you can configure the mask not to be saved, but in app web I don’t know if there’s a way. Someone knows how to help me?

Big hug!!! :)

  • Don’t you just have to change the default to date-Pattern="[[99999999]]" ? Or Do you want to keep the dash?

  • Because it is @George Wurthmann, in data-Pattern I specify the format of Mask with the dash so that it is easier for the user. Only that the trace occupies space in the field... I wish that the trace was not considered... In fact, that the characters of Mask were not considered.. only the numbers...

1 answer

1


How do you want to keep the dash, instead of changing your mask to data-pattern="[[99999999]]" change the Maxlength of the field.

[MaxLength(9, ErrorMessage = "O campo {0} deve ter no máximo {1} caracteres")]

And in your backend you can treat these fields by removing the "-" this way:

CEP.Trim('-');

Javascript

Another option that may be valid for you is to use Javascript and before doing the form Ubmit remove the character:

$("form").submit(function(){
   var cepApenasNum = $("#campoCEP").val().replace('-', '');
   $("#campoCEP").val(cepApenasNum);
   alert("Meu cep enviado no form é: " + $("#campoCEP").val());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
<form action="#">
  <input type="text" id="campoCEP"/>
  <input type="submit" value="Enviar">
</form> 

  • Thank you @George Wurthmann!!!! :)

Browser other questions tagged

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