You can achieve this either client-side or server-side.
For client-side, you can mask the HTML input using using JavaScript. Check this page for reference.
And if you need server-side, create a custom validation attributes with model data annotations. You can check this page to familiarize with the validation attributes. Then you can search how to do a custom validation attribute so that you could achieve your logical criteria for masking the input.