Here is some code which can show you how to do this for phone numbers of the format (xxx) xxx-xxxx. Only numbers are allowed to be entered, with the brackets and dash being automatically applied. You can adapt the code to any other format desired. https://www.wixcodebank.com/dynamically-format-user-input
Hope this helps!