How to implement input masking with ASP.NET MVC

Matthew Dresser picture Matthew Dresser · Nov 20, 2013 · Viewed 8k times · Source

I'm working on a form with ASP.NET MVC and am trying to figure out how best to implement input masking (e.g. for phone number or US zip code). Are there any commonly accepted approaches here for the data types I should use in my model. This question suggests to use a long for phone number and using a DisplayFormat attribute on the model field but it seems to conflict with validation. I don't really mind converting from one type in my view model to another type in my data models, but would like to chose an approach which sits well with jQuery unobtrusive validation.

Answer

Cloud SME picture Cloud SME · Nov 20, 2013

I personally like using the Masked Input Plugin. You set up the mask in JavaScript, so it is unobstrusive, but it does not use data-* attributes.

In the project I'm working on, we do use long to store the phone number, which personally I do not like.