Search code examples
c#keypressspacesmaskedtextbox

Prevent spaces in a MaskedTextbox


How can I prevent spaces in a maskedtextbox? Used KeyPress but the e.handled doesnt work. Can somebody direct me to a post that shows how to handle this? The masked textbox is used for ssn. Thanks


Solution

  • Rather than 9's you'd want 0's, due to 9's allowing spaces as acceptable characters

    A US Social Security Number mask would look like this:

    myMaskedTextBox.Mask = "000-00-0000";
    

    In addition you'll want to make sure the ResetOnSpace property is set to false.