How to implement input masking with ASP.NET MVC

2019-08-12 10:26发布

问题:

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.

回答1:

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.