3

I'm having issues with text that users enter into a Textbox since not all users enter text in the correct manner. The users should be entering their text in the following format

xx xxxxxx xxxxxx x

Is there a way that I can take any text they enter (normally in the format of xxxxxxxxxxxxxxx) and have it automatically add spaces?

I've looked into Regex, InsertAt, Replace, and LastIndex, but I'm not sure how to make it work. It's worth mentioning that the text is a string of numbers. Any help would be greatly appreciated.

I have tried to implement what is mentioned in the links below, but I guess I just don't understand Regex enough to make it work.

Use Regex in C# to insert spaces between ConcatenatedFileNamesLikeThis?

How to insert spaces between characters using Regex?

Regex to insert space C#

CDspace
  • 2,639
  • 18
  • 30
  • 36
SomethingStrange
  • 121
  • 1
  • 12
  • I'm not sure regex is the best solution for your problem, but if you still want to use it I would recommend playing around with a regex tool such as regex101.com. For the record, you can probably accomplish what you're trying to do by matching `^(\d{2})(\d{6})(\d{6})(\d)$` and replacing with `\1 \2 \3 \4`. – CAustin Aug 28 '17 at 17:36

1 Answers1

7

If you're using WinForms, the MaskedEdit control will force your users to provide input in the precise format that you have specified. Just supply your input mask at design-time using the Mask property and leave the rest to the control.

For your specific format, your mask should be: ## ###### ###### #. Note that a # in the mask is placeholder for one required digit. For full details of the mask placeholders, see Using the MaskedEdit Control topic in MSDN.

dotNET
  • 33,414
  • 24
  • 162
  • 251