Here's a demo if you wanna play with it:
https://codepen.io/moinism/pen/PoGXrdj?editors=1010
P.S: It's just a simple and lazy implementation, could be made more generalized.
P.P.S: It does not mask the input. It's just a method which can be used anywhere.