Your attempt looks good, you allow the user to enter something, and then check to see if it matches the required form. If not, retry the user request or print an error or something else. If you want to make the program in some way, these letters, such as "-" char, do not even appear on the screen during input, you still have to read everything that the user entered, check, string (delete the illegal letter) and refresh the screen with every keystroke. This is probably not what you need to do here.
Imagine your common social sites when creating a profile with password requirements, for example, with one number, at least 6 letters, etc. You can enter everything you want into the text box only after the form is submitted, the program either accepts your input or redirects you to the same form with an error message to hint at the problem.
source share