Search code examples
.net-4.5maskedinputasp.net-mvc-5.2

Input mask MVC unmask in controller


Im working in MVC 5.2 .NET 4.5:

Can anyone point me to a js or other library / method for an input mask that will unmask and just send the entered data in the view model to the controller?

Any .js library I've tried so far that can unmask, still sends the mask to the controller.

Thank you.


Solution

  • https://github.com/RobinHerbots/jquery.inputmask

    Set autoUnmask to true for client side validation and removeMaskOnSubmit to true to get the input with the literals removed in the server side validation or controller.

    Example of an editor template using this for a phone number:

    @model string
    
    @Html.TextBox("", Model, new { @class = "form-control" })
    
    <script type="text/javascript">
    
    $(function () {
        $('#@Html.IdForModel()').inputmask({ "mask": "(999) 999-9999", 'autoUnmask': true, 'removeMaskOnSubmit': true });
    });
    </script>