Skip to main content
edited tags
Link
Peter Kelly
  • 14.4k
  • 7
  • 57
  • 64
Source Link
gacanepa
  • 353
  • 5
  • 16

Restrict numbers and letters in textbox - C#

I want to restrict what numbers and letters can be entered into a textbox. Let's say I only want to allow numbers 0-5 and letters a-d (both lower and uppercase). I already tried using a masked text box but it only let me specify numbers only, letters only (both without restriction) or numbers and letters together but in a particular order. Best scenario would be: user tries to enter number 6 and nothing gets entered into the textbox, same for letters outside the range a-f. I think the best event to use would be the Keypress event, but I am at a loss as to how I can achieve the restriction thing.