How to disable the Zip Mask to save only the Digits numbers when doing a Post - Asp.net MVC

1

I'm using a Remark component that has a data-plugin="formatter" in which to apply a zip mask in field . The field has size of 8 characters, but with the mask it gets 9 because of "-" (Ex: 29780-000). When saving the registry, JavaScript Validation performs client-side validation and does not allow the post because of the amount of characters.

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 get only numbers to be sent when giving post ? In Desktop applications it is possible to configure the mask not to be written, but in app web I do not know if it has. Does anyone know how to help me?

Thank you! :)

    
asked by anonymous 12.12.2018 / 23:58

1 answer

1

As you want to keep the stroke, 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 on your backend you can treat these fields by removing the "-" like this:

CEP.Trim('-');

Javascript

Another option that may be valid for you is to use JavaScript and before submitting form 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><formaction="#">
  <input type="text" id="campoCEP"/>
  <input type="submit" value="Enviar">
</form> 
    
13.12.2018 / 11:30